0

我正在尝试使用 while 循环和 if 来创建骰子游戏。我已经成功完成了这项工作,但是我正在尝试弄清楚如何对游戏进行编程,以便如果未输入数字 4,6 或 12,它将表明选择无效并再次询问 diceChoice。有人可以帮忙吗?

到目前为止,我已经...

rollAgain = "Yes" or "yes" or "y"

while rollAgain == "Yes" or "yes" or "y":
    diceChoice = input ("Which dice would you like to roll; 4 sided, 6, sided or 12 sided?")
    if diceChoice == "4":
        import random 
        print("You rolled a ", random.randint(1,4)) 
    if diceChoice == "6":
        import random
        print("You rolled a ", random.randint(1,6))
    if diceChoice == "12":
        import random
        print("You rolled a ", random.randint(1,12))

    rollAgain = input ("Roll Again?") 
print ("Thank you for playing")
4

4 回答 4

3

修复 While 循环,整理所有重复。将导入语句移到顶部。结构化以允许 rollAgain 和 diceChoice 有更多选项。

import random

rollAgain = "Yes"

while rollAgain in ["Yes" , "yes", "y"]:
    diceChoice = input ("Which dice would you like to roll; 4 sided, 6, sided or 12 sided?")
    if diceChoice in ["4","6","12"]:
        print("You rolled a ",random.randint(1,int(diceChoice)))
    else:
        print "Please input 4,6, or 12."
    rollAgain = input ("Roll Again?") 

print ("Thank you for playing")

做这样的任务:

rollAgain = "Yes" or "yes" or "y"

是不必要的 - 只会输入第一个值。为这个变量选择一个;您只需要一个即可。

这种分配在这里也不起作用:

while rollAgain == "Yes" or "yes" or "y":

它将再次只检查第一个值。您要么必须像其他海报一样将其拆分,要么使用不同的数据结构将它们全部合并,就像上面代码中的列表一样。

于 2013-08-01T17:02:27.783 回答
1

您应该只在顶部随机导入一次

import random  #put this as the first line

您的 rollAgain 声明应仅将其设置为一个值

rollAgain = "yes"  # the or statements were not necessary

rollAgain ==您在随后的条件中忘记做,这是一种更简单的方法

while rollAgain.lower().startswith("y"):  #makes sure it starts with y or Y

要执行无效的输入语句,您可以使用elif:andelse:语句来保持简单

if diceChoice == "4":
    print("You rolled a ", random.randint(1,4)) 
elif diceChoice == "6":
    print("You rolled a ", random.randint(1,6))
elif diceChoice == "12":
    print("You rolled a ", random.randint(1,12))
else:
    print("Invalid Input, please enter either 4, 6, or 12")

您的旧 while 循环永远不会退出,因为您基本上是在说这个

while rollAgain == "Yes" or True or True  #because non-empty strings are treated as True

编辑因为您询问了in陈述,这里是一个简短的例子

>>>5 in [1,2,3,4]
False
>>>5 in [1,2,3,4,5]
True

in语句与contains()其他语言中的 a 类似,它检查变量是否在列表中

但是,由于5不在[1,2,3,4]它返回的列表中,所以它在列表中,所以它返回False
5[1,2,3,4,5]True

您可以在代码中的多个位置使用它,特别是如果您想确保变量在一组选项中。我不推荐它为您保持简单。

于 2013-08-01T17:01:51.973 回答
0
diceChoice = None
while diceChoice not in ["4","12","6"]:
    diceChoice = input("enter choice of dice(4,6,12)")
print "You picked %d"%diceChoice
于 2013-08-01T17:04:03.983 回答
0

只是我的看法:

# If you are only using one function from random
# then it seems cleaner to just import that name
from random import randint
while True:
    choice = int(input("Which dice would you like to roll; 4 sided, 6, sided or 12 sided?\n:"))
    # Using sets for 'in' comparisons is faster
    if choice in {4, 6, 12}:
        print("You rolled a", randint(1, choice))
    else:
        print("Please input 4, 6, or 12.")
    # Make the input lowercase so that it will accept anything that can be
    # interpreted as "yes".
    if input("Roll Again?\n:").lower() not in {"yes", "y"}:
        # End the game by breaking the loop
        break
# You should use an input at the end to keep the window open to see the results
input("Thank you for playing!")
于 2013-08-01T17:28:39.410 回答