如何基于差异返回字符串,无论是+还是Python?

2024-04-28 20:05:33 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我想让IDE(即Pydroid)根据变量的存储值与给定范围内的其他值之间的差异返回不同的字符串,我该怎么做?我知道我可以用集合运算来做,但是还有其他方法吗

例如:让我们说游戏“热或冷”。如果要求您猜一个介于0和10之间的数字(因此,range(1,10)),并且中奖号码是4'W=4',我希望输入3或5的字符串“几乎”打印出来,输入2或6的字符串“热”,输入1或7的字符串“热”,输入8的字符串应返回“冷”,然后 “不能再往前走了”为9

我试过:

print("Can you guess the winning number between 0 and 10?")
A=int(input('Pleasant picks: '))
if A not in range(1,10):
        print("That is not an option...")
elif A == 4:
print("You win!")
else:
    if A is int(3) or int(5):
         print("Almost...")
    elif A is int(2) or int(6):
         print("You're hot")
    elif A is int(1) or int(7):
         print("Warm, but not as hot as your mom")
    elif A is int(8):
         print("cold...")
    elif A is int(9):
        print("Couldn't be further")
    else:
        print(i)

它只返回“几乎”的所有选择,但4和超出范围的工作权利

在返回尝试次数的同时,如何循环此操作


Tags: or字符串youifisasnotrange
1条回答
网友
1楼 · 发布于 2024-04-28 20:05:33

你可以试试这个

def case(c):
    options = {
        4: "you win",
        3: "Almost",
        5: "Almost",
        2: "You're hot",
        6: "You're hot",
        1: "Warm, but not as hot as your mom",
        7: "Warm, but not as hot as your mom",
        8: "cold...",
        9: "Couldnt be further"
    }
    
    return options.get(c,"That is not an option")

print("Can you guess the winning number between 0 and 10?")
count = 0
while True:
    A=int(input('Pleasant picks: '))
    count+=1
    print(case(A))

    

相关问题 更多 >