I need to make my program start over in Python if the enter key is pressed. I found this question and solution: how to check if the enter key is pressed python. However when I googled event.keysym, it seemed to have something to do with Tkinter which I don't think I have.
When I try using the solution I get an error:
Traceback (most recent call last):
File "/home/q/Desktop/PigsAndBulls.py", line 52, in <module>
if event.keysym == 'Return':
NameError: name 'event' is not defined
I am a complete newbie having just completed a course with Dr. Severance on Coursera.
Here is the program I wrote to play pigs and bulls at work. Everything works as I want. The only problem is to exit the program if any key other than the "enter" button is pushed.
while True:
while True:
word= raw_input("Enter a four letter English word with no repeating letters: ")
print
if len(word) <> 4:
print "What part of 'four letter word' did you not understand? Try again."
print
continue
else: break
guesses = 0
while True:
correct = 0
position = 0
cnt = 0
result = 0
guess= raw_input("Enter a guess: ")
guesses = guesses+1
#print "guessses", guesses
for w in guess:
cnt = cnt+1
#print "cnt", cnt
position=0
for g in word:
position=position+1
#print "position", position
if g == w:
correct = correct+1
if position == cnt:
result = result+1
#print "result", result
print
print "Number correct:", correct
print "Number in the right position:", result
print
if correct<>4 and result<>4:
print "Give me another guess"
print
continue
elif correct == 4 and result == 4:
print
print "YOU WIN"
print
print "It took you", guesses, " guesses to get it right"
print
break
answer= raw_input("press ""enter"" to play again")
if event.keysym == 'Return':
continue
else:
exit
print
print
Then I thought, maybe I have replace "event" with my string variable "answer" but then I got this error:
Traceback (most recent call last):
File "/home/q/Desktop/PigsAndBulls.py", line 52, in <module>
if answer.keysym == 'Return':
AttributeError: 'str' object has no attribute 'keysym'
Also, If I press any other key, it simply prints in Idle and the program does not exit.
By the way, I know there has to be a better way to program this using lists or dictionaries, but this is all I know how to do.