0

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)
toonarmycaptain
  • 2,093
  • 2
  • 18
  • 29
  • I ran your code and I didn't seem to get an error :C Edit: This is using python 3.6.1 on Windows – Sasha May 05 '17 at 12:51
  • I should clarify, it's not throwing an error as in not working, but running correctly, with a warning next to lines 3, 21 in my code. – toonarmycaptain May 05 '17 at 12:53
  • maybe http://stackoverflow.com/questions/28843079/redefined-outer-name-from-outer-scope-python3 – binboavetonik May 05 '17 at 12:53
  • 1
    A warning is just a warning. The one about `max` is because Python has a built-in function called `max` and by using the same name you won't be able to later use the function `max` (in the same scope). When unintentional, such things can lead to difficult to find bugs and are best avoided. The other warning seems more pointless. Different IDEs will give different warnings (if any -- IDLE doesn't give warnings). – John Coleman May 05 '17 at 12:55

1 Answers1

0

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.

toonarmycaptain
  • 2,093
  • 2
  • 18
  • 29