Python Forum

Full Version: Whats Wrong!?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I am new to programming. I need help in understanding what i am doing wrong or what is the error in this code.
Basically the program is an eligibility checker *practice purposes, following a textbook*. user will enter their information age, if they have id and salary. depending on their inputs the code will generate a message stating if they are eligible.

print("Welcome to our online elegibility checker!")
age = int(input("Please enter your age:"))
have_id = input("Do you have valid id: [Y/N]:")
if have_id.lower()=="y":
    have_id == True
else:
    have_id == False
salary = int(input("What is your annual salary:$"))
if age <= 35:
    print("The age is right")
    if have_id == True:
        print("You have a valid license")
        if salary >= 35000:
            print("Perfect you are eligible!")
        else:
            print("I am sorry, but you are below our minimum requirements.")
    else:
        print("Sorry but you need to have a valid license.")
else:
    print("You are above the age limit!")
Every time i execute the program, i am able to input the information, however for the result for some reason the program is skipping the if salary >= 35000: line and also for have_id printing out the else statement, regardless of the validity of any of the information. Below are two examples.

1st example with all valid answers, which should print out the following:
The age is right
You have a valid license
Perfect you are elegible

However this is what prints out:


C:\Users\rajib\PycharmProjects\Helloworld\venv\Scripts\python.exe C:/Users/rajib/PycharmProjects/Helloworld/app.py
Welcome to our online elegibility checker!
Please enter your age:29
Do you have valid id: [Y/N]:y
What is your annual salary:$40000
The age is right
Sorry but you need to have a valid license.

Process finished with exit code 0

[b]2nd scenario:
age and license is valid but not salary[:
This is what prints out:/b]

C:\Users\rajib\PycharmProjects\Helloworld\venv\Scripts\python.exe C:/Users/rajib/PycharmProjects/Helloworld/app.py
Welcome to our online elegibility checker!
Please enter your age:29
Do you have valid id: [Y/N]:y
What is your annual salary:$400
The age is right
Sorry but you need to have a valid license.

Process finished with exit code 0

Again as i said, i am very new to programming and i am following a book, however the book is not that detailed, and i have put the code in as exactly as the book has. So please, can you help me understand what my mistake is.
== is equality, = is assignment. fix it on lines 5 and 7
Nested if's are a thing to avoid. For a problem like this it often works best to test for all the bad and if you get past that assume the input is good.
if not too_young < age < too_old:
    print('We don't like babies or or old people')
elif salary < 35,000:
    print('Get a decent job!')
elif have_id.lower() != 'y':
    print('another demeaning remark')
Better yet, don't ask further questions if they are already ineligible.
def eligibility():
    if input('Do you have a valid license? ').lower() != 'y':
         print('Sorry, a valid license is required.')
         return False
    if int(input('What is your age? ')) > 35:
         print('Sorry, we only accept applicants 35 or younger')
         return False
    # more tests
    print('Your are eligible')
    return True
(May-13-2020, 07:46 PM)buran Wrote: [ -> ]== is equality, = is assignment. fix it on lines 5 and 7
thank you so much it worked!!

(May-13-2020, 07:56 PM)deanhystad Wrote: [ -> ]Nested if's are a thing to avoid. For a problem like this it often works best to test for all the bad and if you get past that assume the input is good.
if not too_young < age < too_old:
    print('We don't like babies or or old people')
elif salary < 35,000:
    print('Get a decent job!')
elif have_id.lower() != 'y':
    print('another demeaning remark')
Better yet, don't ask further questions if they are already ineligible.
def eligibility():
    if input('Do you have a valid license? ').lower() != 'y':
         print('Sorry, a valid license is required.')
         return False
    if int(input('What is your age? ')) > 35:
         print('Sorry, we only accept applicants 35 or younger')
         return False
    # more tests
    print('Your are eligible')
    return True
Thank you for the advise! Really appreciate it!