繁体   English   中英

制作简单的骰子滚动游戏时,Python中的OR语句出现问题

[英]Issue with OR statement in Python while making simple dice rolling game

我是Python编程的新手,并且此程序中的or语句有问题。 当我在PyCharm中运行它并输入'roll'时,它可以工作。 但是当我使用“是”时,似乎什么也没做。 而input()==(“ roll”)或input()==(“ yes”):这是问题所在。 我想知道我在做什么错。 例如,当我仅使用一个命令roll时,它可以完美运行多次,就像我想运行它一样。

# this is a dice rolling game
import random

min = 1
max = 6
def roll_dice():
    x = random.randint(min, max)
    print(x)

print("Would you like to roll the dice?")


while input() == ("roll") or input() == ("yes"):
    roll_dice()
    print("Would you like to roll the dice again ?")

不要两次调用input() 调用一次并使用in测试集合in是否存在:

while input() in {'roll', 'yes'}:
    ...

如果您使用的是python2,请记住您将需要raw_input ,而不是input

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM