[英]How to stop script if user inputs certain characters - 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()
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()
这应该解决它!
尝试:
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()
阻塞直到使字符终止:
import sys
sys.stdin.read(1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.