如何根据差异返回字符串,无论是 + 还是 - 与 Python?

问题描述

如果我希望 IDE(即 Pydroid)根据变量的存储值与给定范围的其他值之间的差异返回不同的字符串,我该怎么做?我知道我可以通过 Set 操作来实现,但还有其他方法吗?

例如:假设游戏“热或冷”。如果要求您猜测 0 到 10 之间的数字(因此,range(1,10)),并且获胜数字是 4 'W=4',我希望为输入 3 或 5 打印出字符串“Almost”, 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 和超出范围的工作正常......

我如何在循环的同时返回尝试次数

解决方法

你可以试试这个

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,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))