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 😬

6th Dec 2020, 12:50 AM
Jennywren
Jennywren - avatar
7 Réponses
+ 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...")
6th Dec 2020, 1:18 AM
rodwynnejones
rodwynnejones - avatar
+ 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 :)
6th Dec 2020, 4:03 AM
Simba
Simba - avatar
+ 2
Try to give the code in code playground and just send the link here so it will be easy to read to others
7th Dec 2020, 4:21 PM
Sayyam Jain
Sayyam Jain - avatar
+ 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?
6th Dec 2020, 4:25 AM
Jennywren
Jennywren - avatar
0
Thank you sooo much for your help 🙏
6th Dec 2020, 2:24 AM
Jennywren
Jennywren - avatar
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 + "!")
6th Dec 2020, 3:06 AM
Jennywren
Jennywren - avatar
0
Sayyam Jain Ok I’ll do that from now on, thanks! 😅
7th Dec 2020, 5:19 PM
Jennywren
Jennywren - avatar