Demo entry 6772068

Homework

   

Submitted by anonymous on Nov 23, 2018 at 09:50
Language: Python. Code size: 1.3 kB.

import random, operator

def requestInput():
	'''
	Asks for user name and prints greeting
	'''
	print("Welcome to the Maths Quiz")
	name = raw_input("what is your name?")
	print('hello ' + name + ' ready for your first question?')
	return 0

def randomCalc():
	'Define Dictionary of maths functions'
	ops = {
		'+': 'operator.add',
		'-': 'operator.sub',
		'*': 'operator.mul',
		'/': 'operator.truediv'
	}

	'select random dictionary item and assign to tuple'
	operatorTuple = random.choice(ops.items())

	'assign items from tuple to strings - step for clarity'
	'this step could be removed and the tuple entries could be returned as part of the function return'
	operatorString = operatorTuple[0]
	operatorFunction = operatorTuple[1]

	'assign random numbers to strings'
	num1 = int(random.randint(1, 101))
	num2 = int(random.randint(1, 101))

	'return mathmateical string, python operator function and two random numbers in one long string'
	return operatorString,operatorFunction,num1, num2


'call the two functions above'
requestInput()
print "Random data to use : " + str(randomCalc())

'Note - you will need to use input rather than raw_input in python v3.x'
raw_input("Press Enter to continue ...")

'''
def askQuestion():
answer=randomCalc()
guess=float(input)
return guess==answer
'''

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).