1
inventory = [“sword”, “Shield”, “Charm”, “Luck”]
spent = “”
word = “”
import random
while inventory != “”:
word = random.choice(inventory)
print (word)
spent = spent+word
if word in spent:
inventory.remove(word)
else:
print(“/n”)
input (“press a key”)
What this program does is select a word at random from a list.
Then it displays the word.
It then selects another word, it will not select the same word more than once.
It will always be a random selection.
The program concludes when there are no more words to display.
2
def game():
again = “”
buy = “”
wallet = 30
attributes = {“Dexterity”: 30, “Wisdom”:30, “Health”:30, “Strength”:30}
print (attributes)
while again != “n”:
if attributes == “”:
print(“goodbye!”)
else:
choice = int(input(“1 = exit n 2 = spend money on attributes n for your character. n 3 = sell an attribute. n 4 = sell off an entire attribute. n Choose a number from 1-4:”))
choices = {1, 2, 3, 4}
while choice in choices:
if choice == 1:
print (“Goodbye”)
input (“press a key”)
elif choice == 2:
#spend coin
buy = input (“what would you like to spend money on?:”)
if buy in attributes:
attributes[buy] += 5
wallet -= 5
print (“You have “, wallet, ” dollars left”); print (attributes)
else: print (“that is not a legit attribute!”)
again = input (“play again y/n”)
if wallet <= 0: print (“you can’t play anymore”); break
elif choice == 3:
#make coin
sell = input (“what would you like to sell for money?:”)
if sell in attributes:
attributes[sell] -= 5
wallet += 5
print (“You have “, wallet, ” dollars left”); print (attributes)
else: print (“that is not a legit attribute!”)
again = input (“play again y/n”)
if wallet <= 0: print (“you can’t play anymore”); break
elif choice == 4:
#sell an attribute
sellAll = input (“which attribute would you like to sell all of?:”)
if sellAll in attributes:
wallet += attributes[sellAll]
del attributes[sellAll]
print (“You have “, wallet, ” dollars left”); print (attributes)
else: print (“that is not a legit attribute!”)
again = input (“play again y/n”)
if wallet <= 0: print (“you can’t play anymore”); break
replay = input(“Are you tough enough to play? y/n”)
if replay == “y”: game()
This program still has a ton of bugs in it.
What it does is display a list of attributes and quantities of those attributes that your character has in his inventory.
Then the program gives you the option of selling some of or all of any of the attributes, or buying more of any of the attributes. I haven’t yet got it to the point where you can choose to do anything.
It all runs fine until you decide to take a different action.
The program never returns to the main menu to list your available actions, it just repeats the question, for example, “what would you like to spend money on?” whereas it should say “what would you like to do next?”
3
parents = {“Luke Skywalker”: “Darth Vader”, “Judy Garland”:”Liza Minelli”, “Peter Fonda”:”Henry Fonda”, “Kate Hudson”:”Goldie Hawn”}
print (“I know these children”)
allInherit = list(parents.keys())
print (allInherit)
child = input (“Tell me a child and I will tell you their parent.”)
if child in parents:
inheritance = parents[child]
print (inheritance)
input (“press a key.”)
This program is much simpler.
The program lists a number of people’s names and boasts that it can tell you the name of one of their parents. You are supposed to input one of the names, be sure to spell it correctly. And the program will display the name of the parent.
This works, but it is a bit too simple, it does one thing and then exits.