0
Easy help with lists š š
I'm practicing working with lists by trying to make a simple birthday month calculator: months[Jan, Feb, Mar, Apr, May, June, July, Aug, Sept, Oct, Nov, Dec] months_ago = int(input()) #How many months ago was your birthday? this_month = input() #What month is it now? bday_month = months[this_month - 9] if this_month in [months]: print(āYour birthday is in ā + bday_month + ā!ā) else: print("Er, try again using an abbreviation...") Can you help me spot the mistakes? Iām getting an error message about not giving the list (month) an identifier, so my syntax there must be wrong, for starters š¬
7 Answers
+ 4
months = ["Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"]
months_ago = int(input("How many months ago was your birthday?:- "))
this_month = input("What month is it now?:- ")
if this_month in months:
bday_month = months.index(this_month)-months_ago
print(months[bday_month])
else:
print("Er, try again using an abbreviation...")
+ 3
#Try this
print("Her birthday is in " + str(bday_month) + "!")
in order to convert a number to a string, the str() function is used.
Edit:
Jennywren I mean we can't add an integer to strings . So, we have to convert it.
Your code is working fine but problem is the print statement.
print("Her birthday is in"+str(bday_month)+"!")
Here, bday_month is an integer. But "Her birthday is in " and "!" are strings. That's why we used str() function to convert it to string :)
+ 2
Try to give the code in code playground and just send the link here so it will be easy to read to others
+ 1
Simba Letās see if i understand: the items in the list ARE in string format, but because the previous code refereneces the index, i have to typecast it back to string... is that right?
0
Thank you sooo much for your help š
0
Followup question:
since Iāve defined the bday_month variable, why doesnt it work to write it without brackets or parenthesis like this?:
if this_month in months:
bday_month = months.index(this_month) - months_ago
print("Her birthday is in " + bday_month + "!")
0
Sayyam Jain Ok Iāll do that from now on, thanks! š