If elif else

If you haven't read if else do that first.

elif allows you to extend your choices to more than just the two available with if else.
Now you can have
if
elif
elif
elif
elif (as many as you need)
else

Adventure Game Example
choice = input("Type a to attack, b to back away or c to creep past.") #The user now fills the variable choice with a, b or c
if choice == ("a"):
     print("You attack with your terrible sword")
elif choice == ("b"):
     print("You back away quietly")
else:
     print("Carefully you sneak past")
Note it is possible for the user to type something other than a, b or c. In this case it would go to the else command.

Web Page Menu Example
(Copy and paste to try the code)
import webbrowser
bbc = ("http://www.bbc.co.uk")
newsround = ("http://www.bbc.co.uk/newsround")
yahookids = ("http://kids.yahoo.com/")
moshi = ("http://www.moshimonsters.com/")

choice = input("""
Type
a for BBC
b for Newsround
c for YahoohKids
d for Moshi Monsters""")

if choice == ("a"):
    webbrowser.open(bbc)
elif choice == ("b"):
    webbrowser.open(newsround)
elif choice == ("c"):
    webbrowser.open(yahookids)
else:
    webbrowser.open(moshi)
#Webbrowser opens web pages
#Put websites inside these variables




#Allow user to choose which website to open






#If user chooses a then run indented lines

#elif user chooses b run indented code

#elif user chooses c run code underneath

#else user clicks anything else run final lines #of code

webbrowser
Comments