1

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.

Mikech
  • 27
  • 8

1 Answers1

1

pressing enter would result in a zero-length word. make that your first check.

however, if you want to catch a single keyhit, like getch() in C, it's a lot more complicated, e.g. https://stackoverflow.com/a/6599441/493161

another alternative would be to trap ^C (control-C):

try:
    answer = raw_input('Control-C to exit, <ENTER> to play again: ')
    if len(answer) > 0:
        raise(ValueError('Unexpected input'))
    else:
        continue
except (KeyboardInterrupt, ValueError):
    sys.exit(0)
Community
  • 1
  • 1
jcomeau_ictx
  • 37,688
  • 6
  • 92
  • 107
  • Yea, I found that and read it but most of it is so far above my skill level that I didn't understand it or how to use it. The one comment that I could understand was "that isn't possible in Python." So I will give up this idea and switch to a character that Python can read, and then use 'try' and 'except' to exit if it isn't pressed: Maybe something like "type 'more' in lower case to play again. Any other word or character will terminate the program" – Mikech Feb 21 '16 at 06:33
  • 1
    I realized after I left my comment that you actually had a great idea and I implemented it. It works great. Thank you. I don't have enough reputation to add to your reputation though, I'm sorry to say. – Mikech Feb 22 '16 at 17:07
  • 1
    P.S. Your code is much better than mine so I cut and paste it in and it works great also!! Thank you again. – Mikech Feb 22 '16 at 17:21