如何解决如何根据差异返回字符串,无论是 + 还是 - 与 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))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。