Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Beginner: How do I print the name of a variable instead of the int that is connected to the variable?

Beginner in python here.
For an assignment I need to get input from the user for whole hours worked on weekdays.
After I get this, I need to print a summary and based on the hours that the user inputs, messages show up saying they worked too much or not enough on a given day.
However. I understand that because I have given the variable a weekday name, the integer I get is a number.
So when I link this it prints just the numbers and not the weekday name.

How can I improve the code so the message would print Too many hours worked on Monday instead of Too many hours worked on 9.

However. I understand that because I have given the variable a weekday name, the integer I get is a number.
So when I link this it prints just the numbers and not the weekday name.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I improve the code so the message would print Too many hours worked on Monday instead of Too many hours worked on 9.
I know in the code I have put the days of the week in a row so it prints Too many hours worked on 9 etc

The code I wrote is as follows:

week = input("Enter Current Working Week: ")
id = input("Enter Employee ID: ")
name = input("Enter Employee Name: ")
monday = int(input("Enter hours worked for Monday: "))
tuesday = int(input("Enter hours worked for Tuesday: "))
wednesday = int(input("Enter hours worked for Wednesday: "))
thursday = int(input("Enter hours worked for Thursday: "))
friday = int(input("Enter hours worked for Friday: "))

print("Summary for Employee", id,)
for i in monday, tuesday, wednesday, thursday, friday:
    if i > 8:
        print("Too many hours worked on", monday, tuesday, wednesday, thursday, friday)
    elif i < 4:
     print("Insufficient hours worked on", monday, tuesday, wednesday, thursday, friday)) 

>Solution :

Here is simple Example, you can modify whatever you needed.

# Dictionary mapping weekday numbers to names
weekday_names = {
    1: "Monday",
    2: "Tuesday",
    3: "Wednesday",
    4: "Thursday",
    5: "Friday"
}

# Initialize an empty list to store the hours worked for each weekday
hours_worked = []

# Get input from the user for each weekday
for i in range(1, 6):
    hours = int(input(f"Enter the number of hours worked on {weekday_names[i]}: "))
    hours_worked.append(hours)

# Print the summary and check for hours worked
for i in range(5):
    if hours_worked[i] > 8:
        print(f"Too many hours worked on {weekday_names[i+1]}.")
    elif hours_worked[i] < 8:
        print(f"Not enough hours worked on {weekday_names[i+1]}.")
    else:
        print(f"Worked exactly 8 hours on {weekday_names[i+1]}.")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading