简体   繁体   English

python 中的一系列值返回了错误的字符串

[英]Wrong string is being returned on a range of values in python

I'm new to coding and have been trying to figure out why my code isn't working right.我是编码新手,一直试图弄清楚为什么我的代码不能正常工作。

credit_score = int(input('What is your credit score? '))
income = int(input('What is your annual income? '))
good_credit = credit_score>= 400
k = (20*50)

high_income = income>=250000
medium_income = high_income>income>= 70000
low_income = income<70000

if high_income and good_credit or medium_income and good_credit:
  loan = True
  if loan:
    loan = 'approved'
else:
  loan = 'not approved'
  

print(f'Your loan is {loan}')

The medium income variable falls between 70,000 and 250,000.中等收入变量介于 70,000 和 250,000 之间。 What my code is supposed to do is return the string 'approved' when the inputted credit value is greater than or equal to 400 and the income falls between 70,000 and 250,000.我的代码应该做的是当输入的信用值大于或等于 400 并且收入在 70,000 和 250,000 之间时返回字符串“已批准”。

But every time I run it and put in the medium-income values it consistently returns 'not approved', which should only be returned if a low-income value is entered with a high credit value.但是每次我运行它并输入中等收入值时,它始终返回“未批准”,只有在输入具有高信用值的低收入值时才应返回。 However, if I enter a high credit value and a high-income value the code works just fine and returns 'approved'.但是,如果我输入一个高信用值和一个高收入值,代码就可以正常工作并返回“已批准”。

What is your credit score? 900
What is your annual income? 90000
Your loan is not approved?

With these values, it should return 'approved'使用这些值,它应该返回 'approved'

Any help is appreciated.任何帮助表示赞赏。

In this line在这一行

medium_income = high_income>income>= 70000

Did you mean to put 250000?你的意思是放250000? Because of that error, medium_income will be False for 90K also.由于该错误,对于 90K,medium_income 也将为 False。

Also please get in habit of not using magic numbers.另外请养成不使用幻数的习惯。

You should add some brackets to your if statement for readability and to ensure it does what you intended.您应该在 if 语句中添加一些括号以提高可读性并确保它符合您的预期。 As it stands, or will be evaluated first, so your conditions will be就目前而言, or将首先评估,因此您的条件将是

high_income and (good_credit or medium_income) and good_credit

which isn't what you intended it to be.这不是你想要的。

Your calculation of medium_income also appears to be incorrect.您对medium_income的计算似乎也不正确。 It appears that you intended it to be看来您打算这样做

medium_income = 250000>income>= 70000

You are incorrectly using the high_income object which is a boolean to compare for the mid_income .您错误地使用了high_income object 这是一个 boolean 来比较mid_income Also, consider adding brackets around the logical expression.此外,请考虑在逻辑表达式周围添加括号。

Try this working code:试试这个工作代码:

credit_score = int(input('What is your credit score? '))
income = int(input('What is your annual income? '))
good_credit = credit_score>= 400
k = (20*50)

high_income = income >= 250000
medium_income = 250000 > income >= 70000
low_income = income < 70000
if (high_income and good_credit) or (medium_income and good_credit):
  loan = 'approved'
else:
  loan = 'not approved'

print(f'Your loan is {loan}')

Here:这里:

high_income = income>=250000
medium_income = high_income>income>= 70000
low_income = income<70000

You create high_income which is True or False depending on user input, then you use this in comparison您根据用户输入创建TrueFalsehigh_income ,然后使用它进行比较

high_income>income>= 70000

which mean depending on user input is True>income>= 70000 or False>income>= 70000 .这意味着取决于用户输入的是True>income>= 70000False>income>= 70000 True and False when used in numeric comparison does act as 1 and 0 thus they become 1>income>= 70000 or 0>income>= 70000 therefore medium_income is always False . TrueFalse在数字比较中使用时确实充当10 ,因此它们变为1>income>= 700000>income>= 70000因此medium_income始终为False

You are treating high_income as a threshold against which income can be compared, not the boolean value that it is.您将high_income视为可以比较income的阈值,而不是 boolean 值。 Since high_income == False , your definition of medium_income becomes由于high_income == False ,您对medium_income的定义变为

medium_income = False > income >= 70000

This is legal, because bool is a subclass of int and False == 0 .这是合法的,因为boolintFalse == 0的子类。 As a result, medium_income is False , because 0 is not greater than income .结果, medium_incomeFalse ,因为0不大于income

Instead, you want相反,你想要

medium_income = not high_income and medium_income >= 70000

If you want to store the thresholds, I suggest something like如果你想存储阈值,我建议像

 high_income = 250_000
 medium_income = 70_000

 has_high_income = income >= high_income
 has_medium_income = high_income > income >= medium_income
 has_low_income = medium_income > income

 has_good_credit = credit_score >= 400

# if has_good_credit and (has_high_income or has_medium_income):
if has_high_income and has good_credit or has_medium_income and has_good_credit:
    loan = 'approved'
else:
    loan = 'not approved'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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