0

I wanted to make a number guesser game, and I wrote this code:

from random import randint as rand

number=rand(-1,1001)
tries=0
def numguess():
    guess=int(input("The chosen number is between 0 to 1000.\nEnter your guess : "))
    tries=tries+1    
numguess()

while True:
    if number==guess:
        print ("You won. My number was effectively" ,number,". \n It took you ",tries, "to guess the number.")
        break 

    elif number<guess:
        print ("The number I chose is lower than your guess")
        numguess()

    else:
        print ("The number I chose is higher than your guess")
        numguess()

When I run it, it asks me for input then raises a UnboundLocalError. Is there something I am doing wrong? I tried searching but I don't understand. Thank you.

4

1 回答 1

1

your tries variable is treated locally - since you assign value to it, use:

global tries
tries = tries +1

in your function

于 2017-11-28T12:32:16.563 回答