鱼C论坛

 找回密码
 立即注册
查看: 1495|回复: 4

不知道什么原因陷入死循环出不来

[复制链接]
发表于 2023-5-24 23:39:57 | 显示全部楼层 |阅读模式

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

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

x
  1. import random as r
  2. import pickle

  3. admin_energy = 1  # 我方初始能量
  4. ai_energy = 1  # 敌方初始能量
  5. admin_action = ""  # 我方行动


  6. def player_action():
  7.     global admin_action, admin_energy, ai_energy
  8.     print("这是你的回合")
  9.     print("你的运有%s" % admin_energy)
  10.     while True:
  11.         admin_action = input('请输入您的手势')
  12.         if admin_action == '运':
  13.             admin_energy += 1
  14.             break
  15.         elif admin_action == '攻':
  16.             if admin_energy >= 1:
  17.                 admin_energy -= 1
  18.                 break
  19.             else:
  20.                 print('能量不足,请重新输入')
  21.         elif admin_action == '防':
  22.             break
  23.         elif admin_action == '小防':
  24.             break
  25.         elif admin_action == '小波':
  26.             if admin_energy >= 3:
  27.                 admin_energy -= 3
  28.                 break
  29.             else:
  30.                 print('能量不足,请重新输入')
  31.         elif admin_action == '大防':
  32.             if admin_energy >= 1:
  33.                 admin_energy -= 1
  34.                 break
  35.             else:
  36.                 print('能量不足,请重新输入')
  37.         elif admin_action == '大波':
  38.             if admin_energy >= 5:
  39.                 admin_energy -= 5
  40.                 break
  41.             else:
  42.                 print('能量不足,请重新输入')
  43.         else:
  44.             print('输入错误请重新输入')


  45. def Pc_action(string):
  46.     action = ["运", "攻", "防", "小波", "大波", "大防"]
  47.     weight = Ai_Get_Data(string)  # 从“人工智能获取数据”函数里,得到权重的值
  48.     print("Ai_action已运行!action为")
  49.     temp = r.choices(action, weights=weight)
  50.     print(temp)
  51.     return temp


  52. def Ai_Get_Data(string):  # 传参string,如果人类和AI运都为1,则传OneOne,如果人为1 AI为2,则传OneTwo,以此类推
  53.     file = open(r"AiLearn.txt", 'rb')
  54.     print("打开文件成功")
  55.     ai_data = []
  56.     print("正在获取Ai数据……")
  57.     print(getData)
  58.     if string == "OneOne":  # 检测输入字符串是否为标识查找符,3运以上全归为三运
  59.         OneOne_ai_data = pickle.load(file)
  60.         ai_data = OneOne_ai_data
  61.     elif string == "OneTwo":
  62.         OneTwo_ai_data = pickle.load(file)
  63.         ai_data = OneTwo_ai_data
  64.     elif string == "OneThree":
  65.         OneThree_ai_data = pickle.load(file)
  66.         ai_data = OneThree_ai_data
  67.     elif string == "TwoOne":
  68.         TwoOne_ai_data = pickle.load(file)
  69.         ai_data = TwoOne_ai_data
  70.     elif string == "TwoTwo":
  71.         TwoTwo_ai_data = pickle.load(file)
  72.         ai_data = TwoTwo_ai_data
  73.     elif string == "TwoThree":
  74.         TwoThree_ai_data = pickle.load(file)
  75.         ai_data = TwoThree_ai_data
  76.     elif string == "ThreeOne":
  77.         ThreeOne_ai_data = pickle.load(file)
  78.         ai_data = ThreeOne_ai_data
  79.     elif string == "ThreeTwo":
  80.         ThreeTwo_ai_data = pickle.load(file)
  81.         ai_data = ThreeTwo_ai_data
  82.     elif string == "ThreeThree":
  83.         ThreeThree_ai_data = pickle.load(file)
  84.         ai_data = ThreeThree_ai_data
  85.     file.close()
  86.     print("获取的ai的data为")
  87.     print(ai_data)
  88.     return ai_data


  89. ''' # 弃用该方法
  90.     ai_data = []
  91.     f = open(r"AiLearn.txt")
  92. for each in f.readline():  # 遍历所有权重的值
  93.         if len(string) == 6:
  94.             if each[:6] == string:  # 如果该标识查找符在该行
  95.                 each = each.replace(string, '')  # 去除标识查找符
  96.                 ai_data = ai_data + each.split(',')  # 以逗号分开,将数据变成列表
  97.         elif len(string) == 8:
  98.             if each[:8] == string:
  99.                 each = each.replace(string, '')  # 去除标识查找符
  100.                 ai_data = ai_data + each.split(',')  # 以逗号分开,将数据变成列表
  101.         elif len(string) == 10:
  102.             if each[:10] == string:
  103.                 each = each.replace(string, '')  # 去除标识查找符
  104.                 ai_data = ai_data + each.split(',')  # 以逗号分开,将数据变成列表
  105. '''


  106. def Ai_Learn(ai_action_number):
  107.     file = open(r"AiLearn.txt", "rb")
  108.     OneOne_ai_data = pickle.load(file)
  109.     OneTwo_ai_data = pickle.load(file)
  110.     OneThree_ai_data = pickle.load(file)
  111.     TwoOne_ai_data = pickle.load(file)
  112.     TwoTwo_ai_data = pickle.load(file)
  113.     TwoThree_ai_data = pickle.load(file)
  114.     ThreeOne_ai_data = pickle.load(file)
  115.     ThreeTwo_ai_data = pickle.load(file)
  116.     ThreeThree_ai_data = pickle.load(file)
  117.     print("Learn函数中已加载Data")
  118.     file.close()
  119.     file = open(r"AiLearn.txt", 'ab')
  120.     if getData == "OneOne":  # 检测输入字符串是否为标识查找符,3运以上全归为三运
  121.         OneOne_ai_data[ai_action_number] = OneOne_ai_data[ai_action_number] + 1  # 每个number代表action在该数组的索引值
  122.         pickle.dump(OneOne_ai_data, file)
  123.     elif getData == "OneTwo":
  124.         OneTwo_ai_data[ai_action_number] = OneTwo_ai_data[ai_action_number] + 1  # 每个number代表action在该数组的索引值
  125.         pickle.dump(OneTwo_ai_data, file)
  126.     elif getData == "OneThree":
  127.         OneThree_ai_data[ai_action_number] = OneThree_ai_data[ai_action_number] + 1  # 每个number代表action在该数组的索引值
  128.         pickle.dump(OneThree_ai_data, file)
  129.     elif getData == "TwoOne":
  130.         TwoOne_ai_data[ai_action_number] = TwoOne_ai_data[ai_action_number] + 1  # 每个number代表action在该数组的索引值
  131.         pickle.dump(TwoOne_ai_data, file)
  132.     elif getData == "TwoTwo":
  133.         TwoTwo_ai_data[ai_action_number] = TwoTwo_ai_data[ai_action_number] + 1  # 每个number代表action在该数组的索引值
  134.         pickle.dump(TwoTwo_ai_data, file)
  135.     elif getData == "TwoThree":
  136.         TwoThree_ai_data[ai_action_number] = TwoThree_ai_data[ai_action_number] + 1  # 每个number代表action在该数组的索引值
  137.         pickle.dump(TwoThree_ai_data, file)
  138.     elif getData == "ThreeOne":
  139.         ThreeOne_ai_data[ai_action_number] = ThreeOne_ai_data[ai_action_number] + 1  # 每个number代表action在该数组的索引值
  140.         pickle.dump(ThreeOne_ai_data, file)
  141.     elif getData == "ThreeTwo":
  142.         ThreeTwo_ai_data[ai_action_number] = ThreeTwo_ai_data[ai_action_number] + 1  # 每个number代表action在该数组的索引值
  143.         pickle.dump(ThreeTwo_ai_data, file)
  144.     elif getData == "ThreeThree":
  145.         ThreeThree_ai_data[ai_action_number] = ThreeThree_ai_data[ai_action_number] + 1  # 每个number代表action在该数组的索引值
  146.         pickle.dump(ThreeThree_ai_data, file)
  147.     file.close()


  148. ''' # 弃用该方法
  149.     ai_alldata = file.read()
  150.     ai_data_update[ai_action_number] = ai_data[ai_action_number] + 1  # 每个number代表action在该数组的索引值
  151.     for each in ai_alldata:
  152.         if getData in each:
  153.             ai_alldata.replace("%s%s" % (getdata, aidata), "%s%s" % (getdata, ai_data_update))
  154.     file.close()
  155. '''


  156. def stringTranslation(num):
  157.     trans = ""
  158.     if num <= 1:
  159.         trans = "One"
  160.     elif num == 2:
  161.         trans = "Two"
  162.     elif num >= 3:
  163.         trans = "Three"
  164.     return trans


  165. def DelE():
  166.     global admin_energy, ai_energy
  167.     admin_energy = 0
  168.     ai_energy = 0


  169. def compete(pc_action):
  170.     if admin_action == "运" and pc_action == ["运"]:  # action = ["运", "攻", "防", "小波", "大波", "大防"]
  171.         Ai_Learn(0)
  172.         print("平")
  173.     elif admin_action == "运" and pc_action == ["攻"]:
  174.         Ai_Learn(1)
  175.         print("Ai胜")
  176.         DelE()
  177.     elif admin_action == "运" and pc_action == ["防"]:
  178.         Ai_Learn(2)
  179.         print("平")
  180.     elif admin_action == "运" and pc_action == ["小波"]:
  181.         Ai_Learn(3)
  182.         print("Ai胜")
  183.         DelE()
  184.     elif admin_action == "运" and pc_action == ["大波"]:
  185.         Ai_Learn(4)
  186.         print("Ai胜")
  187.         DelE()
  188.     elif admin_action == "运" and pc_action == ["大防"]:
  189.         Ai_Learn(5)
  190.         print("平")
  191.         
  192.     elif admin_action == "攻" and pc_action == ["运"]:  # action = ["运", "攻", "防", "小波", "大波", "大防"]
  193.         print("人类胜利")
  194.         DelE()
  195.     elif admin_action == "攻" and pc_action == ["攻"]:
  196.         Ai_Learn(1)
  197.         print("平")
  198.     elif admin_action == "攻" and pc_action == ["防"]:
  199.         Ai_Learn(2)
  200.         print("平")
  201.     elif admin_action == "攻" and pc_action == ["小波"]:
  202.         Ai_Learn(3)
  203.         print("平")
  204.     elif admin_action == "攻" and pc_action == ["大波"]:
  205.         Ai_Learn(4)
  206.         print("平")
  207.     elif admin_action == "攻" and pc_action == ["大防"]:
  208.         Ai_Learn(5)
  209.         print("平")

  210.     elif admin_action == "防" and pc_action == ["运"]:  # action = ["运", "攻", "防", "小波", "大波", "大防"]
  211.         Ai_Learn(0)
  212.         print("平")
  213.     elif admin_action == "防" and pc_action == ["攻"]:
  214.         Ai_Learn(1)
  215.         print("平")
  216.     elif admin_action == "防" and pc_action == ["防"]:
  217.         Ai_Learn(2)
  218.         print("平")
  219.     elif admin_action == "防" and pc_action == ["小波"]:
  220.         Ai_Learn(3)
  221.         print("Ai胜利")
  222.         DelE()
  223.     elif admin_action == "防" and pc_action == ["大波"]:
  224.         Ai_Learn(4)
  225.         print("Ai胜利")
  226.         DelE()
  227.     elif admin_action == "防" and pc_action == ["大防"]:
  228.         Ai_Learn(5)
  229.         print("平")

  230.     elif admin_action == "小波" and pc_action == ["运"]:  # action = ["运", "攻", "防", "小波", "大波", "大防"]
  231.         print("人类胜利")
  232.         DelE()
  233.     elif admin_action == "小波" and pc_action == ["攻"]:
  234.         Ai_Learn(1)
  235.         print("平")
  236.     elif admin_action == "小波" and pc_action == ["防"]:
  237.         print("人类胜利")
  238.         DelE()
  239.     elif admin_action == "小波" and pc_action == ["小波"]:
  240.         Ai_Learn(3)
  241.         print("平")
  242.     elif admin_action == "小波" and pc_action == ["大波"]:
  243.         Ai_Learn(4)
  244.         print("平")
  245.     elif admin_action == "小波" and pc_action == ["大防"]:
  246.         Ai_Learn(5)
  247.         print("平")

  248.     elif admin_action == "大波" and pc_action == ["运"]:  # action = ["运", "攻", "防", "小波", "大波", "大防"]
  249.         print("人类胜利")
  250.         DelE()
  251.     elif admin_action == "大波" and pc_action == ["攻"]:
  252.         Ai_Learn(1)
  253.         print("平")
  254.     elif admin_action == "大波" and pc_action == ["防"]:
  255.         print("人类胜利")
  256.         DelE()
  257.     elif admin_action == "大波" and pc_action == ["小波"]:
  258.         Ai_Learn(3)
  259.         print("平")
  260.     elif admin_action == "大波" and pc_action == ["大波"]:
  261.         Ai_Learn(4)
  262.         print("平")
  263.     elif admin_action == "大波" and pc_action == ["大防"]:
  264.         print("人类胜利")
  265.         DelE()

  266.     elif admin_action == "大防" and pc_action == ["运"]:  # action = ["运", "攻", "防", "小波", "大波", "大防"]
  267.         Ai_Learn(0)
  268.         print("平")
  269.     elif admin_action == "大防" and pc_action == ["攻"]:
  270.         Ai_Learn(1)
  271.         print("平")
  272.     elif admin_action == "大防" and pc_action == ["防"]:
  273.         Ai_Learn(2)
  274.         print("平")
  275.     elif admin_action == "大防" and pc_action == ["小波"]:
  276.         Ai_Learn(3)
  277.         print("平")
  278.     elif admin_action == "大防" and pc_action == ["大波"]:
  279.         Ai_Learn(4)
  280.         print("平")
  281.     elif admin_action == "大防" and pc_action == ["大防"]:
  282.         Ai_Learn(5)
  283.         print("平")


  284. def ai_energy_count():
  285.     global ai_energy, ai_action
  286.     print("Ai的之前的运为")
  287.     print(ai_energy)
  288.     while True:
  289.         if ai_action == ['运']:
  290.             ai_energy += 1
  291.             break
  292.         elif ai_action == ['攻']:
  293.             if ai_energy >= 1:
  294.                 ai_energy -= 1
  295.                 break
  296.             else:
  297.                 ai_action = Pc_action(getData)
  298.                 print("test1")
  299.         elif ai_action == ['防']:
  300.             break
  301.         elif ai_action == ['小防']:
  302.             break
  303.         elif ai_action == ['小波']:
  304.             if ai_energy >= 3:
  305.                 ai_energy -= 3
  306.                 break
  307.             else:
  308.                 ai_action = Pc_action(getData)
  309.                 print("test2")
  310.         elif ai_action == ['大防']:
  311.             if ai_energy >= 1:
  312.                 ai_energy -= 1
  313.                 break
  314.             else:
  315.                 ai_action = Pc_action(getData)
  316.                 print("test3")
  317.         elif ai_action == ['大波']:
  318.             if ai_energy >= 5:
  319.                 ai_energy -= 5
  320.                 break
  321.         else:
  322.             ai_action = Pc_action(getData)
  323.             print("test4")
  324.     print("Ai的之后的运为")
  325.     print(ai_energy)


  326. while 1:
  327.     player_action()
  328.     str1 = stringTranslation(admin_energy)
  329.     str2 = stringTranslation(ai_energy)
  330.     getData = "%s%s" % (str1, str2)
  331.     if ai_energy == 0:
  332.         ai_action = ["运"]
  333.     elif ai_energy > 0 and admin_energy == 0:
  334.         choicelist = ["运", "攻", "小波", "大波"]
  335.         ai_action = r.choices(choicelist)
  336.     else:
  337.         ai_action = Pc_action(getData)
  338.     ai_energy_count()
  339.     print("Ai做出的手势为:%s" % ai_action)
  340.     compete(ai_action)
复制代码



谢谢大佬指点!本人刚学python不是很熟看不出来是哪里入了死循环
大概就是print"Ai之前的运为”
如何输出了一个数就陷入死循环了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-5-24 23:42:39 | 显示全部楼层
你的代码看起来有些复杂,但我可以看到一些可能导致你的代码进入死循环的情况。

一种可能的情况是在 player_action() 函数中。如果用户输入的行动不符合任何给定的选项,函数将不断提示用户重新输入。这在理论上可能导致一个无限循环,如果用户持续输入无效的行动。在这种情况下,你可以在函数中设置一个最大尝试次数,以防止无限循环。

第二个可能的问题出在你的 Pc_action() 函数和 Ai_Get_Data() 函数中。如果从文件中获取的数据不包含你期望的字符串,那么这两个函数都可能会陷入无限循环。你需要检查你的数据文件以确保所有的字符串都能被正确地找到。

此外,还有一个 Ai_Learn() 函数,它似乎是打开文件,修改它,然后再次保存。如果在修改数据的过程中发生错误,或者如果文件无法正确保存,这可能会导致程序无法正常退出。

在解决这个问题时,你可以尝试在代码中添加更多的错误检查和异常处理,以确保代码在遇到错误时能够优雅地退出,而不是陷入死循环。另外,考虑使用更好的数据结构来简化你的代码,这也可能会有所帮助。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-5-24 23:49:20 | 显示全部楼层
isdkz 发表于 2023-5-24 23:42
你的代码看起来有些复杂,但我可以看到一些可能导致你的代码进入死循环的情况。

一种可能的情况是在 pla ...

问题似乎出在
def Ai_Get_Data(string):
    file = open(r"AiLearn.txt", 'rb')
    print("打开文件成功")
这里的“打开文件成功”并没有打印
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-24 23:52:17 | 显示全部楼层
Sudaowan 发表于 2023-5-24 23:49
问题似乎出在
def Ai_Get_Data(string):
    file = open(r"AiLearn.txt", 'rb')

从你给出的代码中,如果你正在尝试打开一个文件,并且预期看到的 "打开文件成功" 没有打印,那么可能的原因是以下之一:

1. 你的程序没有达到这段代码。请确保你的程序执行到了这个方法。如果这个函数没有被调用,那么内部的代码自然不会执行。

2. "AiLearn.txt" 文件的路径可能不正确。在你的代码中,你尝试打开的文件的位置是当前工作目录下。如果文件不在那里, open  函数将引发一个  FileNotFoundError  异常。

3. 可能存在其他未处理的异常,导致程序在打开文件之前就崩溃了。你可以尝试在你的代码中添加异常处理,以查看是否有异常发生。

请尝试在你的函数中添加一些异常处理,看看是否有任何错误。例如:

  1. def Ai_Get_Data(string):
  2.     try:
  3.         file = open(r"AiLearn.txt", 'rb')
  4.         print("打开文件成功")
  5.     except Exception as e:
  6.         print("错误发生:", str(e))
复制代码


这将捕获到函数中发生的任何异常,并打印出来。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-5-25 00:00:02 | 显示全部楼层
isdkz 发表于 2023-5-24 23:52
从你给出的代码中,如果你正在尝试打开一个文件,并且预期看到的 "打开文件成功" 没有打印,那么可能的原 ...

仍旧会陷入循环,甚至try的内容都不执行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 20:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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