# How To Convert Number Into Signs (Math) In Python

## 05 March 2019 - 1 answer

I am trying to make Math Quiz that asking with two random numbers (1, 10) with randomly choice with sum, difference or product. I used `z = random.randint(1, 3)` to generate sum, difference or product, but I want to use this numbers converting to signs like "x", "/", or "+" to shown output to ask Question because I am new to Python language and I am trying to learn on how to convert numbers to symbols.

My Code Here:

``````import random

while(1):
try:
userInput = int(input("Enter a number: "))
break
except ValueError:
print("Incorrect Input!")

return userInput

x = random.randint(1, 10)
y = random.randint(1, 10)
z = random.randint(1, 3)

print(" 1 = product \n 2 = sum \n 3 = difference")
print("What is " + str(x)+" " + str(z)+" " + str(y)+"?")

if z == 1 and u==x*y:
return 1  #product
elif z == 2 and u==x+y:
return 1 #sum
elif z == 3 and u==x/y:
return 1 #difference
else:
return 0
amount = 10
correct = 0
for i in range(amount):

print("You got %d correct out of %d" % (correct, amount))
``````

Reality Output:

``````[email protected]:/u1/work/Python/math> python3 mathquiz.py
1 = product
2 = sum
3 = difference
What is 4 2 6?
Enter a number: 10
1 = product
2 = sum
3 = difference
What is 7 2 6?
Enter a number: 13
1 = product
2 = sum
3 = difference
What is 3 2 3?
Enter a number: 6
1 = product
2 = sum
3 = difference
What is 8 3 4?
Enter a number: 2
1 = product
2 = sum
3 = difference
What is 8 3 10?
Enter a number: 0.8
Incorrect Input!
Enter a number: .8
Incorrect Input!
Enter a number: 0
1 = product
2 = sum
3 = difference
What is 2 2 6?
Enter a number: 8
1 = product
2 = sum
3 = difference
What is 6 3 4?
Enter a number: 1.5
Incorrect Input!
Enter a number: 2
1 = product
2 = sum
3 = difference
What is 7 1 10?
Enter a number: 70
1 = product
2 = sum
3 = difference
What is 9 2 5?
Enter a number: 14
1 = product
2 = sum
3 = difference
What is 5 1 10?
Enter a number: 50
You got 8 correct out of 10
``````

EXPECTED OUTPUT:

``````[email protected]:/u1/work/Python/math> python3 mathquiz.py
What is 4 + 6?
Enter a number: 10
What is 7 + 6?
Enter a number: 13
What is 3 + 3?
Enter a number: 6
What is 8 / 4?
Enter a number: 2
What is 8 / 10?
Enter a number: 0.8
Incorrect Input!
Enter a number: .8
Incorrect Input!
Enter a number: 0
What is 2 + 6?
Enter a number: 8
What is 6 / 4?
Enter a number: 1.5
Incorrect Input!
Enter a number: 2
What is 7 * 10?
Enter a number: 70
What is 9 + 5?
Enter a number: 14
What is 5 * 10?
Enter a number: 50
You got 8 correct out of 10
``````

In this part of your code:

``````print(" 1 = product \n 2 = sum \n 3 = difference")
print("What is " + str(x)+" " + str(z)+" " + str(y)+"?")
``````

instead of `str(z)`, define a list like `ops = ['*', '+', '-']` and use `ops[z - 1]`. The `- 1` is because your `z` starts from 1, but array index starts from zero. So your function would become:

``````def askQuestion():
ops = ['*', '+', '-']
x = random.randint(1, 10)
y = random.randint(1, 10)
z = random.randint(1, 3)

print(" 1 = product \n 2 = sum \n 3 = difference")
print("What is " + str(x) + " " + ops[z - 1] + " " + str(y) + "?")