Python中IF语句判断异常问题求助(新手)
Python中IF判断失效的问题解决
我不确定Python里的IF语句为啥判断不对。我正在学Python,怀疑问题出在字符串比较上。我懂SQL但刚接触Python,测试时输入1个家庭成员、200000的家庭收入,本应显示“Moderate Income”,但所有打印语句都跑了。代码如下:
#capture number of people in the household Householdtotal = input("Please enter the total amount of people in your household:") time.sleep(.5) print("Thank you") print("") #capture total household income HouseholdtotalIncome = input("Please enter your combined household income:") time.sleep(.5) print("Thank you") print("") if Householdtotal < '1': print("You must enter a value greater than 1") print("") #Extremely Low if HouseholdtotalIncome <= "31050" and Householdtotal <= "1" : print("Your Income is categorized as Extremely Low Income (30% or lower)") if HouseholdtotalIncome <= '35500' and Householdtotal <= '2' : print("Your Income is categorized as Extremely Low Income (30% or lower)") if HouseholdtotalIncome <= '44350' and Householdtotal == '4' : print("Your Income is categorized as Extremely Low Income (30% or lower)") if HouseholdtotalIncome <= '44350' and Householdtotal > '4' : print("Your Income is categorized as Extremely Low Income (30% or lower)") #Very Low Income if HouseholdtotalIncome <= '51800' and Householdtotal == "1" : print("Your Income is categorized as Very Low Income (50% or lower)") if HouseholdtotalIncome <= '51800' and Householdtotal == '2' : print("Your Income is categorized as Very Low Income (50% or lower)") if HouseholdtotalIncome <= '73950' and Householdtotal == '4' : print("Your Income is categorized as Very Low Income (50% or lower)") if HouseholdtotalIncome <= '73950' and Householdtotal > '4' : print("Your Income is categorized as Very Low Income (50% or lower)") #Low Income if HouseholdtotalIncome <= '78550' and Householdtotal == "1": print("Your Income is categorized as Low Income (80% or lower)") if HouseholdtotalIncome <= '89750' and Householdtotal == '2' : print("Your Income is categorized as Low Income (80% or lower)") if HouseholdtotalIncome <= '112150' and Householdtotal == '4' : print("Your Income is categorized as Low Income (80% or lower)") if HouseholdtotalIncome <= '112150' and Householdtotal > '4' : print("Your Income is categorized as Low Income (80% or lower)") #Median Income if HouseholdtotalIncome <= '103550' and Householdtotal == "1" : print("Your Income is categorized as Median Income (100% or lower)") if HouseholdtotalIncome <= '118300' and Householdtotal == '2' : print("Your Income is categorized as Median Income (100% or lower)") if HouseholdtotalIncome <= '147900' and Householdtotal == '4' : print("Your Income is categorized as Median Income (100% or lower)") if HouseholdtotalIncome <= '147900' and Householdtotal > '4' : print("Your Income is categorized as Median Income (100% or lower)") #Moderate Income if HouseholdtotalIncome <= '124250' and Householdtotal == "1" : print("Your Income is categorized as Moderate Income (120% or lower)") if HouseholdtotalIncome <= '142000' and Householdtotal == '2' : print("Your Income is categorized as Moderate Income (120% or lower)") if HouseholdtotalIncome <= '177500' and Householdtotal == '4' : print("Your Income is categorized as Moderate Income (120% or lower)") if HouseholdtotalIncome <= '177500' and Householdtotal > '4' : print("Your Income is categorized as Moderate Income (120% or lower)") print("") time.sleep(.5) print("Good Bye")
问题根源
你猜的没错,问题就出在字符串比较上:
input()函数返回的是字符串类型,你直接拿字符串和字符串形式的数字做大小比较,Python会用字典序判断,而不是数值大小。比如字符串"200000"和"31050"比较时,第一个字符"2"比"3"小,所以"200000" <= "31050"会被判定为True,这就导致所有符合家庭人数条件的if语句都触发执行。- 另外,所有if语句都是独立判断,没有用
elif串联,就算前面的条件满足,后面的也会继续检查,不过这不是这次问题的核心原因。
修复方案
- 转换输入类型:把输入的字符串转成整数,这样才能做正确的数值比较:
Householdtotal = int(input("Please enter the total amount of people in your household:")) HouseholdtotalIncome = int(input("Please enter your combined household income:")) - 修改判断条件:把所有条件里的字符串数字改成整数,比如
<= 31050而不是<= "31050" - 优化判断逻辑(可选):用
elif替代独立的if,避免重复判断,让逻辑更清晰高效。比如按收入从高到低或者从低到高排序,用elif串联,一旦匹配就不再检查后续条件。
修复后的示例代码(关键部分修改)
import time #capture number of people in the household Householdtotal = int(input("Please enter the total amount of people in your household:")) time.sleep(.5) print("Thank you") print("") #capture total household income HouseholdtotalIncome = int(input("Please enter your combined household income:")) time.sleep(.5) print("Thank you") print("") if Householdtotal < 1: print("You must enter a value greater than 1") print("") # 按收入从高到低判断,用elif避免重复执行 #Moderate Income if (HouseholdtotalIncome <= 124250 and Householdtotal == 1) or \ (HouseholdtotalIncome <= 142000 and Householdtotal == 2) or \ (HouseholdtotalIncome <= 177500 and Householdtotal >=4): print("Your Income is categorized as Moderate Income (120% or lower)") #Median Income elif (HouseholdtotalIncome <= 103550 and Householdtotal == 1) or \ (HouseholdtotalIncome <= 118300 and Householdtotal == 2) or \ (HouseholdtotalIncome <= 147900 and Householdtotal >=4): print("Your Income is categorized as Median Income (100% or lower)") #Low Income elif (HouseholdtotalIncome <= 78550 and Householdtotal == 1) or \ (HouseholdtotalIncome <= 89750 and Householdtotal == 2) or \ (HouseholdtotalIncome <= 112150 and Householdtotal >=4): print("Your Income is categorized as Low Income (80% or lower)") #Very Low Income elif (HouseholdtotalIncome <= 51800 and Householdtotal in (1,2)) or \ (HouseholdtotalIncome <= 73950 and Householdtotal >=4): print("Your Income is categorized as Very Low Income (50% or lower)") #Extremely Low elif (HouseholdtotalIncome <= 31050 and Householdtotal ==1) or \ (HouseholdtotalIncome <=35500 and Householdtotal ==2) or \ (HouseholdtotalIncome <=44350 and Householdtotal >=4): print("Your Income is categorized as Extremely Low Income (30% or lower)") # 超过Moderate的情况 else: print("Your Income is above Moderate Income level") print("") time.sleep(.5) print("Good Bye")
内容的提问来源于stack exchange,提问作者user3772443
相关产品推荐
相关产品推荐

