Python Exercises
1. Exercise 1
Design a program to help students practice addition within 10
Details:
- randomly generated addition topic;
- students view the subject and enter the answers;
- determine students answer correctly?
- When you exit, the total number of statistics students answer, the correct amount and the correct rate (Reserved two decimal places);
import random
count = 0
right = 0
while True:
num1 = random.randint(0, 10)
num2 = random.randint(0, 10)
print('题目:%d+%d= ' % (num1, num2))
answer = num1 + num2
answer1 = input('请输入你的答案(输入exit退出)')
if answer1 == str(answer):
right += 1
count += 1
print('你答对了')
elif answer1 == 'exit':
a = right / count
print("你共答了%d道题,正确%d道题,正确率为%.2f%% " %(count, right, a*100))
break
else:
print('你答错了')
count += 1
Output:
2. English II
Primary arithmetic capability test system:
design a program to help implement arithmetic pupils within one hundred practice,
it has the following functions:
to provide 10 to add, subtract, multiply or topic other four basic arithmetic operations;
practitioners of the display title input their answers;
the program automatically determines if the input answer is correct and display the corresponding information.
import random
op = ['+', '-', '*', '/']
i=0
for i in range(1,11):
a = random.choice(op)
num1=random.randint(0,100)
num2=random.randint(0,100)
print('%d%s%d='%(num1,a,num2))
if a == '+':
ans=num1+num2
if a == '-':
ans=num1-num2
if a == '*':
ans=num1*num2
if a == '/':
ans=num1/num2
ans1=int(input('输入你的答案:'))
if ans1==ans:
print('答案正确')
else:
print('答案错误,正确答案为%d' %(ans))
print('练习结束')
Output: