I wanted to experiment with != instead of = here (by switching the if
and else
statements), in case I wanted to add an elif: after (maybe a test-response if entered guess is greater than max).
I don't understand why it is now throwing a warning/error, or what that warning means.
To clarify, the code runs and seems to work, but I get a warning in the repl.it I'm using to write/run.
On line 3 def make_guess
It says - Redefining name 'guess' from outer scope (line 21)
<=What does this mean?
And then on line 21 max=int(input("Guess number between 1 and what?"))
It says Redefining built-in 'max'
[Although I'm guessing this isn't the issue, because if I rename that variable maxi
it doesn't show the warning for line 21]
Original code:
import random
def make_guess(guess):
if guess == randomnumber:
print()
print("Congratulations, you guessed my number!")
else:
guess = int(input("Guess again?"))
make_guess(guess)
print("GUESS MY NUMBER")
print()
max = int(input("Guess number between 1 and what?"))
randomnumber = random.randint(1, max)
guess = int(input("What is your best guess?"))
make_guess(guess)
print("My number was", randomnumber)
New code:
import random
def make_guess(guess):
if randomnumber != guess:
guess = int(input("Guess again?"))
make_guess(guess)
else:
print()
print("Congratulations, you guessed my number!")
print("GUESS MY NUMBER")
print()
max = int(input("Guess number between 1 and what?"))
randomnumber = random.randint(1, max)
guess = int(input("What is your best guess?"))
make_guess(guess)
print("My number was", randomnumber)
This particular warning was given by my IDE (in this case repl.it).
It was letting me know that I was reusing/redefining a variable (in this case guess
) in a different scope (ie within the function) and that this might be an issue.
The second warning was being given because I was using max
as a variable name, and max
has other uses within python, chiefly as a the max
Built-in Function used with lists and other iterables. Redefining max
could also be an issue if you actually wanted to use the Built-in function. In this case it is not, but the IDE helpfully warns you, as this isn't an advisable practice.