简体   繁体   中英

checking condition gives wrong result

I'm beginner in python. I made simple program using multiple if conditions but there's some issue in this code. If my lifting capability is lower than item's weight and i said no i can't lift that item, it should say "yes you can't,...." but it rather say: "don't be so lazy,..." I don't know why although my logic is correct, i couldn't find any bug.

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

you are looping twice through your dictionary. Rewrite your code with one single for-loop like this:

for object, weight in Weights.items():

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM