Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def registration():
- ## asks for users username and password and then adds it to a list
- username = raw_input('Enter username ')
- password = raw_input('Enter password ')
- regis = []
- regis.append(username)
- regis.append (password)
- ##prints username and password and returns it to the main code
- print 'Username:', username
- print 'Password:', password
- return regis
- def menu():
- ## prints different options for the user to choose from
- print ' --------- MENU --------- '
- print ''
- print ' Option 1: Rectangle'
- print ''
- print ' Option 2: Triangle'
- print ''
- print ' Option 3: Circle (pi = 3.14)'
- print ''
- print ' - Pick an option (number) - '
- def rectangle():
- ## if the user selects the rectangle this function will run
- import random
- ## selects random numbers between 1 and 12 for the width and height
- wid = random.randint(1,12)
- hei = random.randint(1,12)
- rarea = wid*hei
- ## creates different options for the user to choose from. Each vary a little
- ro1 = rarea
- r2 = rarea+1
- r3 = rarea-2
- r4 = rarea+4
- rectangleoptions = [ro1,r2,r3,r4]
- random.shuffle(rectangleoptions)
- ## asks the user to guess the area.
- print 'Width =', wid, ' Height =', hei
- print 'options are', rectangleoptions
- rguess = input('What is the area?')
- ## if they get it correct the first time, the user is rewarded 2 points
- if rguess == ro1:
- print 'correct'
- rscore = 2
- return rscore
- else:
- ## if they dont get it right the first time they are given a second chance after the user is the formula to help them
- print 'Try again'
- print 'The formula to find the area of a rectangle is width x height'
- rguess2 = input('What is the area?')
- if rguess2 == ro1:
- print 'correct'
- rscore = 1
- return rscore
- ## if they dont get it right they get zero points
- ## the score is returned to the main program
- else:
- print 'incorrect'
- print 'the answer was', ro1
- rscore = 0
- return rscore
- def triangle():
- ## if the user selects the triangle this function will run
- import random
- wid1 = random.randint(1,12)
- hei1 = random.randint(1,12)
- tarea = 0.5 * wid1 * hei1
- to1 = tarea
- t2 = tarea+1
- t3 = tarea-2
- t4 = tarea+4
- triangleoptions = [to1,t2,t3,t4]
- random.shuffle(triangleoptions)
- print 'Width =', wid1, ' Height =', hei1
- print 'options are', triangleoptions
- tguess = input('What is the area? (type in exactly how option has shown (10.5))')
- if tguess == to1:
- print 'correct'
- tscore = 2
- return tscore
- else:
- print 'Try again'
- print 'The formula to find the area of a traingle is 1/2 x base x height'
- tguess2 = input('What is the area?')
- if tguess2 == to1:
- print 'correct'
- tscore = 1
- return tscore
- else:
- print 'incorrect'
- print 'the answer was', ro1
- tscore = 0
- return tscore
- def storeinfile(stats):
- filename = 'stats.txt'
- f = open(filename, 'a')
- for i in stats:
- f.write(str(i) + "\n")
- f.close()
- def circle():
- import random
- radius = random.randint(1,12)
- carea = 3.14 * radius**2
- o1 = carea
- o2 = carea + 1
- o3 = carea - 2
- o4 = carea + 4
- circleoptions = [o1,o2,o3,o4]
- random.shuffle(circleoptions)
- print 'radius =', radius
- print 'options are', circleoptions
- cguess = input('What is the area? (type in exactly how option has shown (10.5))')
- if cguess == o1:
- print 'correct'
- cscore = 2
- return cscore
- else:
- print 'Try again'
- print 'The formula to find the area of a circle is pi x radius**2'
- cguess2 = input('What is the area?')
- if cguess2 == o1:
- print 'correct'
- cscore = 1
- return cscore
- else:
- print 'incorrect'
- print 'the answer was', o1
- cscore = 0
- return cscore
- try:
- regis = registration()
- a = 1
- import sys
- total = 0
- while a == 1:
- menu()
- print ''
- option = raw_input('Choose an option ')
- if option == '1':
- rscore = rectangle()
- total = total + rscore
- elif option == '2':
- tscore = triangle()
- total = total + tscore
- elif option == '3':
- cscore = circle()
- total = total + cscore
- restart = raw_input('Do you want to choose another shape? y / n ')
- if restart == 'y':
- print ''
- elif restart == 'n':
- 'quitting program'
- a = 2
- print total
- stats = []
- stats.append(regis)
- stats.append(total)
- print stats
- storeinfile(stats)
- break
- else:
- print ''
- except:
- print 'An error has occured'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement