last day (15 days later) » 

7:54 AM
1
A: Python: leap year - olympic games

DimKYou could store all the years with olympic games, associated with the corresponding city in a dictionary like so: host = {2004 : "Athens", 2008 : "Beijing", 2012 : "London", 2016 : "Rio"} Then, once your leap year function has evaluated a year as a leap year, simply access the value of that ye...

 
How would i add this into the code?
 
It depends on how you want to structure your program. Could you be more specific?
 
So i want the user to enter in a year, the program then tests if that year is a leap year, then if it is i want the program to find where the olympic games were held that year (through the code) and add that to the answer
maybe using something like this years=["1986,1900,1905,1908,1912,1920,1924,1928,1932,1936,19‌​48,1952,1956,1960,19‌​64,1968,1972,1976,19‌​80,1984,1988,1992,19‌​96,2000,2004,2008,20‌​12,2016,2020,2024,20‌​28"] cities=["Athens,Paris,St.louis,London,Stockholm,Antwerp,Pari‌​s,Amsterdam,Los Angeles,Berlin,London,Helsinki,Melbourne-stockholm,Rome,Toky‌​o,Mexico,Munich,Mont‌​real,Moscow,Los Angeles,Seoul,Barcelona,Atlanta,Sydney,Athens,Beijing,London‌​,Rio,Tokyo,Paris, LA"]
 
So you are wondering how to create the dictionary? You can either do it manually, or, if you already have those two lists, you can do host = dict(zip(years, cities)). However, the two lists you have provided above will not work with that, because they both contain only one single string element.
 
Sorry im very new to python, how would i change those lists to make them work?
 
7:54 AM
@Codi.no Each year and city would have to be a separate member of the list: e.g. years = ["1896", "1900", "1904"]. If you are going to do this by hand, and not take the data from a file, you might as well create the dictionary directly though (by writing all the entries one by one like I have above).
 
So once i change the entries where would add the host = dict(zip(years, cities)) and how would i make it display the host
 
@Codi.no I added a possible implementation to my answer. See if that helps.
 
Can you please help me with what i did wrong here def isLeap(y): year = int(input("Enter Year: ")) if year % 4 == 0 and year % 100 != 0: print(year, "is a Leap Year") elif year % 400 ==0: print(year, "is a Leap Year") elif year % 100 == 0: print(year, "is not a Leap Year") else: print(year, "is not a Leap Year") host = {2004 : "Athens", 2008 : "Beijing", 2012 : "London", 2016 : "Rio"} userInput = int(input()) if(isLeap(userInput)): print(host[userInput])
sorry i dont know how to make it look right
 
@Codi.no Are you getting an error when running it?
 
"expected an indented block" on the year in year = int(input("Enter Year: "))
and when i move that statement it says "year is not defined"
 
7:54 AM
@Codi.no Could you edit your original question and write the entire code you are running.
 
Just edited it then
 
@Codi.no Ok, I see what the problem is: the if-else block is supposed to be part of the function isLeap, so it should be indented under it. isLeap takes a number as an argument and assesses whether it is a leap year or not. Instead of printing the answer, it must return the appropriate value (True / False).Also, you need not and should not ask for input inside of it.
 
So how would i change that code?
 
@Codi.no Copy and paste the code in my answer without changing anything, and maintain proper indentation. Then, add all the year-city pairs in the dictionary named host. Let me know if it works the way you want.
 
Just finished that, just wondering if im meant to input a date in the code because when i run it nothing comes up. (I'll add the code i have now to my question)
 
7:54 AM
@Codi.no Very good, to have a message appear when you run it simply change userInput = int(input()) to userInput = int(input("Enter Year: ")). Let me know if everything works, and if it does, consider accepting my answer!
Also, you have written host = host = ..., you can delete the second host of course.
 
i changed that, but when i answer the message that appears nothing happens
 
@Codi.no You mean that the message appears, you write a leap year, hit enter, and the city does not appear?
There is a small bug: in the first elif it should say return True, and in the second elif it should say return False.
Also, a few mistakes in the years: the first one is 1896, not 1986, and the third is 1904, not 1905.
 
thank you for all the help
 
@Codi.no Did it work?
 
Yeah it worked, thanks again
 
7:54 AM
@Codi.no You are very welcome!
 

last day (15 days later) »