#import string so we can use string.upper
#import random to generate random integers
import string
import random
#function for exercise 3
def divisionPlusRemainder(num1, num2):
divided = num1/num2
remainder = num1 % num2
print num1, "divided by ", num2, "is: ", divided, "with remainder: ", remainder
#function for exercise 8
def factorialWhileLoop(number):
#declaring total and assigning it a value of 1 works because any x *1 = x always
total = 1
#loops calculates number * number -1 * number-2 *....*1
while number>0:
total = total*number
number = number -1
print total
#function for exercise 9
def factorialForLoop(number):
#declaring total and giving it a value of 1, 1 * x always equals x
total = 1
#This for loop ranges from 0- (number -1)
#Must multiply by i+1 so that we do not multiply by 0 and to make sure we multiply by number
for i in range(number):
total = total*(i+1)
print total
#Alternate loop: for i in range(1, (number+1)):
# total = total * i
#For the alternate loop i will have the values 1 to number
#First function for exercise 10
#Adds an item to a shopping list
def addItem(shoppingList):
newItem = raw_input("Enter the item you wish to add: ")
shoppingList.append(newItem)
return shoppingList
#Second function for exercise 10
#Removes an item from a shopping list
def delItem(shoppingList):
delItem = raw_input("Enter the item you wish to remove: ")
countItems = 0
i = 0
#We are counting the number of items in the list so we know how many times we need to loop to match the item the
#user wishes to remove
for item in shoppingList:
countItems = countItems+1
#J goes from 0 to countItems -1 because lists start at 0
for j in range(0,countItems):
#If the value stored at that index is equal to the string the user wants to remove, delete it
if shoppingList[i]==delItem:
del shoppingList[i]
#Only increase i if we don't find a match. If we remove an item, our list gets shorter and all items
#Get shifted left. If we always increase i, we'll go out of bounds (an index higher than what our list has)
else:
i = i+1
return shoppingList
#Third function for exercise 10
#Simply prints the items in the shopping list, one item per line
def printList(shoppingList):
print "Your shopping list contains the following items:"
for item in shoppingList:
print item
#Fourth function for exercise 10
#Prints the menu options for the user and returns their input
def printMenu():
print
print"Please select one of the following:"
print " 1.Add a new item to your shopping list"
print " 2.Remove an item from your shopping list"
print " 3.Print your shopping list"
print " 4.Exit"
#change the type of the input from string to an integer
selected = int(raw_input(""))
return selected
def function1():
num = int(raw_input("Enter a number: "))
print "2 times " + str(num) + " is " + str(2 * num)
def function2():
userName = string.upper(raw_input("Enter your name"))
print userName
def function3():
num1 = int(raw_input("Enter your first number: "))
num2 = int(raw_input ("Enter your second number: "))
divisionPlusRemainder(num1, num2)
def function4():
randomNumber = random.randint(1,10)
userGuess = 0
while userGuess!=randomNumber:
userGuess = int(raw_input("Enter a number between 1 and 10 "))
print "Congrats! You got it!"
def function5():
randomNumber = random.randint(1,10)
userGuess = 0
while userGuess!=randomNumber:
userGuess = int(raw_input("Enter a number between 1 and 10 "))
randomNumber = random.randint(1,10)
print "Congrats! You got it!"
def function6():
age = int(raw_input("Enter your age: "))
if age<=19:
print "you get a student discount"
elif age<55:
print "you do not qualify for a discount"
else:
print "you qualify for a senior discount"
def function7():
babyNames = ["Martha", "Magda", "Mandy", "Missy", "Marsha"]
for name in babyNames:
print name + " Smith"
newName = raw_input("Enter another name: ")
babyNames.append(newName)
for name in babyNames:
print name + " Smith"
def function8():
number = int(raw_input("Enter the number you wish to calculate the factorial of. "))
factorialWhileLoop(number)
def function9():
number = int(raw_input("Enter the number you wish to calculate the factorial of. "))
factorialForLoop(number)
def function10():
shoppingList = ["bread", "milk", "cheese", "eggs"]
userInput = 1
while userInput!=4:
userInput = printMenu();
if userInput == 1:
shoppingList = addItem(shoppingList);
elif userInput ==2:
shoppingList = delItem(shoppingList);
elif userInput ==3:
printList(shoppingList);
print "Thank you for using your shopping list"
print
userInput = int(raw_input("Enter the number of the function you wish to run between 1 and 10 or 0 to exit"))
while userInput!=0:
if userInput ==1:
function1()
elif userInput ==2:
function2();
elif userInput ==3:
function3()
elif userInput ==4:
function4()
elif userInput ==5:
function5()
elif userInput ==6:
function6()
elif userInput ==7:
function7()
elif userInput ==8:
function8()
elif userInput ==9:
function9()
elif userInput ==10:
function10()
else:
print "You need to enter a number between 1 and 10, or 0 to exit"
print
userInput = int(raw_input("Enter the number of the function you wish to run between 1 and 10 or 0 to exit"))
print