我是蟒蛇的初学者。我使用多个if条件编写了简单的程序,但这段代码中有一些问题。如果我的起重能力低于物品的重量,而我说不,我不能举起那件物品,它应该说“是的,你不能,.”而是说:“别这么懒,.”我不知道为什么,虽然我的逻辑是正确的,但我找不到任何错误。
Weights = {
"table":5, #kg
"sofa":9, #kg
"Cupboard":85, #kg
"Ladder":7, #kg
}
print("How much weight can you lift? ")
lifting_capability = input("Enter your lifting capability"+" ")
lifting_capability_int = int(lifting_capability)
for Objects in Weights.keys():
Answer = input("Can you lift" + " " + Objects + "? " + "Y/N ")
Answer_Adjusted = Answer.lower()
for Wt in Weights.values():
if Answer_Adjusted == "y" and Wt > lifting_capability_int:
print("mmmmm...., i don't think you can lift this much")
break
elif Answer_Adjusted == "n" and Wt > lifting_capability_int:
print("Yes you can't, you know how to keep your backbone in place")
break
elif Answer_Adjusted == "y" and Wt <= lifting_capability_int:
print("Yes, obviously you can, its so light for you")
break
elif Answer_Adjusted == "n" and Wt <= lifting_capability_int:
print("Don't be so lazy, its lighter than you think")
break
else:
print("you think you can, but watch yor feet")发布于 2020-04-21 07:48:43
你在字典里翻了两遍。用重写代码一个for-循环,如下所示:
for object, weight in Weights.items():https://stackoverflow.com/questions/61338334
复制相似问题