(1)打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位的十进制数,其各位数字立方和等于该数本身。例如:153是一个“水仙花数”,因为153=13+53+33
>>> s=100
>>> e=999
>>> while(s<=e):
a=s%10
b=s//10%10
c=s//100
if(s==(a**3+b**3+c**3)):
print(s)
s+=1
(2)编写小学生100以内加减法训练程序,并在学生结束测验后能报告正确答案的个数、正确率、测验所用的时间,并能让用户自己决定随时结束测验。。
import random
import time
start=time.time()
count = 0
right = 0
op = ['+', '-']
while True:
a = random.randint(0, 99)
b = random.randint(0, 99)
s = random.choice(op)
print('%d %s %d' % (a, s, b))
question = input('请输入你的答案:(输入exit退出)')
if s == '+':
result = a + b
elif s == '-':
result = a - b
elif s == '*':
result = a * b
elif s == '/':
result = a / b
if question == str(result):
print('回答正确')
right += 1
count += 1
elif question == 'exit':
percent = right / count
print('测试结束,共回答%d道题,正确个数为%d,正确率为%.2f%%'
% (count, right, percent * 100))
end=time.time()
print('共用时%.2fs'% (end-start))
break
else:
print('回答错误')
count += 1
|