如何在python中正确退出程序

时间:2012-10-23 01:33:46

标签: python string python-3.x quit

我是一名中学生,我开始学习python中的编码。我一直在看视频教程,但我似乎无法弄清楚如果你键入q如何让游戏退出。在这里我有... ..

print('How old do you thing Fred the Chicken is?')
number = 17

Quit = q
run = 17
while run:

guess = int(input('Enter What You Think His Age Is....'))

print('How old do you thing Fred the Chicken is?')
number = 17

Quit = 'q'
run = 17
while run:

guess = int(input('Enter What You Think His Age Is....'))

if guess == number:
    print('Yes :D That is his age...')
    run = False
elif guess < number:
    print('No, Guess a little higher...')
elif guess > number:
    print('No, Guess a little lower....')

print('Game Over')
print('Press Q to Quit')

if run == False:
choice = input('Press Q to Quit')
if choice == 'q'
import sys
exit(0)

3 个答案:

答案 0 :(得分:11)

获取Q作为输入

Quit = int(input('Press Q to Quit')

您要求Q作为输入,但仅接受int。所以取消int部分:

Quit = input('Press Q to Quit')

现在Quit将是用户输入的内容,所以让我们检查“Q”而不是True

if Quit == "Q":

而不是sys.exit(0),如果你在某个功能中,你可以用breakreturn来结束你的同时。

另外,对于只存储用户输入的变量,我不建议使用名称“Quit”,因为它最终会让人感到困惑。

请记住缩进在Python中很重要,所以它必须是:

if run == False:
    choice = input('Press Q to Quit')
    if choice == "Q":
        # break or return or..
        import sys
        sys.exit(0)

但这可能只是一个复制/粘贴错误。

缩进和语法

我修复了缩进并删除了一些无关的代码(因为你复制了外部循环和一些print语句)并得到了这个:

print('How old do you thing Fred the Chicken is?')
number = 17

run = True
while run:

    guess = int(input('Enter What You Think His Age Is....t'))

    if guess == number:
        print('Yes :D That is his age...')
        run = False
    elif guess < number:
        print('No, Guess a little higher...')
    elif guess > number:
        print('No, Guess a little lower....')

    if run == False:
        print('Game Over')
        choice = input('Press Q to Quit')
        if choice == 'q'
            break

这给了我一个语法错误:

  

blong @ ubuntu:〜$ python3 chicken.py
    文件“chicken.py”,第23行       如果选择=='q'
                     ^
  SyntaxError:语法无效

因此Python在if语句之后说错了。如果您查看其他if语句,您会注意到此语句最后缺少:,因此请将其更改为:

if choice == 'q':

因此,通过该更改程序运行,并且似乎可以执行您想要的操作。

一些建议

  • 您的说明是“按Q退出”,但实际上您只接受“q”退出。你可能想要同时接受这两个。 Python有一个operator called or,它接受​​两个真值(TrueFalse)并返回True,如果它们中的任何一个是True(它实际上超过TrueFalse>> True or True True >>> True or False True >>> False or True True >>> False or False False 之外的其他值,如果您有兴趣,请参阅文档。

    示例:

    if choice == "Q" or choice == "q":

    所以我们可以用q来询问Q或q。

    另一种选择是将字符串转换为小写,并仅使用if choice.lower() == "q":检查choice。如果.lower()为Q,则首先将其转换为q(使用number = random.randint(5, 20) ),然后进行比较。

  • 您的号码始终为17.Python有一个名为random.randint()的函数,它会为您提供一个随机数,这可能会让游戏更有趣。例如,这会使鸡的年龄在5到20之间(含):

    {{1}}

答案 1 :(得分:1)

有很多方法可以退出某些事情。对于循环,它使用break,对于函数,您可以使用return。但是,对于程序,如果您希望在解释完成之前退出程序(脚本结束),则有两种不同类型的exit()函数。有sys.exit()sys模块的一部分,exit()quit()是内置的。但是,sys.exit()适用于不在IDLE(python interactive)中的程序,而内置的exit()quit()函数适用于IDLE。

答案 2 :(得分:1)

您可以使用break语句来摆脱while循环:

while True:
    guess = int(input("...")

    # ...rest of your game's logic...

    # Allow breaking out of the loop
    choice = input("Enter Q to quit, or press return to continue")
    if choice.lower() == "q":
        break

这意味着解释器退出while循环,继续寻找更多要运行的命令,但是到达文件的末尾!然后Python将自动退出 - 无需调用sys.exit

(顺便说一下,你应该很少使用sys.exit,它实际上只是用于设置非零exit-status而不是来控制程序的工作方式,例如转义来自while循环)

最后,您发布的代码的主要问题是缩进 - 您需要在每行的开头放置适当数量的空格 - 您可以使用以下内容:

if run == False:
choice = input('Press Q to Quit')

必须是这样的:

if run == False:
    choice = input('Press Q to Quit')

whileforif之类的内容相同。

相关问题