Code:
def Division():
print "************************\n""********DIVISION********\n""************************"
counter = 0
import random
x = random.randint(1,10)
y = random.randint(1,10)
answer = x/y
print "What will be the result of " + str(x) + '/' + str(y) + " ?"
print "\n"
userAnswer = input ("Enter result: ")
if userAnswer == answer:
print ("Well done!")
print "\n"
userInput = raw_input ("Do you want to continue? \n Enter 'y' for yes or 'n' for no.")
if userInput == "y":
print "\n"
Division()
else:
print "\n"
Menu()
else:
while userAnswer != answer:
counter += 1
print "Try again"
userAnswer = input ("Enter result: ")
if counter == 3: break
userInput = raw_input ("Do you want to continue? \n Enter 'y' for yes or 'n' for no.")
if userInput == "y":
print "\n"
Division()
else:
print "\n"
Menu()
In this case I want x value to be always bigger than y value. How to do I do it?
Code for subtraction is similar and the question remains the same, target is to avoid
negative result.
You can check if
x < yand swap them e.g.Note in python you can swap two variables without need of a temporary variable.
You can even take further shortcut by using bultin
minandmaxand do it in a single line e.g.