Exercise 34-40

This commit is contained in:
Ganome 2021-04-20 20:17:14 +00:00
parent 3562ed8e41
commit bc73134e6c
5 changed files with 190 additions and 0 deletions

View File

@ -0,0 +1,8 @@
animals = ["Bear", "Tiger", "Penguin", "Zebra", "Elephant", "Hippo"]
bear = animals[0]
#different ways to print items from a list
print(f"the list animals[0] is {animals[0]}, but the variable bear is {bear}")
print(f"The animal 1: {animals[1]}\nThe third animal: {animals[2]}\nThe first animal: {animals[0]}")
print(f"The animal at 3: {animals[3]}\nThe fifth animal: {animals[4]}\nThe animal at 2: {animals[2]}")
print(f"The sixth animal: {animals[5]}\nThe animal at 4: {animals[4]}")

View File

@ -0,0 +1,81 @@
#!/usr/bin/python3
from sys import exit
def goldRoom():
print("This room is full of gold. How much do you take?")
choice = input("Greedy? > ")
if "0" in choice or "1" in choice:
howMuch = int(choice)
else:
dead("Man, learn to type a number.")
if howMuch < 50:
print("Nice, you're not that greedy.\n\t\t***YOU WIN***")
exit(0)
else:
dead("You greedy bastard!!")
def bearRoom():
print("There is a bear in this room!")
print("The bear has a bunch of honey.\nThe fat bear is in front of another door.")
print("How are you going to move the bear?")
print("'take honey' , 'taunt bear' , 'open door'")
bearMoved = False
while True:
choice = input(" > ")
if choice == "take honey":
dead("The bear looks at you then slaps your face off!")
elif choice == "taunt bear" and not bearMoved:
print("The bear has moved from the door.\nYou can now go through!")
bearMoved = True
elif choice == "taunt bear" and bearMoved:
goldRoom()
elif choice == "open door" and bearMoved:
goldRoom()
elif choice == "open door" and not bearMoved:
dead("You tried to push through the door and the bear ate you instead of the honey!")
else:
print("I got no idea what that means. Please try again")
def cthulhuRoom():
print("Here you see the great evil cthulhu.\nHe, it, whatever stares at you and you go inside.")
print("Do you flee for your life or eat your own head?")
choice = input("Dine or Dash? > ")
if "Dash" in choice or "dash" in choice:
start()
elif "Dine" in choice or "dine" in choice:
dead("Well aren't you tasty?")
else:
cthulhuRoom()
def dead(why):
print("\n", why, "\nGood Job, you are DEAD!!!\v\v")
#Adding in an option to play again
choice = input("Would you like to play again?\nYes or No\n>")
if choice == "yes" or choice == "Yes":
start()
elif choice == "no" or choice == "No":
exit(0)
else:
print("Not a a valid answer!!")
def start():
print("You are in a dark room.\nThere is a door to your right and left")
#print("Which door do you walk through??")
#choice = input(" > ")
choice = input("Which door do you walk through??\n>")
if "Left" in choice or "left" in choice:
bearRoom()
elif "Right" in choice or "right" in choice:
cthulhuRoom()
else:
dead("You stumbled around and wondered yourself to death.")
start()

View File

@ -0,0 +1,26 @@
#!/usr/bin/python3
tenThings = "Apples Oranges Crows Telephone Light Sugar"
print("Wait there are not 10 things in that list. Let's fix that.")
stuff = tenThings.split(' ')
moreStuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]
while len(stuff) != 10:
nextOne = moreStuff.pop() #pop pulls the last index first, unless otherwise specified inside the ()
print(f"Adding {nextOne} to the list")
stuff.append(nextOne)
print(f"There are {len(stuff)} items now.")
print(f"There we go: {stuff}")
print("Let's do some things with stuff!")
print(stuff[1])
print(stuff[-1]) #should be end of list
print(stuff.pop())
print(' '.join(stuff))#This prints the entire list using and seperates entries with a space
print(" # ".join(stuff[3:5])) #this joins index 3 and index 5, with a "#" in between
print(f"Length of list: {len(stuff)}")

View File

@ -0,0 +1,56 @@
#!/usr/bin/python3
states = {
'Oregon' : 'OR',
'Colorado' : 'CO',
'Florida' : 'FL',
'California' : 'CA',
'New York' : 'NY',
'Michigan' : 'MI'
}
cities = {
'CA' : 'San Jose',
'CA' : 'Sacramento',
'CO' : 'Denver',
'CO' : 'Colorado Springs',
'MI' : 'Detroit',
'NY' : 'New York City',
'FL' : 'Jacksonville',
'OR' : 'Portland'
}
print('-' * 10)
print("NY State has: ", cities['NY'])
print(f"Colorado state has: {cities['CO']}")
print("-" * 10)
print("Michigan's Abbrviation is: ", states['Michigan'])
print(f"California's abbreviation is: {states['California']}")
print("Colorado has city: ", cities[states['Colorado']])
print(f"Florida has city: {cities[states['Florida']]}")
print("-" * 10)
for state, abbrev in list(states.items()):
print(f"{state} has abbreviation {abbrev}")
#print every city in every state
for city, state in list(cities.items()):
print(f"{city} has {state}")
#now both at the same time!
print("-" * 10)
for state, abbrev in list(states.items()):
print(f"{state} is abbreviated {abbrev}")
print(f"and has city {cities[abbrev]}")
#safely fetch a state from the list that does not exist
state = states.get("Texas")
if not state:
print(f"Sorry that state is not in our list!")
#get a city with default value
city = cities.get("TX", "Does not Exist")
print(f"The city for the state 'TX' is: {city}")

View File

@ -0,0 +1,19 @@
#!/usr/bin/python3
class Song(object):
def __init__(self, lyrics):
self.lyrics = lyrics
def singMeASong(self):
for line in self.lyrics:
print(line)
happyBday = Song(["Happy Birtday to you",
"IYou belong in a zoo",
"You look like a monkey, and smell like one too!"])
bullsOnParade = Song(["They rally around your family",
"With a pocket full of shells"])
happyBday.singMeASong()
bullsOnParade.singMeASong()