-2

I need to make sure that all the conditions are met, with a special case. The student is still deemed 'eligible' even if either the 'before2010' or 'gerequirements' aren't met.

However, I can't get the program to work properly. I want to be able to type 'y', 'yes', 'n', or 'no' for the answers to the yes/no questions, but it comes off as an error, because I didn't assign 'y' apparently.

def main():
    credits = int(input("Enter the total number of credits completed: "))
    udcredits = int(input("Enter the number of upper-division credits completed: "))
    localcredits = int(input("Enter the number of local credits completed: "))
    mrequirements = input("Have you completed all major requirements? ")
    before2010 = eval(input("In what year did you matriculate? "))
    gerequirements = input("Are your general education requirements done? ")

    if before2010 < 2010 and credits >= 120 and udcredits >= 40 and localcredits >= 30 and mrequirements[0] == y:
        print("eligible")
    else:
        print("ineligible")

    if gerequirements[0] == y and credits >= 120 and udcredits >= 40 and localcredits >= 30 and mrequirements[0] == y:
        print("eligible")
    else:
        print("ineligible")
main()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

2 Answers2

3
gerequirements[0] == y

This line will not compile. If you're trying to match the character y you have to wrap it in quotes to denote a string. Without quotes, Python expects y to be a variable.

So the expression becomes:

gerequirements[0] == 'y'

As the commenters have mentioned, there's several other problems with your code:

Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244
1

Your full code would be:

def main():
    credits = int(input("Enter the total number of credits completed: "))
    udcredits = int(input("Enter the number of upper-division credits completed: "))
    localcredits = int(input("Enter the number of local credits completed: "))
    mrequirements = input("Have you completed all major requirements? ")
    before2010 = int(input("In what year did you matriculate? "))
    gerequirements = input("Are your general education requirements done? ")

    if before2010 < 2010 and credits >= 120 and udcredits >= 40 and localcredits >= 30 and mrequirements[0].lower() == 'y':
        print("eligible")
    else:
        print("ineligible")

    if gerequirements[0].lower() == 'y' and credits >= 120 and udcredits >= 40 and localcredits >= 30 and mrequirements[0].lower() == 'y':
        print("eligible")
    else:
        print("ineligible")

main()

CHANGES

  • Line 6, changed eval() to int() safer this way, better practice

  • Line 9 & 14 added .lower() to mrequirements[0] and gerequirements[0] so that even if the user typed a capital Y the test woud still pass.

  • Line 9 & 14 Added quotes to "y" since it's saved as a string from the input() function in Python. Otherwise the if statement wouldn't return true

It should run fine now.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dor-Ron
  • 1,787
  • 3
  • 15
  • 27