0

I know my code is a little messy, I am going to clean it up once I can figure out why the if statements to check state[0] is only printing the first statement. I tried moving it around the codeblocks and indentations but still only getting the first if statement to print. Any advice/help would be great. Just came back to python, been learning java/SQL, this class is going back to python, so I'm a little off at then moment. Thanks

ballotLoop = input("Hey there, are you here for the voter ballot? Exit to stop.\n")

while ballotLoop != "exit":

lastName = input("Please enter your last name.\n")

firstName = input("Please enter your first name.\n")

age = int(input("Please enter your age.\n"))

usCit = input("Are you a United States Citizen?\n")



if usCit == "yes":
    if age >= 18 and age < 80:
        print(firstName, lastName +". Great you are over 18 and a US citizen\nYou may proceed with the form.\n")
       
    elif age > 0 and age < 18:
        print("I am sorry you are not old enough to complete this voter form.")

    elif age > 80:
         print("Error age does not make sense.\n")
         
    elif age < -1:
        print("Error age does not make sense.\n")

    state = input("What state do you currently reside in?\n")

    zipcode = input("What is your current zipcode\n")
        
    print("Alright so to make sure everything is correct we have the following.\n"\
          + firstName, lastName + ", you are "+ str(age) + " years old.\nYou marked " + usCit + \
          " for being a US citizen.\nYou currently reside in " + state + " and your zipcode is " + zipcode +"\n")

    if state[0]  == "a" or "c":
             print("You should recieve your ballot in 1 week")
        
    elif state[0] == "t" or "d":
             print("You should recieve your ballot in 4 weeks")
            
    elif state[0] == "m" or "l":
             print("You should recieve your ballot in 2 weeks")

    
elif usCit == "no":
    print("I am sorry but you may not complete this form, I am going to have to ask you to leave.")
    

ballotLoop = input("Would you like to fill out another ballot? Exit to stop.\n")

if ballotLoop == "exit":
    print("Thank you for you vote today, every little bit makes a difference!!")
  • 1
    Your conditions should be written as `if state[0] == "a" or state[0] == "c"`. Otherwise, it checks whether `"c"` is true, which it is (non-empty string). – Timur Shtatland May 20 '22 at 20:16
  • 3
    Does this answer your question? ["or" conditional in Python troubles](https://stackoverflow.com/questions/17375793/or-conditional-in-python-troubles) – Timur Shtatland May 20 '22 at 20:18

1 Answers1

0

Solution
state[0] == "a" or state[0] == "c"
instead of
state[0] == "a" or "c"
and so on for next two logics.

Explanation

There are two parts per and / or keywords in python. The left part (any boolean expression) to the keyword is compared with the right part (another boolean expression). And you know that boolean expression is either True or False. Keeping these in mind let's break down your if statement.

if state[0]  == "a" or "c":
    print('...')

Here, the boolean expressions are:

  1. state[0] == "a" Possible results: Either True or False

  2. "c" which is always True in python. Check this.

So under the hood your logical operation in the if statement is
state[0] == "a" or True
Which will always return True, satisfying the first if statement everytime. Long story short, explicitly write:
state[0] == "a" or state[0] == "c"