Python newbie here.
My task is to create a dictionary with months, then ask for a user to input number. If number is 1-12, return name of the month, if user has entered any other number, return 'Please enter valid number in range 1-12' and if user entered anything else (f.e. "What?") - to return '{input} is not a number!'
So far my first issue is that input is string by default. I decided to change type, but then I have issues when user inputs anything else than integer - I get ValueError: invalid literal for int() with base 10: 'd' But if I leave type string, I have issues when user inputs <1 or >12..
Here is what I have so far:
# 1. Create dictionary
months = {
1: 'January',
2: 'February',
3: 'March',
4: 'April',
5: 'May',
6: 'June',
7: 'July',
8: 'August',
9: 'September',
10: 'October',
11: 'November',
12: 'December'
}
# 2. Create input
answer = input('Please input a number of the month: ')
answer = int(answer)
# 3. Create possible outcomes
if answer == 1:
print(months[1])
if answer == 2:
print(months[2])
if answer == 3:
print(months[3])
if answer == 4:
print(months[4])
if answer == 5:
print(months[5])
if answer == 6:
print(months[6])
if answer == 7:
print(months[7])
if answer == 8:
print(months[8])
if answer == 9:
print(months[9])
if answer == 10:
print(months[10])
if answer == 11:
print(months[11])
if answer == 12:
print(months[12])
if answer < 1 or answer >12:
print('Please input valid number in range 1-12!')
Thank you in advance!
Aucun commentaire:
Enregistrer un commentaire