Reputation: 11
Is there a way to condense this code to a simpler form or another way to do this same thing?
if h == 0:
day = 'Saturday'
elif h == 1:
day = 'Sunday'
elif h == 2:
day = 'Monday'
elif h == 3:
day = 'Tuesday'
elif h == 4:
day = 'Wednesday'
elif h == 5:
day = 'Thursday'
else:
day = 'Friday'
print('Day of the week is', day)
Upvotes: 1
Views: 61
Reputation: 107287
You can use a dictionary:
days= {
0:'Saturday',
1:'Sunday',
2:'Monday',
3:'Tuesday',
4:'Wednesday',
5:'Thursday'}
print(days.get(h,'Friday'))
The advantage of using dict.get
method is that you can pass a default value to it which will be return if the key doesn't exist in dictionary.
Upvotes: 5
Reputation: 4425
Create the list
days = ('Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')
day = days[h]
print('Day of the week is', day)
Upvotes: 2