您好我正在尝试创建一个脚本,为用户提供生成1-10个不同乘法问题的选项,以便他们回答但我遇到问题我相信我没有采用正确的方法使用此脚本如果有人可以帮助我,我会很感激!
from random import randint
random_number1 = randint(0, 12)
random_number2 = randint(0, 12)
sum = random_number1 * random_number2
key = int(input('How many problems do you want to solve? '))
if key == 1:
print(random_number1,'x',random_number2, '=', sum )
elif key == 2:
print(random_number1,'x',random_number2, '=', sum )
elif key == 3:
print(random_number1,'x',random_number2, '=', sum )
elif key == 4:
print(random_number1,'x',random_number2, '=', sum )
elif key == 5:
print(random_number1,'x',random_number2, '=', sum )
elif key == 6:
print(random_number1,'x',random_number2, '=', sum )
elif key == 7:
print(random_number1,'x',random_number2, '=', sum )
elif key == 8:
print(random_number1,'x',random_number2, '=', sum )
elif key == 9:
print(random_number1,'x',random_number2, '=', sum )
elif key == 10:
print(random_number1,'x',random_number2, '=', sum )
else:
print('Invalid Number')
答案 0 :(得分:2)
以下代码如何:
import random
upper_limit = 10
number_problems = int(input('How many problems do you want to solve? '))
for i in range(number_problems):
x, y = random.randint(1,upper_limit), random.randint(1,upper_limit)
true_ans = x*y
print(x ,'x', y , '=' )
ans = int(input('your answer:'))
if ans == true_ans:
print("correct!")
else:
print("incorrect! The answer is ", true_ans)
答案 1 :(得分:1)
不确定你试图做什么,但我猜你的意思是每次迭代都会生成不同的随机数(你要求的数字),那么这个呢?
from random import randint
MAX_PROBLEMS = 10
key = int(input('How many problems do you want to solve? '))
if key > 0 and key <= MAX_PROBLEMS:
for i in range(key):
random_number1 = randint(0, 12)
random_number2 = randint(0, 12)
print random_number1,'x',random_number2, '=', random_number1*random_number2
答案 2 :(得分:0)
这是一个类似你想要的东西。它介绍了python的一些不同特性:for
和while
循环,try
/ except
子句,断言错误。
尝试一下,玩它,研究代码并确保你理解每一点,然后回来问问题。祝你好运!
from random import randint
while True:
try:
key = int(input('How many problems do you want to solve?'))
break
except ValueError:
print('Invalid entry. Input an integer.')
continue
for k in range(key):
x, y = randint(0, 12), randint(0, 12)
while True:
try:
i = int(input('What is {0} times {1}?'.format(x, y)))
break
except ValueError:
print('Invalid entry. Input an integer.')
continue
assert i == x*y, "Incorrect! The correct answer is {0}, while you input {1}".format(x*y, i)
print("Well done! You answered all questions correctly!")
答案 3 :(得分:0)
这应该有效:
from random import randint
questions = int(input('How many problems do you want to solve? '))
# Since we want to ask multiple questions, this is a good case for using
# a for loop. Putting a number into the range() function will
# enable our for loop to repeat that number of times
for num in range(questions):
# To ensure that we make new random numbers each time, we have to put
# the following steps inside the for loop.
random_number1 = randint(0, 12)
random_number2 = randint(0, 12)
answer = random_number1 * random_number2
# Strings have a format method that allows us to create templates
# to allow us to input new values into the string every time
# the for loop cycles.
print("{} x {} = {}".format(r1, r2, answer))