Home > other >  Semantic Error with If ,elif, and else statements
Semantic Error with If ,elif, and else statements

Time:09-19

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"

CodePudding user response:

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:

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]

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.")

CodePudding user response:

You need to assign the user's input to a variable, then you can compare it to the tuples you've created.

For consistency I've changed half_week_day to half_week_days, and made it a tuple (putting a , after week_day_5 in the assignment).

The assignments like week_days = True are replacing the tuples that list each type of day. They aren't needed.

week_day_1 = "Monday"
week_day_2 = "Tuesday"
week_day_3 = "Wednesday"
week_day_4 = "Thursday"
week_day_5 = "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
half_week_days = week_day_5,
today = input("What day of the week is it?") #the input allows the user to insert the day of the week.
if today in week_days:
    print("You must go to work.")
elif today in weekend_days:
    print("It is the weekend. Enjoy your time off.")
elif today in 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.")

CodePudding user response:

The main reason your existing code is not working is because you are not saving the input value and comparing that further, that's why it is executing the last else every time. If I were you I would use this way to do the task-

week_days = ["Monday", "Tuesday", "Wednesday", "Thursday"]
weekend_days = ["Saturday", "Sunday"]
half_week_day = "Friday"    
day_name = input(
        "What day of the week is it?"
)  # the input allows the user to insert the day of the week.
if day_name in week_days:
    print("You must go to work.")
elif day_name in weekend_days:
    print("It is the weekend. Enjoy your time off.")
elif day_name == 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.")

Explanation,

1. I used list to store week_days instead of multiple variables.

2. I used a new variable that will store the user input data so I can use it to compare with the existing week_days, weeked_days, and half_week_day.

3. I used in which will help us to check if a value exists in multiple values.

CodePudding user response:

if weekdays basically checks for the "truthyness" of an object in python. Since weekdays is an array with elements in it this will evaluate to True (whereas an empty array would evaluate to False e.g. if [] == False)

what you want to do is assign your input to a variable like day_to_check and then you want to check if this day is IN these arrays.

e.g:

day_to_check = "Monday"
if day_to_check in weekdays:
    ...

I'd recommend writing a script for this and using argparse, chekc out how to use `if name == "main" when writing the script and google argparse.

Also, you can make this a bit easier/less boilerplate by using the calendar module in python:

import calendar

print(calendar.day_name[0])
>>>Monday
  • Related