# Class 11 Examples import random SECRET_NUM = random.randint(1,100) # main program logic, gets user input, does calculations, prinouts output def main(): #accumulator() #guessNumber() #for i in range(100): # print("roll: ", diceRoll()) user_input = int(input("How many times to roll? ")) count = 0 for i in range(user_input): # roll the die roll = diceRoll() # count how many 6's were rolled if ( roll == 6): count += 1 # print what was rolled print("roll: ", roll) print("rolled 6", count, "times") def diceRoll(): ''' returns a random number between 1 and 6 ''' # random roll between 1-6 inclusively roll = random.randint(1,6) # The house loses money on 6's so it's skimming 10% of the 6 rolls if (roll == 6): on_the_house = random.randint(1,10) # 10% of the time, re-roll without a 6 if (on_the_house == 1): roll = random.randint(1,5) return roll def guessNumber(): ''' asks the user to guess a number, tell the user if they are either too high of a guess, or too low of a guess. If they guess the number print the number of guesses it took. ''' numTries = 1 number = int(input("Guess the secret number")) while (number != SECRET_NUM): numTries += 1 if (number > SECRET_NUM): print("Number is too high") number = int(input("Try again: ")) elif (number < SECRET_NUM): print("Number is too low") number = int(input("Try again: ")) print("You guessed the number") print("It took you", numTries, "guesses.") def accumulator(): ''' Adds any number of numbers from the user, until 100 is reached. ''' # start summing at zero total = 0 # get user input user_input = int(input("Give me a number! ")) while (user_input != 0): # sum inputs total += user_input print("total so far:", total) # exit if larger than 100 if (total > 100): print("Exiting because total is greater than 100") break # get the next number user_input = int(input("Give me more numbers (or 0 to exit)!!! ")) # Don't print if even if (user_input % 2 == 0): continue; # run the program main()