【问题标题】:How to properly quit a program in python如何在 python 中正确退出程序
【发布时间】:2022-11-14 09:11:49
【问题描述】:

我是一名中学生,我开始学习 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)

【问题讨论】:

  • 如果您愿意,您可以使用number = random.randint(0,10),这样它就不会总是每场比赛都设置为 17。请记住在文件顶部添加import random。阅读更多here

标签: python string python-3.x quit


【解决方案1】:

获取Q 作为输入

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

您要求Q 作为输入,但只接受int。所以去掉int 部分:

Quit = input('Press Q to Quit')

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

if Quit == "Q":

如果您在一个函数中,您可以使用breakreturn 来结束您的 while 查找,而不是 sys.exit(0)

此外,我不建议将仅存储用户输入的变量命名为“Quit”,因为它最终会造成混淆。

请记住,缩进在 Python 中很重要,因此它需要:

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

不过,这可能只是一个复制/粘贴错误。

缩进和语法

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

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(除了True 和@987654348 之外的值实际上还不止于此) @,如果您有兴趣,请参阅文档)。

    例子:

    >> 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 是Q,它会先将其转换为q(使用.lower()),然后进行比较。

  • 你的数字总是 17。Python 有一个名为random.randint() 的函数,它会给你一个随机数,这可能会使游戏更有趣。例如,这将使鸡的年龄在 5 到 20 岁(含)之间:

    number = random.randint(5, 20)
    

【讨论】:

  • 嗯.....我尝试了5种不同的方法,结果语法无效......一定有一些间距错误......谢谢你的帮助。你能检查我当前的代码吗?生病编辑我当前的帖子,以便您可以看到修复...
  • @user1766994 您当前的帖子没有正确缩进。您需要在每个while:if:(也包括classdef)之后缩进四个空格。请使用正确的缩进更新它,并包括整个错误消息。记住,在 Python 缩进很重要.
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    您可以使用 break 语句跳出 @9​​87654324@ 循环:

    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等相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-22
      • 2018-02-09
      • 2012-10-10
      相关资源
      最近更新 更多