I am writing a simple program that calculates a random addition math problem, and asks the user to enter the answer.
When the user enters an answer, the computer inputs the entered keys from left to right. I want the input to do the opposite: from right to left.
http://euclidmagazine.com/2010/12/why-do-we-carry-in-addition/
Of course, the way addition works is that you add the column on the rightmost side first - the single digits, then the ten's and hundred's, ect.
For example, if I type the answer to this addition problem above (635), and I type it in that order (6 then 3 then 5), I want the 6 to appear under the 9 + 6, then when I type 3 I want it to move the 6 (to the left) under the 5 + 7 and the 3 will appear under the 9 + 6, and then when I enter the 5 it moves the 6 under the 3 + 2, moves the 3 under the 5 + 7, then place the 5 under the 9 + 6.
Is there an alternative input function or module that I can use for this? Or a super-secret technique that you have? Thanks.
Here is my code:
# MAIN
def main():
keepGoing = "Y"
while keepGoing == "Y":
keepGoing = "N"
entranceMessage()
number1, number2 = calculateProblem()
userInput = getInput(number1, number2)
calculateResults(userInput, number1, number2)
keepGoing = input("Would you like to try another problem? \
(Enter \"Y\" for \"Yes\", or any other key for \"No\".): ")
exitMessage()
# DISPLAY OPENING MESSAGE
def entranceMessage():
print()
print("2. Math Quiz (Page 235)")
print("This program will prompt you to answer a series of random mathematical problems.")
# INPUT
def getInput(number1, number2):
print("Answer the following math problem: ")
print("")
if (number1 < 10):
print(" ", number1)
else:
print(" ", number1)
if (number2 < 10):
print("+ ", number2)
else:
print("+", number2)
print("_____")
print(" ")
result = (number1 + number2)
if (result >= 100):
userInput = float(input(" "))
elif ((result >= 10) and (result < 100)):
userInput = float(input(" "))
elif ((result >= 0) and (result < 10)):
userInput = float(input(" "))
return userInput
# PROCESS
def calculateProblem():
import random
number1 = random.randint(1, 100)
number2 = random.randint(1, 100)
return number1, number2
# OUTPUT
def calculateResults(userInput, number1, number2):
result = (number1 + number2)
if (userInput == result):
print()
print("Correct!")
else:
print()
print("Sorry, the answer is:", result)
# DISPLAY CLOSING MESSAGE
def exitMessage():
print("Thank you. Exiting the program..")
# CALL THE MAIN FUNCTION
main()
If you notice that in my code in the function "getInput" (the last If-elif), at the very end it I have one method that places the initial cursor at a correct alignment pertaining to what the answer would be, but (A) This sort of gives a clue to what the answer would be, (B) Its not what I want.