10以内的加法
设计一个程序,帮助小学生练习10以内的加法
详情:
- 随机生成加法题目;
- 学生查看题目并输入答案;
- 判别学生答题是否正确?
- 退出时, 统计学生答题总数,正确数量及正确率(保留两位小数点);
import random
num = 0
for i in range(11):
x = random.randint(0,10)
y = random.randint(0,10)
print("%d+%d=" %(x,y),end='')
val=int(input())
if val == (x+y):
num+=1
print("true")
else:
print("false")
D=float(num/10)*100
print("percent:%.2f%%" %(D))
四则运算
小学生算术能力测试系统:
设计一个程序,用来实现帮助小学生进行百以内的算术练习,它具有以下功能:提供10道加、减、乘或除四种基本算术运算的题目;练习者根据显示的题目输入自己的答案,程序自动判断输入的答案是否正确并显示出相应的信息。
import random
op = ['+', '-', '*', '/']
count = 0
for i in range(1, 11):
s = random.choice(op)
if s == '+':
num1 = random.randint(0, 101)
num2 = random.randint(0, 101)
result = num1 + num2
print("the %d question: %d + %d" % (i, num1, num2), end='')
result1 = int(input(' = '))
print(result == result1)
if result == result1:
count += 1
elif s == '-':
num1 = random.randint(0, 101)
num2 = random.randint(0, 101)
maxnum = num1 if num1>num2 else num2
minnum = num1 if num1<num2 else num2
result = maxnum - minnum
print("the %d question: %d - %d" % (i, num1, num2), end='')
result1 = int(input(' = '))
print(result == result1)
if result == result1:
count += 1
elif s == '*':
num1 = random.randint(0, 11)
num2 = random.randint(0, 11)
result = num1 * num2
print("the %d question: %d * %d" % (i, num1, num2), end=':')
result1 = int(input(' = '))
print(result == result1)
if result == result1:
count += 1
else:
num1 = random.randint(0, 11)
num2 = random.randint(1, 11)
result = int(num1 / num2)
print("the %d question: %d / %d" % (i, num1, num2), end='')
result1 = int(input(' = '))
print(result == result1)
if result == result1:
count += 1
print(count)
percent = (count / 10) * 100
print('right percent = %.2f%%' % (percent))
算法之指定范围内判断
1). 若一个正整数n的所有小于n的因子之和等于n, 则称n为完全数, 如6=1+2+3是完全数。判断正整数n是否为完全数。
num = int(input("请输入你所要判断的数:"))
spectors=[]
sum = 0
for i in range(1,num):
if (num % i) ==0:
spectors.append(i)
sum = sum + i
if sum == num:
print("%s is a perfect number" %(num))
print("%s have factors:" %(num),end=':' )
print(spectors[:])
else:
print('%s is not a perfect number' % (num))
2). 输入一个正整数,判断其是否为一个素数
num2=int(input('please input a number:'))
valse=[]
for i in range(1,num2+1):
if (num2 % i) == 0:
valse.append(i)
if valse==[1,num2]:
print('%s is a prime number' %(num2))
else:
print("%s is not a prime number" %(num2))
3). 输入一个正整数,判断其是否为一个回文数(例1221、12321都是回文数);
num3=input('please input a number:')
if num3[:]==num3[::-1]:
print('%s is a palindromic number' %(num3))
else:
print('%s is not a palindromic number' %(num3))
累加和类型程序:
1). 求f=1+2+...+9【很简单的问题】
num = 0
for i in range(10):
num = i +num
print(num)
2). 求f=1-2+3-4+...+9【在(一)的基础上,学习如何一正一负地累加】
num = 0
for i in range(1,10,2):
num = i +num
for i in range(2,10,2):
num = num -i
print(num)
3). 求f=1-1/2+1/3-1/4+...+1/9【在(二)的基础上,被加数成了自然数的倒数】
num = 0
for i in range(1,10,2):
num = (1/i) +num
for i in range(2,10,2):
num = num -(1/i)
print(num)
4). 求f=1!-2!+3!-4!+...+9!【在(二)的基础上,被加数成了自然数的阶乘】
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。