gpt4 book ai didi

python - 将我的代码放在一行中

转载 作者:太空宇宙 更新时间:2023-11-04 09:05:25 25 4
gpt4 key购买 nike

我无法使以下功能正常运行。我需要通过添加

进行验证
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 循环,循环就会中断。

最佳答案

我没有看到循环,但有一件事是错误的:

代替

if number=="4" or "6" or "12":

使用

if number in (4, 6, 12):

请注意,数字已经转换为 int

关于python - 将我的代码放在一行中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21134974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com