标签:elif 四则运算 randint 小学 random result n1 n2
运行截图如下:
代码如下:
import random #四则运算def t(): sym = ['+', '-', '×', '÷'] f= random.randint(0, 3) n1 = random.randint(1, 20) n2 = random.randint(1, 20) result = 0 if f== 0:#加法 result = n1 + n2 elif f == 1:#减法,要先比较大小,防止输出负数 n1, n2 = max(n1, n2), min(n1, n2) result = n1 - n2 elif f== 2:#乘法 result = n1 * n2 elif f == 3:#除法,要比较大小,并循环取整除 n1, n2 = max(n1, n2), min(n1, n2) while n1 % n2 != 0: n1 = random.randint(1, 10) n2 = random.randint(1, 10) n1, n2 = max(n1, n2), min(n1, n2) result = int(n1 / n2) print(n1, sym[f], n2, '= ', end='') return result #制作题库 def test():
print('输入所需要的题目数量') n=int(input()) result =[] m=0 while m<=(n-1): print(m+1,end='、') result .append(t()) print(' ') m=m+1 m=0 print('对应的答案:') while m<=(n-1): print(m+1,'、',result [m]) m=m+1 print('选择想要的模式') print('1、进行四则运算') print('2、制作题库') n=int(input()) #当输入1时,进行四则运算,调用函数t() if n==1: while True: result = t() j= input() s= int(j) if s== result : print('right') else: print('error.,the answer is', result ) #当输入2时,进行制作题库 if n==2: test()
参考:https://www.cnblogs.com/zhouxiaochun/p/10567992.html
标签:elif,四则运算,randint,小学,random,result,n1,n2 来源: https://www.cnblogs.com/iris2333/p/14056523.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。