- Mac OS 10.13.16
- Python 3.7
- PyCharm
I was going through a tutorial for this guessing game and I came across something I thought was weird. On line 18 I call on the guess variable, which I though was a Local Variable under the for loop created above it, let's me call on it as if it were a Global. I though if a var is declared within a function or loop it makes it a local. Can someone help explain this to me.
import random
print("Hello what is your name?")
name = input()
print("Well " + name + " I am thinking of a number between 1 and 20")
secretNumber = random.randint(1,20)
for guessesTaken in range(1, 7):
print("Take a guess.")
guess = int(input())
if guess < secretNumber:
print("Sorry to low")
elif guess > secretNumber:
print("Sorry to high")
else:
break
if guess == secretNumber:
print("Great job " + name + ". You guessed my number in " + str(guessesTaken) + " moves.")
else:
print("Sorry the number I was thinking of is " + str(secretNumber))