Reputation: 3
My python script suddenly stops when it executes a certain piece of code and before that it was working fine but now it like crashes when it executes it Here is my code:
import random; from time import sleep
while True:
if 'playerName' in locals() or 'playerName' in globals():
continue
else:
print('What is your name?')
playerName = input()
print(f'Hi there, {playerName}, welcome to this math game.')
sleep(2)
print('This game can make yourself custom equations to test your math. (only addition.)')
sleep(2)
print('Set the limit for numbers. (example. 1000 and 1234)')
limit1 = int(input())
limit2 = int(input())
number1 = random.randint(limit1, limit2)
number2 = random.randint(limit1, limit2)
answer = number1 + number2
print(f'Here is the question: {number1} + {number2}')
sleep(1.5)
print('Do you know the answer for this?')
playerAnswer = input()
if int(str(playerAnswer)) == answer:
print(f'Good job, {playerName} You answered correctly!')
elif int(str(playerAnswer)) != answer:
print('Nope the answer is ' + str(int(answer)) + '.')
print('Do you want to restart? (y/n)')
restart = input()
if restart == 'y':
continue
else:
print('Goodbye')
break
It stops at the very end once it finishes one round. Can someone help me with this?
Upvotes: 0
Views: 952
Reputation: 115
if 'playerName' in locals() or 'playerName' in globals():
continue
This statement will be only one executed after player will choose "Yes" and will not let you go to the next "print" statement, because 'playerName' will be always in locals() after first round of game.
Simple fix can look like that:
if not ('playerName' in locals() or 'playerName' in globals()):
print('What is your name?')
playerName = input()
Upvotes: 1