简体   繁体   中英

Python: keep game running after converting in py2exe

So I am new to programming and started learning python from a recommendation, I have been combining learning from Learn python the hard way and code academy.

I have made the Pig latin game from code academy and tweaked it slightly so it keeps looping however once I convert it with the help of py2exe to run as an exe in windows it no longer works and I was just wondering if anyone could point me in the right direction to how to change my script slightly to work.

print "Let's do some Pig Latin!"
pyg = 'ay'

original = raw_input('Enter a word:')

word = original.lower()
first = word[0]
new_word = word + first + pyg


if len(original) > 0 and original.isalpha():
    print new_word [1:]
    again = raw_input('Would you like to play again? Y/N \n').upper()
else:
    print 'You were supposed to enter a word!,Try again'
    execfile ('pig.py')

if again == 'Y':
    execfile ('pig.py')
else:
    print 'Goodbye!'

See it works on the whole fact of just rerunning the script with execfile, but obviously this wouldn't work as a exe file, I have looked at some other questions and answers but because I already had the script kind of working I have confused myself and I just kind of need help understanding how I would restructure the script.

Thanks for any help in advance guys.

You should use a while loop instead of re-executing the file:

should_run = True
print "Let's do some Pig Latin!"
while should_run:
    pyg = 'ay'

    original = raw_input('Enter a word:')

    word = original.lower()
    first = word[0]
    new_word = word + first + pyg


    if len(original) > 0 and original.isalpha():
        print new_word [1:]
        again = raw_input('Would you like to play again? [Y/N] \n').upper()
        while again not in "NY":
            again = raw_input("Invalid input. Play again? [Y/N] ").upper()

        should_run = "NY".index(again) # If again is Y, should_run is 1 (True).  Otherwise, it is 0 (False)
    else:
        print 'You were supposed to enter a word!,Try again'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM