Я пытаюсь сделать Math Quiz с двумя случайными числами (1, 10) со случайным выбором с суммой, разницей или произведением.Я использовал z = random.randint(1, 3)
, чтобы сгенерировать сумму, разницу или произведение, но я хочу использовать эти числа для преобразования в знаки типа «x», «/» или «+», чтобы показать вывод, чтобы задать вопрос, потому что я новичок в языке Python иЯ пытаюсь узнать, как преобразовать числа в символы.
Мой код здесь:
import random
def askNum():
while(1):
try:
userInput = int(input("Enter a number: "))
break
except ValueError:
print("Incorrect Input!")
return userInput
def askQuestion():
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)+"?")
u = askNum()
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):
correct += askQuestion()
print("You got %d correct out of %d" % (correct, amount))
Вывод реальности:
dm15125@isu:/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
ОЖИДАЕМЫЙ ВЫХОД:
dm15125@isu:/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