Thursday, September 15, 2022

[FIXED] How to write a program to print name of day corresponding to the day number given by user

Issue

I want an input like If I type 1 then it will give output Sunday , if I type 2 it will give Monday. I tried this code but it is not working , plz help

days =["Sunday" ,"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

user_input = int(input("Enter Day"))
if user_input == (1,2,3,4,5,6,7) :
    print (days[0,1,2,3,4,5,6])

I want to make my program simple , I am a beginner


Solution

days =["Sunday" ,"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

user_input = int(input("Enter Day (from 1-7)"))
if user_input:
    print (days[user_input-1])
else:
    print ("Your input is not valid")

With a dictionary

days_dict ={'1':"Sunday" ,'2':"Monday",'3':"Tuesday", '4':"Wednesday", '5':"Thursday", '6':"Friday", '7':"Saturday"}
print (days_dict[input("Enter Day (from 1-7): ")])


Answered By - Seyi Daniel
Answer Checked By - David Marino (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.