0

Ok, so, I'm trying to make a "guess the number" game, a game where you say a number and the other player say "Lower" or "Higher" depending on your answer, and when you guess the number correctly you win.

Maybe this was already answered, but I can't figure out what's wrong.

I don't get it, if inside the function you call itself, it is supposed to run itself again, right?

Not sure if it's helpful but I'm using Python 3.

number = 897
attempts = 0

def guess():
    guess = input("Number: ")
    guess = int(guess)
    global attempts
    if guess > number:
        print("It's lower.")
        attempts = attempts + 1
        guess()
    elif guess < number:
        print("It's higher.")
        attempts = attempts + 1
        guess()
    else:
        print("Correct! The number was " + str(number) + "!")
        print("It took you " + str(attempts) + "!"),

print("I'm thinking of a number, guess it!")
guess()
zCraazy
  • 3
  • 1
  • 3

2 Answers2

0
....

def guess():                    # guess is a function
    guess = input("Number: ")
    guess = int(guess)          # it's become int now
    global attempts
    if guess > number:
        print("It's lower.")
        attempts = attempts + 1
        guess()                 # you're trying to call an int object, because you defined it as a int object as I said.

....

So please change your function name or that variable name.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
0

Variable name is the same as function name. You've forgotten to add "attempts" at the correct guess string.

number = 897
attempts = 0

def guess1():
    guess = input("Number: ")
    guess = int(guess)
    global attempts
    if guess > number:
        print("It's lower.")
        attempts = attempts + 1
        guess1()
    elif guess < number:
        print("It's higher.")
        attempts = attempts + 1
        guess1()
    else:
        print("Correct! The number was " + str(number) + "!")
        print("It took you " + str(attempts) + " attempts !"),

print("I'm thinking of a number, guess it!")
guess1()
saltcracker
  • 321
  • 3
  • 17
  • "You've forgotten to add "attempts" at the correct guess string." I realized that after posting the code here xd – zCraazy Nov 08 '15 at 16:28