我无法获得以下功能。我需要通过添加
进行验证else:
print("Type only 4, 6, or 12.")
但是在代码上执行此操作不起作用:
def dicegame():
#This defines the dicegame coding so I can call it later on
number=int(input("Hello. Enter either 4,6 or 12 to determine what sided dice you want to use "))
import random
if number=="4" or "6" or "12":
print("You have rolled a", number, "sided dice with the number", random.randint(1,number))
#These if loops direct the program to the correct coding down to what the user inputs (ie. 4, 6 or 12)
print("Would you like to roll again?")
answer=input("Enter either yes or no.")
if answer == ("yes"):
dicegame()
if answer == ("no"):
print("Ok, goodbye")
#Program prints "Would you like to roll again?
#Using input, user can reply to what the program prints, labelled with the variable 'answer'
dicegame()
一旦为我的验证添加了else循环,循环就会中断。
答案 0 :(得分:3)
我没有看到循环,但有一件事是错的:
而不是
if number=="4" or "6" or "12":
使用
if number in (4, 6, 12):
请注意,号码已经投放到int
答案 1 :(得分:2)
试
if number==4 or number==6 or number==12:
或
if number in [4, 6, 12]:
'6'永远是真的 - 所以你总是满足条件......
自写number=int(...)
以来,您还必须删除引号。