首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用system.exit

使用system.exit
EN

Stack Overflow用户
提问于 2019-10-21 06:33:57
回答 1查看 57关注 0票数 0

我现在正在尝试将sys.exit添加到我的代码中,但我希望它在退出之前显示打印消息。我能这么做吗?此外,在我的代码末尾,我想让用户选择退出或重新开始,但当他们在完成第一个游戏后键入quit时,它只是使用quit作为他们的名称。

代码语言:javascript
复制
import random
import sys

def system_exit(exitCode):
    sys.exit()

# uses randrange instead of randint for better results in Python 3.7 
# randrange stops just before the upper range, use (1, 11) for 1-10

num = random.randrange(1, 11)

name = input("What is your name? Or if you would rather quit, type quit at 
any time. " "\n")


i = name
while i != "quit":
    print('Hello', name,'!')

    while i.lower() == 'quit':
        print ("Sorry, you did not select a number in the range or 
selected to quit. Good bye.")
        system_exit(4)





    your_guess = input('Enter a number between 1 and 10.' '\n')
    if (your_guess.lower() == "quit"):
        print ("Sorry, you did not select a number in the range or 
selected to quit. Good bye.") #this isn't showing up before closing
    your_guess = int(your_guess)

# display the number guessed
    print("Your number is", your_guess)

    while num != your_guess:


                if your_guess < num:
                        print("Your guess is too low.")
                        your_guess = int(input("Guess another number from 
1 to 10: " '\n'))

                elif your_guess > num:
                        print("Your guess is too high")
                        your_guess = int(input("Guess another number from 
1 to 10: " '\n'))

                else:
                    print ("Sorry, you did not select a number in the 
range or selected to quit. Good bye.") #this isn't showing up before 
closing
                    system_exit(4)

    print("The correct number was", num)





print("***************************************************************")

    name = input("What is your name? Or if you would rather quit, type 
quit. ")


    num = random.randrange(1, 11)
print("Thank you for playing!") #this isn't showing up before closing
system_exit(0)
EN

回答 1

Stack Overflow用户

发布于 2019-10-21 07:55:39

替换以下代码行:

your_guess = int(input('Enter a number between 1 and 10.'))

代码如下:

代码语言:javascript
复制
your_guess = input('Enter a number between 1 and 10.')
if (your_guess.lower() == 'quit'):
    #print some break message 
your_guess = int(your_guess)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58477872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档