如果用户输入某些字符,如何停止脚本 - PYTHON

时间:2016-10-13 16:44:18

标签: python

我是编程的初学者,我正在制作掷骰子类型的游戏。我想创建一个命令,如果用户键入Exit,整个脚本将打印再见并停止。我试过像:

commandLine = "Exit"

if commandLine == "Exit".
    print ("Bye!")
    quit()

但它根本不起作用。脚本会继续下去。 这将是我的剧本:

import random
from random import randint
import re


print ("Welcome to Roll the Dice")

roll = input("Do you want to Roll the Dice? \n Yes / No \n")

if roll == "Yes".lower():
    print("Nice , let`s do it!")

elif roll == "No".lower():
    print("Too bad! :( See you later!")
    quit()

dice = input("Press 'x' to roll the dice!\n")


if dice == "X".lower():
print(random.randint(1,9))

again = input("Sweet ! Would you like to try again?\n")
while again == "Yes".lower():
    dice = input("Press 'x' to roll the dice!\n")

    if dice == "X".lower():
        print(random.randint(1, 9))

    if again == "No".lower():
        print("See you later!")
        quit()

3 个答案:

答案 0 :(得分:0)

尝试:

while again == "yes":
    dice = input("Press 'x' to roll the dice!\n")

    if dice == "x":
        print(random.randint(1, 9))

    if dice == "no":
        print("See you later!")
        quit()

答案 1 :(得分:0)

again = input("Sweet ! Would you like to try again?\n")

while again == "Yes".lower():
    dice = input("Press 'x' to roll the dice!\n")

    if dice == "X".lower():
        print(random.randint(1, 9))

    again = input("Sweet ! Would you like to try again?\n")

if again == "No".lower():
    print("See you later!")
    quit()

这应该解决它!

答案 2 :(得分:0)

封锁直到角色终止:

import sys
sys.stdin.read(1)