Reputation: 43
Every time I input 1 it will say list index out of range I ended up putting a if statement that said if input is 1 print January How can I make this program work with out the first if statement
months= ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]
months_nums=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
user_input= int(input("Enter month: "))
while user_input != 0:
for i in months_nums:
if user_input == months_nums[i]:
print(months[i])
break
user_input= int(input("Enter months: "))
Upvotes: 1
Views: 60
Reputation: 18914
Instead of creating a list why not use the datetime library:
import datetime
user_input = int(input("Enter month: "))
while user_input != 0:
month = datetime.datetime.strptime(str(user_input),"%m")
print(month.strftime("%B"))
user_input= int(input("Enter months: "))
Basically you can read more about strftime which prints a datetime object in a specific way: e.g. datetime.strftime("%B") gives month. You also need strptime which reads a date from a string, e.g. datetime.strptime("31/12/1999","%d/%m/%Y"),and we only need to read the month in this case.
However, I would probably check if input is an integer and then check if it is within the range 1-12 if not quit. Something like this this:
import datetime
# Start a loop
while True:
# Ask for a number until you get one
while True:
user_input= input("Enter months: ")
try:
user_input = int(user_input)
break
except ValueError:
print("you must enter an integer")
# Break if user input not in range
if user_input not in range(1,12):
break
# Get current month
month = datetime.datetime.strptime(str(user_input),"%m")
# Print
print(month.strftime("%B"))
Upvotes: 0
Reputation: 4744
If you are simply looking to print the month name based on a continuously prompted user input, why not use this?
user_input= int(input("Enter month: "))
while user_input != 0:
print(months[user_input-1])
user_input= int(input("Enter months: "))
This takes advantage of the order in the array month
- month name index in that array is simply the input number - 1.
The reason why your loop didn't work is actually a small and not that visible bug. for i in months_nums:
means you're literally fetching the elements of months_nums
, not indices. In other words i = 1,2,3,..,12
not 0,1,2,..11
.
When looking for January you ask for the first element which is never found because i
in months_nums[i]
will never have a value 0
and fetch you month number 1. On the other hand it will eventually reach value 12 and the last index of month_nums
is 11 - which is when it throws an error.
You can fix this issue in your original loop by using i
as elements which they actually are, rather then indices in the if
statement:
user_input= int(input("Enter month: "))
while user_input != 0:
for i in months_nums:
if user_input == i:
print(months[i-1])
break
user_input= int(input("Enter months: "))
This one still takes advantage of ordering in months
so the printed value has an index of i-1
.
Upvotes: 1
Reputation: 356
months= ["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December" ]
months_nums=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
user_input= int(input("Enter month: "))
while user_input != 0:
for i in months_nums:
if user_input == i:
print(months[i-1])
break
user_input= int(input("Enter months: "))
Upvotes: 0
Reputation: 2338
months= ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]
months_nums=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
user_input= int(input("Enter month: "))
while user_input != 0:
if user_input in months_nums:
print(months[user_input-1])
break
user_input= int(input("Enter months: "))
Upvotes: 0
Reputation: 4821
The issue has to do with the way you are iterating the indices and checking if a user's input matches the month in your months_num list.
if user_input == months_nums[i]
wouldn't work because if a user inputs 1
months will be February
cause lists count from 0
.
Instead you should do the following:
months= ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]
months_nums=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
options = dict(zip(months_num, months))
user_input= int(input("Enter month: "))
try:
print(options[user_input])
except KeyError as e:
print(e)
Upvotes: 0