Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

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. 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.

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'

Any help is appreciated.

question from:https://stackoverflow.com/questions/65921485/wrong-string-is-being-returned-on-a-range-of-values-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

high_income>income>= 70000

which mean depending on user input is True>income>= 70000 or False>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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...