|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 陈尚涵 于 2020-3-26 13:56 编辑
大家好,我做了计算的程序 ,就是有个缺点 ,不管什么算式都是从左到右的-_-。
代码如下:
- #由于需要使用到random函数,所以先导入进来-_-
- import random
- def main1(num1,num2,a,b):
- """定义第一个主要部分
- num1和num2是随机数的两个对应的随机数
- a是加减乘除的类型序号,b是是否多位数的类型序号"""
- if b == 1:
- le = 3
- else:
- le = 5
- li = []
- if a == 1:
- h1 = 1
- h2 = 2
- elif a == 2:
- h1 = 3
- h2 = 4
- else:
- h1 = 1
- h2 = 4
- for i in range(le-b):
- li.append(random.randint(num1,num2))
- li.append("|")
- for i in range(b):
- li.append(random.randint(h1,h2))
- li.append("|")
- if le == 3:
- if li[3] == 1:
- li[3] = "+"
- if li[3] == 2:
- li[3] = "-"
- if li[3] == 3:
- li[3] = "*"
- if li[3] == 4:
- li[3] = "/"
- li.append(input("{0}{1}{2}=".format(str(li[0]),str(li[3]),str(li[1]))))
- else:
- if li[4] == 1:
- li[4] = "+"
- if li[4] == 2:
- li[4] = "-"
- if li[4] == 3:
- li[4] = "*"
- if li[4] == 4:
- li[4] = "/"
- if li[5] == 1:
- li[5] = "+"
- if li[5] == 2:
- li[5] = "-"
- if li[5] == 3:
- li[5] = "*"
- if li[5] == 4:
- li[5] = "/"
- li.append(input("{0}{1}{2}{3}{4}=".format(li[0],li[4],li[1],li[5],li[2])))
- li.append("|")
- li.append(le)
- return li
- def main2(num1,num2,typed):
- """定义第二个主要部分
- num1和num2是随机数的两个对应的随机数
- typed则是类型,a是加减,b是乘除,c是加减乘除,此外,如果再加上一个'd',那就不是两个数一个符号,而是三个数两个符号"""
- if typed.find("a") != -1:
- if typed == "ad":
- return main1(num1,num2,1,2)
- else:
- return main1(num1,num2,1,1)
- if typed.find("b") != -1:
- if typed == "bd":
- return main1(num1,num2,2,2)
- else:
- return main1(num1,num2,2,1)
- if typed.find("c") != -1:
- if typed == "cd":
- return main1(num1,num2,3,2)
- else:
- return main1(num1,num2,3,1)
- def check(*num,result):
- """定义检查部分"""
- true_result = 0
- if len(num) == 3:
- h = [num[0],num[1]]
- if num[2] == "+":
- true_result = h[0] + h[1]
- if num[2] == "-":
- true_result = h[0] - h[1]
- if num[2] == "*":
- true_result = h[0] * h[1]
- if num[2] == "/":
- true_result = h[0] / h[1]
- else:
- h = [num[0],num[1]]
- if num[3] == "+":
- true_result = h[0] + h[1]
- if num[3] == "-":
- true_result = h[0] - h[1]
- if num[3] == "*":
- true_result = h[0] * h[1]
- if num[3] == "/":
- true_result = h[0] / h[1]
- if num[4] == "+":
- true_result = true_result + num[2]
- if num[4] == "-":
- true_result = true_result - num[2]
- if num[4] == "*":
- true_result = true_result * num[2]
- if num[4] == "/":
- true_result = true_result / num[2]
- if true_result == int(result):
- print("做对啦!")
- return 1
- else:
- print("不对哦。答案应该是:"+str(true_result))
- return 0
- def others(_list):
- """定义一个能帮助下面的doing函数的函数"""
- #两个数一个符号的li列表的实例:["1","1","|","+","|","2","|","3"]
- #三个数两个符号的li列表的实例:["1","1","1","|","+","-","|","3","|","5"]
- if len(_list) == 8:
- return check(_list[0],_list[1],_list[3],result=_list[5])
- else:
- return check(_list[0],_list[1],_list[2],_list[4],_list[5],result=_list[7])
- def doing():
- """定义一个可以给用户体验的一个函数"""
- print("这是一个可以考验你的计算能力的程序。所有的算式都是从左往右的。你准备好了吗?")
- l1 = [1,1,20,30,30,100,215,400]
- l2 = [10,9,50,100,100,300,500,1000]
- l3 = ["a","bd","a","c","cd","b","cd","c"]
- score = 20
- for i in range(8):
- while True:
- if others(main2(l1[i+1],l2[i+1],l3[i+1])) == 1:
- score += 1
- else:
- score -= 2
- if score > 30 or score < 0:
- break
- if score >= 30:
- if i == 7:
- print("您已挑战成功!您的计算能力已经很强了。")
- break
- else:
- print("您已通关!")
- else:
- print("您已挑战失败。您在第%s关被终止。" % str(i+1))
- break
- doing()
复制代码 |
|