0

I'm attempting to create a simple "number guessing game" in Python.

My code:

minimum = 1
maximum = 100
current_number = 50




def new_number(x):
    global sign, current_number, minimum, maximum
    if x == ">":
        minimum = current_number + 1
        curent_number = minimum + maximum / 2
        guess()
    else:
        maximum = current_number - 1
        current_number = minimum + maximum / 2
        guess()


print "Pick a number between 1 - 100, keep it in your head"
print "I'm going to guess it within 6 guesses"

def guess():
    print "Is your number > or < %d"  % current_number

guess() 

sign = raw_input(": ")
new_number(sign)

Attempting to run it with the number "27" seems to work fine for the first iteration. However, after an input is placed on the second iteration, where the input == ">", I receive:

bash: syntax error near unexpected token `newline'

There's no specific line number that the error is pointing to. I am certain it has to do with the if x == ">": section.

Zachary M.
  • 17
  • 1
  • 9
  • 1
    What do you mean the "second iteration"? There is no iteration in your code. – kindall Apr 25 '17 at 19:04
  • 1
    **How** you're running your code matters hugely -- the very fact that it's bash, not python, that's generating the error message indicates that the python interpreter isn't being started correctly, *or* that there's non-Python code involved in this problem you haven't included in the question. – Charles Duffy Apr 25 '17 at 19:10
  • 2
    How are you starting it? `python yourscript`? Just `./yourscript`? (If it's the latter, you'll want a shebang as the first line -- like `#!/usr/bin/env python`). – Charles Duffy Apr 25 '17 at 19:10
  • you are not in a loop, your 'second iteration' is not python at all, your python script already returned. – DSLima90 Apr 25 '17 at 19:11
  • This looks like bash thinks you're trying to run a shell script. How did you run the program? ./file_name.py would only work if you specify the interpreter at the top (e.g. `#!/usr/bin/env python`). Otherwise, python file_name.py will work. – Ryan McCleary Apr 25 '17 at 19:14
  • What I was not noticing is that the script ended after printing a second time. When I entered ">" on my "second iteration", I was actually inputting that into the bash menu, no longer in python. The ">" section of the new_number() function still doesn't actually work yet however. – Zachary M. Apr 25 '17 at 19:25
  • If you only get an error from Python when you paste code into a REPL and not when you tell the interpreter to run code from a file, that should be incorporated into your question. Most folks will assume that you're running code from a file, as with `python yourfile.py`. – Charles Duffy Apr 25 '17 at 19:27

1 Answers1

1

You are not in a loop, your 'second iteration' is not python at all, your python script already returned.

Check these changes to your code:

minimum = 1
maximum = 100
current_number = 50




def new_number(x):
    global sign, current_number, minimum, maximum
    if x == ">":
        minimum = current_number + 1
        current_number = (minimum + maximum) / 2
        guess()
    else:
        maximum = current_number - 1
        current_number = (minimum + maximum) / 2
        guess()


print "Pick a number between 1 - 100, keep it in your head"
print "I'm going to guess it within 6 guesses"

def guess():
    print "Is your number >, < or = %d"  % current_number

guess() 

while(1):
    sign = raw_input(": ")
    if (sign == '='):
        break
    new_number(sign)

The problem is that as you were not in a loop, when your script returned after first iteration, you probably hit < <enter> in bash, so you got a bash error.

I also suggest that you refactor your code to avoid using globals, take a look at: Why are global variables evil? to see how this is bad for your code.

Community
  • 1
  • 1
DSLima90
  • 2,680
  • 1
  • 15
  • 23
  • That was exactly what I was doing. This works to maintain a loop. However when ">" is entered, no changes are made, only the "else" section of new_number(x) functions properly. – Zachary M. Apr 25 '17 at 19:23
  • That was a typo in your original code that i also missed: `curent_number = minimum + maximum / 2` note that it should be current_number. – DSLima90 Apr 25 '17 at 19:40
  • also, i didn't analise your full logic but i think `minimum + maximum / 2` should be `(minimum + maximum) / 2` – DSLima90 Apr 25 '17 at 19:42
  • I edited the answer with the proposed changes and also linked some resource to help you be a better coder! – DSLima90 Apr 25 '17 at 19:53
  • Yes, correcting the typo and adding the parenthesis corrected the code to exactly what I was trying to achieve. – Zachary M. Apr 25 '17 at 20:11