鱼C论坛

 找回密码
 立即注册
查看: 778|回复: 1

[作品展示] 看看看看,真的不赖!

[复制链接]
发表于 2020-3-26 13:52:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 陈尚涵 于 2020-3-26 13:56 编辑

大家好,我做了计算的程序,就是有个缺点 ,不管什么算式都是从左到右的-_-。
代码如下:
  1. #由于需要使用到random函数,所以先导入进来-_-
  2. import random
  3. def main1(num1,num2,a,b):
  4.     """定义第一个主要部分
  5.     num1和num2是随机数的两个对应的随机数
  6.     a是加减乘除的类型序号,b是是否多位数的类型序号"""
  7.     if b == 1:
  8.         le = 3
  9.     else:
  10.         le = 5
  11.     li = []
  12.     if a == 1:
  13.         h1 = 1
  14.         h2 = 2
  15.     elif a == 2:
  16.         h1 = 3
  17.         h2 = 4
  18.     else:
  19.         h1 = 1
  20.         h2 = 4
  21.     for i in range(le-b):
  22.         li.append(random.randint(num1,num2))
  23.     li.append("|")
  24.     for i in range(b):
  25.         li.append(random.randint(h1,h2))
  26.     li.append("|")
  27.     if le == 3:
  28.         if li[3] == 1:
  29.             li[3] = "+"
  30.         if li[3] == 2:
  31.             li[3] = "-"
  32.         if li[3] == 3:
  33.             li[3] = "*"
  34.         if li[3] == 4:
  35.             li[3] = "/"
  36.         li.append(input("{0}{1}{2}=".format(str(li[0]),str(li[3]),str(li[1]))))
  37.     else:
  38.         if li[4] == 1:
  39.             li[4] = "+"
  40.         if li[4] == 2:
  41.             li[4] = "-"
  42.         if li[4] == 3:
  43.             li[4] = "*"
  44.         if li[4] == 4:
  45.             li[4] = "/"
  46.         if li[5] == 1:
  47.             li[5] = "+"
  48.         if li[5] == 2:
  49.             li[5] = "-"
  50.         if li[5] == 3:
  51.             li[5] = "*"
  52.         if li[5] == 4:
  53.             li[5] = "/"
  54.         li.append(input("{0}{1}{2}{3}{4}=".format(li[0],li[4],li[1],li[5],li[2])))
  55.     li.append("|")
  56.     li.append(le)
  57.     return li
  58. def main2(num1,num2,typed):
  59.     """定义第二个主要部分
  60.     num1和num2是随机数的两个对应的随机数
  61.     typed则是类型,a是加减,b是乘除,c是加减乘除,此外,如果再加上一个'd',那就不是两个数一个符号,而是三个数两个符号"""
  62.     if typed.find("a") != -1:
  63.         if typed == "ad":
  64.             return main1(num1,num2,1,2)
  65.         else:
  66.             return main1(num1,num2,1,1)
  67.     if typed.find("b") != -1:
  68.         if typed == "bd":
  69.             return main1(num1,num2,2,2)
  70.         else:
  71.             return main1(num1,num2,2,1)
  72.     if typed.find("c") != -1:
  73.         if typed == "cd":
  74.             return main1(num1,num2,3,2)
  75.         else:
  76.             return main1(num1,num2,3,1)
  77. def check(*num,result):
  78.     """定义检查部分"""
  79.     true_result = 0
  80.     if len(num) == 3:
  81.         h = [num[0],num[1]]
  82.         if num[2] == "+":
  83.             true_result = h[0] + h[1]
  84.         if num[2] == "-":
  85.             true_result = h[0] - h[1]
  86.         if num[2] == "*":
  87.             true_result = h[0] * h[1]
  88.         if num[2] == "/":
  89.             true_result = h[0] / h[1]
  90.     else:
  91.         h = [num[0],num[1]]
  92.         if num[3] == "+":
  93.             true_result = h[0] + h[1]
  94.         if num[3] == "-":
  95.             true_result = h[0] - h[1]
  96.         if num[3] == "*":
  97.             true_result = h[0] * h[1]
  98.         if num[3] == "/":
  99.             true_result = h[0] / h[1]
  100.         if num[4] == "+":
  101.             true_result = true_result + num[2]
  102.         if num[4] == "-":
  103.             true_result = true_result - num[2]
  104.         if num[4] == "*":
  105.             true_result = true_result * num[2]
  106.         if num[4] == "/":
  107.             true_result = true_result / num[2]
  108.     if true_result == int(result):
  109.         print("做对啦!")
  110.         return 1
  111.     else:
  112.         print("不对哦。答案应该是:"+str(true_result))
  113.         return 0
  114. def others(_list):
  115.     """定义一个能帮助下面的doing函数的函数"""
  116.     #两个数一个符号的li列表的实例:["1","1","|","+","|","2","|","3"]
  117.     #三个数两个符号的li列表的实例:["1","1","1","|","+","-","|","3","|","5"]
  118.     if len(_list) == 8:
  119.         return check(_list[0],_list[1],_list[3],result=_list[5])
  120.     else:
  121.         return check(_list[0],_list[1],_list[2],_list[4],_list[5],result=_list[7])
  122. def doing():
  123.     """定义一个可以给用户体验的一个函数"""
  124.     print("这是一个可以考验你的计算能力的程序。所有的算式都是从左往右的。你准备好了吗?")
  125.     l1 = [1,1,20,30,30,100,215,400]
  126.     l2 = [10,9,50,100,100,300,500,1000]
  127.     l3 = ["a","bd","a","c","cd","b","cd","c"]
  128.     score = 20
  129.     for i in range(8):
  130.         while True:
  131.             if others(main2(l1[i+1],l2[i+1],l3[i+1])) == 1:
  132.                 score += 1
  133.             else:
  134.                 score -= 2
  135.             if score > 30 or score < 0:
  136.                 break
  137.         if score >= 30:
  138.             if i == 7:
  139.                 print("您已挑战成功!您的计算能力已经很强了。")
  140.                 break
  141.             else:
  142.                 print("您已通关!")
  143.         else:
  144.             print("您已挑战失败。您在第%s关被终止。" % str(i+1))
  145.             break
  146. doing()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-3-26 13:56:25 | 显示全部楼层
输入小数报错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 04:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表