import math def getPwd(maxTries = 3): for i in range(maxTries): print("Enter your password: ", end = "") pwd = input() #print(pwd, pwd == '12345'); input() match = pwd == '12345' if match: return True return False if not getPwd(): exit() # input values years = 10 #number of years making monthly deposits D = 100 #dollars deposited per month # output (calculated) values p = 0.075 / 12 # monthly rate T = years * 12 # number of months S = D * ((math.pow(1 + p, T) - 1) / p) # echoing input values, not rounded print("In", years, "years, $", end = "") print(D, "deposited per month will grow to $", end = "") # rounded output (see 4.1) SFormatted = "%.2f" % S print(SFormatted, ".", sep = "") def askQuestion(question, correctAnswer): userAnswer = input(question) if userAnswer.lower() in correctAnswer: print("Correct") return 1 else: print("Wrong!") return 0 nCorrect = 0 nCorrect += askQuestion("Who heads Microsoft?", ("bill gates", "gates")) nCorrect += askQuestion("100 - 99 =", ("1", "one")) nCorrect += askQuestion("12 + 21 =", ("33")) print("Correct:", nCorrect, "out of 3.") # tuples, "arrays", lists, and maps # objects a = (85, 100, 98, 97, 84) # tuple print(len(a)) for value in a: print(value) if 98 in a: print("Found") b = 98 in a if b: print("Found") print("\nArrays") a = [85, 100, 98, 97, 84] # array a = "85 100 98 97 84".split() for i in range(len(a)): a[i] = int(a[i]) print(len(a)) for value in a: print(value) if 98 in a: print("Found") a[2] = 99 b = 98 in a if b: print("Found") print("\nArray-based lists") a = [None for i in range(5)] n = 0 # number of used positions print(len(a)) print(a) if n < len(a): a[n] = "Hey Jude" n += 1 if n < len(a): a[n] = "She loves you" n += 1 if n < len(a): a[n] = "Paperback writer" n += 1 if n < len(a): a[n] = "Come together" n += 1 if n < len(a): a[n] = "Revolution" n += 1 if n < len(a): a[n] = 840 n += 1 if n < len(a): a[n] = 841 n += 1 if n < len(a): a[n] = 842 n += 1 for i in range(n): for j in range(i, n): if a[j].lower() < a[i].lower(): a[i], a[j] = a[j], a[i] for i in range(n): print(a[i]) # list a = [] a.append("Hey Jude") del a[0] # 13.5 MP3 Player import random songs = [None for i in range(200)] nSongs = 0 # number of used positions songs[nSongs] = "Hey Jude"; nSongs += 1 songs[nSongs] = "She loves you"; nSongs += 1 songs[nSongs] = "Paperback writer"; nSongs += 1 songs[nSongs] = "Come together"; nSongs += 1 songs[nSongs] = "Revolution"; nSongs += 1 songs[nSongs] = "Yes It Is"; nSongs += 1 songs[nSongs] = "All I've Got to Do"; nSongs += 1 songs[nSongs] = "Within You Without You"; nSongs += 1 songs[nSongs] = "Getting Better"; nSongs += 1 songs[nSongs] = "I Feel Fine"; nSongs += 1 while True: index = random.randint(0, nSongs - 1) print(songs[index]) answer = input("Continue? [Y/N]") if answer.lower()[0] == 'n': break # 15.5 MP3 Player import random songs = [None for i in range(200)] nSongs = 0 # number of used positions songs[nSongs] = "Hey Jude"; nSongs += 1 songs[nSongs] = "She loves you"; nSongs += 1 songs[nSongs] = "Paperback writer"; nSongs += 1 songs[nSongs] = "Come together"; nSongs += 1 songs[nSongs] = "Revolution"; nSongs += 1 songs[nSongs] = "Yes It Is"; nSongs += 1 songs[nSongs] = "All I've Got to Do"; nSongs += 1 songs[nSongs] = "Within You Without You"; nSongs += 1 songs[nSongs] = "Getting Better"; nSongs += 1 songs[nSongs] = "I Feel Fine"; nSongs += 1 MRU = [None for i in range(5)] while True: while True: index = random.randint(0, nSongs - 1) if index not in MRU: break MRU.append(index) print(songs[index]) del MRU[0] answer = input("Continue? [Y/N]") if answer.lower()[0] == 'n': break # playing cards class PlayingCard: value = None # 2-14 11=J 12=Q 13=K 14=A suit = None # Spade, Heart, Diamond, Club def sayCard(p): print(p.value, "of", p.suit) deck = [] for suit in ("Spades", "Hearts", "Diamonds", "Clubs"): for i in range(2, 15): p = PlayingCard() p.value = i p.suit = suit deck.append(p) sayCard(deck[10]) # 12.3 playing cards import random class PlayingCard: value = None # 2-14 11=J 12=Q 13=K 14=A suit = None # Spade, Heart, Diamond, Club def sayCard(p): if p.value < 11: print(p.value, "of", p.suit) elif p.value == 11: print("Jack of", p.suit) elif p.value == 12: print("Queen of", p.suit) elif p.value == 13: print("King of", p.suit) elif p.value == 14: print("Ace of", p.suit) deck = [] # the original 52-card deck for suit in ("Spades", "Hearts", "Diamonds", "Clubs"): for i in range(2, 15): p = PlayingCard() p.value = i p.suit = suit deck.append(p) # shuffle for shuffle in range(10): for i in range(len(deck)): for j in range(i, len(deck)): if random.randint(0, 1) == 0: deck[i], deck[j] = deck[j], deck[i] # deal to human and computer humanDeck = [] computerDeck = [] pointer = False while len(deck) != 0: p = deck[0] del deck[0] if pointer: humanDeck.append(p) else: computerDeck.append(p) pointer = not pointer # go until there are no cards left while True: while len(humanDeck) != 0 and len(computerDeck) != 0: humanCard = humanDeck[0]; del humanDeck[0] computerCard = computerDeck[0]; del computerDeck[0] if humanCard.value > computerCard.value: print("\nHuman Wins!") sayCard(humanCard) sayCard(computerCard) break elif humanCard.value < computerCard.value: print("\nComputer Wins!") sayCard(computerCard) sayCard(humanCard) break else: print("\nTie!") sayCard(computerCard) sayCard(humanCard) if len(humanDeck) == 0 or len(computerDeck) == 0: break if input("Again? [Y/N]").lower()[0] == 'n': break # 14.1 import random # Compute and store the number to be guessed (1-10) using the random number generator guessThis = random.randint(1, 10) alreadyGuessed = [] while True: # Output the computer's challenge to the human, to try to guess the randomly selected number print("Guess a number between 1 and 10 inclusive") # Input and store the human's guess as a whole number myGuess = int(input()) if myGuess == guessThis: print("You got it!") break elif myGuess in alreadyGuessed: print("You already guessed", myGuess) elif myGuess < guessThis: print("Too low") alreadyGuessed.append(myGuess) elif myGuess > guessThis: print("Too high") alreadyGuessed.append(myGuess) #fin fin = open("input.txt") for line in fin: line = line.strip() line = line.split('\t') print(line) fin.close() #input.txt one, 1, a two, 2, b three, 3, c # text file output fout = open("output.txt", "w") fout.write("one, two, three\n") fout.write("four, five, six\n") fout.close() # 9.2 and 9.3 s = "Hello, World" # an example sEncoded = "" # an scrambled version of sOld for i in range(len(s)): # for each char... sEncoded += chr(ord(s[i]) + 1) print("Encoded:", sEncoded) sDecoded = "" for i in range(len(sEncoded)): # for each char... sDecoded += chr(ord(sEncoded[i]) - 1) print("Decoded:", sDecoded) # 11.6 and 11.7 a = [4, 5, 2, 1, 3] s = "Hello, World" # an example sEncoded = "" # an scrambled version of sOld for i in range(len(s)): # for each char... x = a[0]; del a[0]; a.append(x) sEncoded += chr(ord(s[i]) + x) print("Encoded:", sEncoded) a = [4, 5, 2, 1, 3] sDecoded = "" for i in range(len(sEncoded)): # for each char... x = a[0]; del a[0]; a.append(x) sDecoded += chr(ord(sEncoded[i]) - x) print("Decoded:", sDecoded) # RSP with maps import random a = {} a[(0, 'r')] = 'tie: rock' a[(0, 's')] = 'computer wins: rock crushes scissors' a[(0, 'p')] = 'human wins: paper covers rock' a[(1, 's')] = 'tie: scissors' a[(1, 'p')] = 'computer wins: scissors cuts paper' a[(1, 'r')] = 'human wins: rock crushes scissors' a[(2, 'p')] = 'tie: paper' a[(2, 'r')] = 'computer wins: paper covers rock' a[(2, 's')] = 'human wins: scissors cuts paper' computer = random.randint(0, 2) human = input("R, S, P:").lower()[0] try: print(a[(computer, human)]) except: print("Invalid selection")