[SOLVED] Semantic Error with If ,elif, and else statements

Issue

This Content is from Stack Overflow. Question asked by Michael Jordan

Hello to anyone this message may reach. I am having issues with a semantic error that I just cant quite figure out. I am very new to coding and I am trying to learn python 3. I am trying to create a code that lets me know if I have to work on a particular day of the week. I have done this by using if statements but I cannot get the code to go to the else or elif for some reason. Any and all answers will be helpful. Thanks in advance.

week_day_1 = "Monday"
week_day_2 = "Tuesday"
week_day_3 = "Wednesday"
week_day_4 = "Thursday"
half_week_day = "Friday"
weekend_day_1 = "Saturday"
weekend_day_2 = "Sunday"
week_days = week_day_1, week_day_2, week_day_3, week_day_4
weekend_days = weekend_day_1, weekend_day_2
weekend_days = True
week_days = True
half_week_day = True
input("What day of the week is it?") #the input allows the user to insert the day of the week.
if week_days:
    print("You must go to work.")
elif weekend_days:
    print("It is the weekend. Enjoy your time off.")
elif half_week_day:
    print("You must go to work, but your weekend start once you clock out.")
else:
    print("Please enter day of the week.")**

No matter what day I input the code returns back “You must go to work”



Solution

3 main problems with your code (thers more):

1. week_days = week_day_1, week_day_2, week_day_3, week_day_4 should be a list with brackets.

2. You assert a boolean value to these variable after assert list to them.

3. There is no examination of the input. There is only an examination of the boolean values which are always True.

Here is a code that will work:

half_week_day = "Friday"
week_days = ["Monday", "Tuesday", "Wednesday", "Thursday"]
weekend_days = ["Saturday", "Sunday"]

day = input("What day of the week is it?") #the input allows the user to insert the day of the week.
if day in week_days:
    print("You must go to work.")
elif day in weekend_days:
    print("It is the weekend. Enjoy your time off.")
elif day == half_week_day:
    print("You must go to work, but your weekend start once you clock out.")
else:
    print("Please enter day of the week.")


This Question was asked in StackOverflow by Michael Jordan and Answered by Colonel Mustard It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?