简体   繁体   中英

Python if one condition is met, print “…”

I am a student that is rather nee to learning programming languages, i decided to pick up Python and am really enjoying it so far. I might have a stupid question but i cant seem to find a solution, i have written a part of a program and the user inputs 3 values. if even one of those values breaks a statement it should print out the correct answer. This is what i have..

lengte = input("Wat is de lengte die u nodig hebt?")
breedte = input("Wat is de breedte die u nodig hebt?")
hoogte = input ("Wat is de hoogte die u nodig hebt?")
a = 140
aa = 100
aaa = 110
b = 220
bb = 115
bbb = 120
if(hoogte in range(a) and breedte in range(aa) and hoogte in range(aaa)):
    print "Type A"
if(hoogte in range(a,b) or breedte in range(aa,bb) or hoogte in range(aaa,bbb)):
    print "Type B"

BUT when i give in 170 for hoogte and 40, 40 for breedte and hoogte it still prints out "Type A" while in my eyes it should say "Type B" because the value exceeds aa but is lower as bb..

any help would be appreciated, sorry if this seems like a "noob" question.. but yeh i am pretty new to this.

May be you mis-type the first condition. You type it hoogte, but it may be lengte.

if(lengte in range(a) and breedte in range(aa) and hoogte in range(aaa)):
    print "Type A"
if(lengte in range(a,b) or breedte in range(aa,bb) or hoogte in range(aaa,bbb)):
    print "Type B"

The first conditional logic will only print "Type A" if all conditions are true, while the second will print "Type B" if at least one condition is true.

The problem is that you specify a,aa,aaa and so on as ranges, but you give no range. So instead use == operators to test whether your input is equal to any of the variables you have specified.

This worked for me.

lengte = input("Wat is de lengte die u nodig hebt?")
breedte = input("Wat is de breedte die u nodig hebt?")
hoogte = input ("Wat is de hoogte die u nodig hebt?")
a = 140
aa = 100
aaa = 110
b = 220
bb = 115
bbb = 120

if(hoogte ==a and breedte == aa and hoogte == aaa):
    print "Type A"
if(hoogte ==a,b or breedte == aa,bb or hoogte == aaa,bbb):
    print "Type B"

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