#6.2 import math while True: print("Enter your password: ", end = "") pwd = input() if pwd == '12345': break # 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 = "") #6.5 import random myNumber = random.randint(10, 11) if myNumber == 10: print("Heads") elif myNumber == 11: print("Tails") #7.4 import random nTosses = input("number of tosses:") nTosses = int(nTosses) counter = 0 while True: print("counter, nTosses", counter, nTosses, nTosses == counter) input() if nTosses == counter: break myNumber = random.randint(10, 11) if myNumber == 10: print("Heads") elif myNumber == 11: print("Tails") counter += 1 #7.5 import random #6.2 import math while True: print("Enter your password: ", end = "") pwd = input() if pwd == '12345': break # 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 = "") #6.5 import random myNumber = random.randint(10, 11) if myNumber == 10: print("Heads") elif myNumber == 11: print("Tails") #7.4 import random nTosses = input("number of tosses:") nTosses = int(nTosses) counter = 0 while True: print("counter, nTosses", counter, nTosses, nTosses == counter) input() if nTosses == counter: break myNumber = random.randint(10, 11) if myNumber == 10: print("Heads") elif myNumber == 11: print("Tails") counter += 1 #7.5 import random while True: nTosses = input("number of tosses (0 to quit):") nTosses = int(nTosses) if nTosses == 0: break counter = 0 while True: if nTosses == counter: break myNumber = random.randint(10, 11) if myNumber == 10: print("Heads") elif myNumber == 11: print("Tails") counter += 1 #6.6 import random # Compute and store the number to be guessed (1-10) using the random number generator guessThis = random.randint(1, 10) # 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!") if myGuess < guessThis: print("Too low -- it's", guessThis) if myGuess > guessThis: print("Too high -- it's", guessThis) #6.7 import random # Compute and store the number to be guessed (1-10) using the random number generator guessThis = random.randint(1, 10) 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 if myGuess < guessThis: print("Too low") if myGuess > guessThis: print("Too high") #round off error! x = 0 x += 0.1 x += 0.1 x += 0.1 x += 0.1 x += 0.1 x += 0.1 x += 0.1 x += 0.1 x += 0.1 x += 0.1 print(x, x == 1.0) #no round off error! x = 0 x += 0.125 x += 0.125 x += 0.125 x += 0.125 x += 0.125 x += 0.125 x += 0.125 x += 0.125 x += 0.125 x += 0.125 print(x, x == 1.25) #for loops for i in ("Mon", "Tue", "Wed"): # tuple print(i) print(i) for i in range(10): print(i) print(i) s = "Hello" for c in s: print(c) #RSP with tuples import random computerOption = ("Rock", "Scissors", "Paper") x = ( (0, "r", "tie", 2), (0, "s", "computer wins", 0), (0, "p", "human wins", 1), (1, "s", "tie", 2), (1, "p", "computer wins", 0), (1, "r", "human wins", 1), (2, "p", "tie", 2), (2, "r", "computer wins", 0), (2, "s", "human wins", 1) ) totals = [0, 0, 0] # computer, human, tie while True: human = input("r, s, p, q").lower()[0] if human == 'q': break computer = random.randint(0, 2) for result in x: if computer == result[0] and human == result[1]: print(computerOption[computer], result[2]) totals[result[3]] += 1 break print(totals) del x #7.8 import random computerWins = 0 humanWins = 0 ties = 0 while True: computerChoice = random.randint(0, 2) # 0=rock, 1=scissor, 2=paper humanChoice = input("R, S, P, Q:").lower()[0] #print(computerChoice, humanChoice) if humanChoice == 'q': break if computerChoice == 0: print("Computer: Rock,", end=" ") if computerChoice == 1: print("Computer: Scissors,", end=" ") if computerChoice == 2: print("Computer: Paper,", end=" ") if computerChoice == 0: if humanChoice == 'r': print("Tie") ties += 1 if humanChoice == 's': print("Computer wins") computerWins += 1 if humanChoice == 'p': print("Human wins") humanWins += 1 if computerChoice == 1: if humanChoice == 's': print("Tie") ties += 1 if humanChoice == 'p': print("Computer wins") computerWins += 1 if humanChoice == 'r': print("Human wins") humanWins += 1 if computerChoice == 2: if humanChoice == 'p': print("Tie") ties += 1 if humanChoice == 'r': print("Computer wins") computerWins += 1 if humanChoice == 's': print("Human wins") humanWins += 1 print("Human wins:", humanWins) print("Computer wins:", computerWins) print("Ties:", ties) #fun with functions def fun1(a, b=0, c=100): print(a, b, c) a = 100 fun1(a, c=1) print(a) #more functions import random def getRandomNumber(): return random.randint(10, 11) def toss(h, t): myNumber = getRandomNumber() if myNumber == 10: print("Heads") h += 1 elif myNumber == 11: print("Tails") t += 1 return h, t nHeads = 0 nTails = 0 while True: nTosses = input("number of tosses (0 to quit):") nTosses = int(nTosses) if nTosses == 0: break counter = 0 while True: if nTosses == counter: break nHeads, nTails = toss(nHeads, nTails) counter += 1 print(nHeads, nTails) #6.1 answer = input("1 + 2 =") if answer == "3": print("Correct") else: print("Wrong!") answer = input("100 - 99 =") if answer == "1": print("Correct") else: print("Wrong!") answer = input("12 + 21 =") if answer == "33": print("Correct") else: print("Wrong!")