Python 数学测验随机数
Python maths quiz random number
你好,我正在尝试为随机数学测验生成器编写代码,我有它,所以它随机生成数字和操作,但我无法让它重复 10 次,因为我想让它问 10 个问题,有人可以帮忙吗请这是我的代码
import random
import time
name=input("What is your name?")
print ("Alright",name,"Welcome to your maths quiz")
score=0
question=0
finish= False
ops = ['+', '-', '*']
rand=random.randint(1,10)
rand2=random.randint(1,10)
operation = random.choice(ops)
maths = eval(str(rand) + operation + str(rand2))
print ("Your first question is",rand,operation,rand2)
question=question+1
d=int(input ("What is your answer:"))
if d==maths:
print ("Correct")
score=score+1
else:
print ("Incorrect. The actual answer is",maths)
使用 while 带条件的循环。
算法:
- 将
counter
设置为 0
。
- 使用
while
循环并检查counter
是否小于10
。
- 向用户提问。
- 做你的计算。
- 将
counter
增加一。
- 当
counter
等于10
时,那个时候condition
就是False
。
演示:
>>> counter = 0
>>> while counter<10:
... que = raw_input("Enter que:")
... print que
... counter += 1
...
Enter que:1
1
Enter que:2
2
Enter que:3
3
Enter que:4
4
Enter que:5
5
Enter que:6
6
Enter que:7
7
Enter que:8
8
Enter que:9
9
Enter que:10
10
>>>
使用 for
循环:
for num in range(5):
# Replace "print" below, with the code you want to repeat.
print(num)
重复所有问题,不包括"whats your name.."
在循环中包含您需要的部分代码:
import random
name=input("What is your name?")
print ("Alright",name,"Welcome to your maths quiz")
score=0
for question_num in range(1, 11):
ops = ['+', '-', '*']
rand=random.randint(1,10)
rand2=random.randint(1,10)
operation = random.choice(ops)
maths = eval(str(rand) + operation + str(rand2))
print('\nQuestion number: {}'.format(question_num))
print ("The question is",rand,operation,rand2)
d=int(input ("What is your answer:"))
if d==maths:
print ("Correct")
score=score+1
else:
print ("Incorrect. The actual answer is",maths)
你好,我正在尝试为随机数学测验生成器编写代码,我有它,所以它随机生成数字和操作,但我无法让它重复 10 次,因为我想让它问 10 个问题,有人可以帮忙吗请这是我的代码
import random
import time
name=input("What is your name?")
print ("Alright",name,"Welcome to your maths quiz")
score=0
question=0
finish= False
ops = ['+', '-', '*']
rand=random.randint(1,10)
rand2=random.randint(1,10)
operation = random.choice(ops)
maths = eval(str(rand) + operation + str(rand2))
print ("Your first question is",rand,operation,rand2)
question=question+1
d=int(input ("What is your answer:"))
if d==maths:
print ("Correct")
score=score+1
else:
print ("Incorrect. The actual answer is",maths)
使用 while 带条件的循环。
算法:
- 将
counter
设置为0
。 - 使用
while
循环并检查counter
是否小于10
。 - 向用户提问。
- 做你的计算。
- 将
counter
增加一。 - 当
counter
等于10
时,那个时候condition
就是False
。
演示:
>>> counter = 0
>>> while counter<10:
... que = raw_input("Enter que:")
... print que
... counter += 1
...
Enter que:1
1
Enter que:2
2
Enter que:3
3
Enter que:4
4
Enter que:5
5
Enter que:6
6
Enter que:7
7
Enter que:8
8
Enter que:9
9
Enter que:10
10
>>>
使用 for
循环:
for num in range(5):
# Replace "print" below, with the code you want to repeat.
print(num)
重复所有问题,不包括"whats your name.."
在循环中包含您需要的部分代码:
import random
name=input("What is your name?")
print ("Alright",name,"Welcome to your maths quiz")
score=0
for question_num in range(1, 11):
ops = ['+', '-', '*']
rand=random.randint(1,10)
rand2=random.randint(1,10)
operation = random.choice(ops)
maths = eval(str(rand) + operation + str(rand2))
print('\nQuestion number: {}'.format(question_num))
print ("The question is",rand,operation,rand2)
d=int(input ("What is your answer:"))
if d==maths:
print ("Correct")
score=score+1
else:
print ("Incorrect. The actual answer is",maths)