Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If else help
#1
Hi everyone
I' just started with python and i am not so good with math...but i am trying. I need some help with my homework to:

"Create a little programm that the user inserts a year between 2000 - 2040 and the program tells if it was Olympic games that year, or world cup in footbol, or nothhing special.

Olympic games are every 4 years(2000, 20004, etc) and world cup every 4 years (2002, 2006 etc)
I wrote a code that works but it must be a shorter way to do it, instead of writing all the years between 2000-2040.... ?

Here is my code:

year = input("Write a year between 2000-2040: ")
if year=="2000" or year=="2004" or year=="2008" or year=="2012" or year=="2016" or year=="2020" or year=="2024" or year=="2028" or year=="2032" or year=="2036" or year=="2040" or year=="2044" or year=="2048":
print("Olympic games")
elif year=="2002" or year=="2004" or year=="2006" or year=="2010" or year=="2014" or year=="2018" or year=="2022" or year=="2026" or year=="2030" or year=="2034" or year=="2038" or year=="2042" or year=="2044" or year=="2040":
print("world cup")
elif year== "2001" or year=="2003" or year=="2005" or year=="2007" or year=="2009" or year=="2011" or year=="2013" or year=="2015" or year=="2017" or year=="2019" or year=="2021" or year=="2023" or year=="2025" or year=="2027" or year=="2029" or year=="2031" or year=="2033" or year=="2035" or year=="2037" or year=="2039":
print("No o-g or w-c this year")
else:
print("Write a year between 2000-2004")

Thanks!
Reply
#2
You could use the modulo operator:
2000 % 4 == 0
2001 % 4 == 1
2002 % 4 == 2
2003 % 4 == 3
Reply
#3
(Nov-25-2017, 01:53 PM)heiner55 Wrote: You could use the modulo operator:
2000 % 4 == 0
2001 % 4 == 1
2002 % 4 == 2
2003 % 4 == 3

Hi and thanks for the help
The problem is that i am absolut beginner and i am not sure how to use the modulo operator.
liitle bit more help please Smile
Reply
#4
if year % 4 == 0:
    print("olympia")
Reply
#5
(Nov-25-2017, 02:34 PM)heiner55 Wrote:
if year % 4 == 0:
    print("olympia")

Thank you so much!
I got it now and it works! Dance
Reply
#6
Then show us your code.
Reply
#7
(Nov-25-2017, 03:01 PM)heiner55 Wrote: Then show us your code.

Here it is, the comments are in swedish....

year = int(input("Ange ett år mellan 1950-2050: "))
if year<1950 or year>2050:
print("Du verkar ha svårt att följa instruktioner, \nAnge ett år mellan 1950-2050")
elif year%4==0:
print(year, "är olympiska spel år")
elif year%2==0:
print(year, "är fotbolls-vm år")
elif year%2==1:
print("Ingeting särskilt detta år")

Here it is in English:
year = int(input("write a year between 1950-2050: "))
if year<1950 or year>2050:
print("You did not understand the instructions, \nwrite a year between 1950-2050")
elif year%4==0:
print(year, "olympic games")
elif year%2==0:
print(year, "footbol wc")
elif year%2==1:
print("Neither")
Reply
#8
Have you tested your code ?
(fotbolls: 2018 2020 2022)
Reply
#9
(Nov-25-2017, 03:24 PM)heiner55 Wrote: Have you tested your code ?
(fotbolls: 2018 2020 2022)

Yes, it works
2018 is footbol
2020 is olympic
2022 is footbol
Reply
#10
Good work.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020