| import random#随机数模块 defen=0#变量defen为0 print("24数游戏,不知道规则请先查询,得分达到5获胜") while defen < 5:#得分到达5获胜     a=random.randrange(1,10)     b=random.randrange(1,10)     c=random.randrange(1,10)     d=random.randrange(1,10)#变量abcd均为1到10的随机数     e=[a,b,c,d]#创建列表e     while 1 == 1:#死循环         print(e,"目前数字")         f=int(input("请输入要调用的第一个数字:"))#输入的第一个数字为f         if e.count(f) < 1:#f如果不在列表里             print("你输入的数字不在里面,请重新输入")             continue#重新输入         g=int(input("请输入要调用的第二个数字,若将第一个数字单独进行运算请输入11:"))#输入的第二个数字为g并询问是否要将f单独运算         if e.count(g) < 1:#g如果不在列表里             if g != 11:#如果g不是11                 print("你输入的数字不在里面,请重新输入")                 continue#重新输入             else:#如果g是11                 l=int(input("要将第一个数字怎么运算?根号输入1,阶乘输入2:"))                 if l == 1:#要进行根号运算                     #检测f是不是1,4,9,是则运算,不是则重新开始                     if f == 1:#如果f是1                         print("根号1等于1")                         continue#运算结束                     elif f == 4:#如果f是4                         print("根号4等于2")                         e.remove(4)#删除4                         e.append(2)#添加2                         continue#运算结束                     elif f == 9:#如果f是9                         print("根号9等于3")                         e.remove(9)#删除9                         e.append(3)#添加3                         continue#运算结束                     else:#f不是1,4,9,根号结果不是整数                         print(f,"根号的结果不是整数,错误!")                         continue#重新输入                                       if l == 2:#要进行阶乘运算                     n=e.index(f)#由于后面会改变f,提前用n储存他的位置                     m=f#阶乘运算                     while f > 1:                         m=m*(f-1)                         f=f-1                     e.pop(n)#删除该数字所在位置                     e.append(m)#添加阶乘后的数字                     print("已将",m,"添加")                     continue#重新输入                 if l != 1:                     if l != 2:#如果l不是1也不是2                         print("错误,重新计算")         h=int(input("要把它们怎么运算?加输入1,减输入2,乘输入3,除输入4,重新计算输入5:"))#加减乘除运算         if h == 1:             i=f+g#两数相加         if h == 2:             i=f-g#两数相减         if h == 3:             i=f*g#两数相乘         if h == 4:             if f%g == 0:#g是否可以被整除                 i=f//g#两数相除             else:#如果不能被整除                 print("两个数不能整除,请重新输入")                 continue#重新输入         if h == 5:             print("重新计算")             e=[a,b,c,d]#重新定义列表e,重新开始游戏             continue         e.remove(f)#删除列表里的f         e.remove(g)#删除列表里的g         e.append(i)#添加两数的计算结果         if len(e) == 1:#如果列表长度为1             if e.count(24) == 1:#且数字24出现的次数为1                 defen=defen+1#得分加一                 j=1#获胜                 break#退出死循环             else:#数字24没有出现                 j=0#失败                 break#退出死循环     if j == 1:#如果获胜         print("获胜!您目前的得分为",defen)         print("开启新的一局")     else:#如果失败         print("失败!你目前的得分为",defen)         print("开启新的一局") print("得分已经达到5,您获胜了!")#得分达到5,游戏获胜 |