Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Whats Wrong!?
#1
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.
Reply
#2
== is equality, = is assignment. fix it on lines 5 and 7
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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
Reply
#4
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,553 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Whats wrong with the elif? inunanimous93 3 2,458 Nov-30-2020, 03:58 AM
Last Post: deanhystad
  Can u see this code and tell whats wrong with it? AhmadKamal 14 5,321 Apr-29-2020, 11:09 AM
Last Post: jefsummers
  python gives wrong string length and wrong character thienson30 2 3,000 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  Whats a good design/approach? hshivaraj 1 1,773 Sep-16-2019, 01:44 AM
Last Post: snippsat
  elevator simulator...whats the wrong at this code? tasos710 5 5,914 Jun-11-2019, 01:38 AM
Last Post: micseydel
  whats the difference between sys.exit() and break? mitmit293 1 4,113 Jan-27-2019, 09:46 PM
Last Post: ichabod801
  Whats wrong with this code? student1 1 2,400 May-18-2018, 04:19 PM
Last Post: skorpius_
  I have no clue whats wrong... Jack_03 1 2,826 Sep-28-2017, 05:36 PM
Last Post: nilamo
  Whats wrong with line 12? brimalm 2 3,189 Sep-11-2017, 12:23 PM
Last Post: brimalm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020