不知道什么原因陷入死循环出不来
import random as rimport pickle
admin_energy = 1# 我方初始能量
ai_energy = 1# 敌方初始能量
admin_action = ""# 我方行动
def player_action():
global admin_action, admin_energy, ai_energy
print("这是你的回合")
print("你的运有%s" % admin_energy)
while True:
admin_action = input('请输入您的手势')
if admin_action == '运':
admin_energy += 1
break
elif admin_action == '攻':
if admin_energy >= 1:
admin_energy -= 1
break
else:
print('能量不足,请重新输入')
elif admin_action == '防':
break
elif admin_action == '小防':
break
elif admin_action == '小波':
if admin_energy >= 3:
admin_energy -= 3
break
else:
print('能量不足,请重新输入')
elif admin_action == '大防':
if admin_energy >= 1:
admin_energy -= 1
break
else:
print('能量不足,请重新输入')
elif admin_action == '大波':
if admin_energy >= 5:
admin_energy -= 5
break
else:
print('能量不足,请重新输入')
else:
print('输入错误请重新输入')
def Pc_action(string):
action = ["运", "攻", "防", "小波", "大波", "大防"]
weight = Ai_Get_Data(string)# 从“人工智能获取数据”函数里,得到权重的值
print("Ai_action已运行!action为")
temp = r.choices(action, weights=weight)
print(temp)
return temp
def Ai_Get_Data(string):# 传参string,如果人类和AI运都为1,则传OneOne,如果人为1 AI为2,则传OneTwo,以此类推
file = open(r"AiLearn.txt", 'rb')
print("打开文件成功")
ai_data = []
print("正在获取Ai数据……")
print(getData)
if string == "OneOne":# 检测输入字符串是否为标识查找符,3运以上全归为三运
OneOne_ai_data = pickle.load(file)
ai_data = OneOne_ai_data
elif string == "OneTwo":
OneTwo_ai_data = pickle.load(file)
ai_data = OneTwo_ai_data
elif string == "OneThree":
OneThree_ai_data = pickle.load(file)
ai_data = OneThree_ai_data
elif string == "TwoOne":
TwoOne_ai_data = pickle.load(file)
ai_data = TwoOne_ai_data
elif string == "TwoTwo":
TwoTwo_ai_data = pickle.load(file)
ai_data = TwoTwo_ai_data
elif string == "TwoThree":
TwoThree_ai_data = pickle.load(file)
ai_data = TwoThree_ai_data
elif string == "ThreeOne":
ThreeOne_ai_data = pickle.load(file)
ai_data = ThreeOne_ai_data
elif string == "ThreeTwo":
ThreeTwo_ai_data = pickle.load(file)
ai_data = ThreeTwo_ai_data
elif string == "ThreeThree":
ThreeThree_ai_data = pickle.load(file)
ai_data = ThreeThree_ai_data
file.close()
print("获取的ai的data为")
print(ai_data)
return ai_data
''' # 弃用该方法
ai_data = []
f = open(r"AiLearn.txt")
for each in f.readline():# 遍历所有权重的值
if len(string) == 6:
if each[:6] == string:# 如果该标识查找符在该行
each = each.replace(string, '')# 去除标识查找符
ai_data = ai_data + each.split(',')# 以逗号分开,将数据变成列表
elif len(string) == 8:
if each[:8] == string:
each = each.replace(string, '')# 去除标识查找符
ai_data = ai_data + each.split(',')# 以逗号分开,将数据变成列表
elif len(string) == 10:
if each[:10] == string:
each = each.replace(string, '')# 去除标识查找符
ai_data = ai_data + each.split(',')# 以逗号分开,将数据变成列表
'''
def Ai_Learn(ai_action_number):
file = open(r"AiLearn.txt", "rb")
OneOne_ai_data = pickle.load(file)
OneTwo_ai_data = pickle.load(file)
OneThree_ai_data = pickle.load(file)
TwoOne_ai_data = pickle.load(file)
TwoTwo_ai_data = pickle.load(file)
TwoThree_ai_data = pickle.load(file)
ThreeOne_ai_data = pickle.load(file)
ThreeTwo_ai_data = pickle.load(file)
ThreeThree_ai_data = pickle.load(file)
print("Learn函数中已加载Data")
file.close()
file = open(r"AiLearn.txt", 'ab')
if getData == "OneOne":# 检测输入字符串是否为标识查找符,3运以上全归为三运
OneOne_ai_data = OneOne_ai_data + 1# 每个number代表action在该数组的索引值
pickle.dump(OneOne_ai_data, file)
elif getData == "OneTwo":
OneTwo_ai_data = OneTwo_ai_data + 1# 每个number代表action在该数组的索引值
pickle.dump(OneTwo_ai_data, file)
elif getData == "OneThree":
OneThree_ai_data = OneThree_ai_data + 1# 每个number代表action在该数组的索引值
pickle.dump(OneThree_ai_data, file)
elif getData == "TwoOne":
TwoOne_ai_data = TwoOne_ai_data + 1# 每个number代表action在该数组的索引值
pickle.dump(TwoOne_ai_data, file)
elif getData == "TwoTwo":
TwoTwo_ai_data = TwoTwo_ai_data + 1# 每个number代表action在该数组的索引值
pickle.dump(TwoTwo_ai_data, file)
elif getData == "TwoThree":
TwoThree_ai_data = TwoThree_ai_data + 1# 每个number代表action在该数组的索引值
pickle.dump(TwoThree_ai_data, file)
elif getData == "ThreeOne":
ThreeOne_ai_data = ThreeOne_ai_data + 1# 每个number代表action在该数组的索引值
pickle.dump(ThreeOne_ai_data, file)
elif getData == "ThreeTwo":
ThreeTwo_ai_data = ThreeTwo_ai_data + 1# 每个number代表action在该数组的索引值
pickle.dump(ThreeTwo_ai_data, file)
elif getData == "ThreeThree":
ThreeThree_ai_data = ThreeThree_ai_data + 1# 每个number代表action在该数组的索引值
pickle.dump(ThreeThree_ai_data, file)
file.close()
''' # 弃用该方法
ai_alldata = file.read()
ai_data_update = ai_data + 1# 每个number代表action在该数组的索引值
for each in ai_alldata:
if getData in each:
ai_alldata.replace("%s%s" % (getdata, aidata), "%s%s" % (getdata, ai_data_update))
file.close()
'''
def stringTranslation(num):
trans = ""
if num <= 1:
trans = "One"
elif num == 2:
trans = "Two"
elif num >= 3:
trans = "Three"
return trans
def DelE():
global admin_energy, ai_energy
admin_energy = 0
ai_energy = 0
def compete(pc_action):
if admin_action == "运" and pc_action == ["运"]:# action = ["运", "攻", "防", "小波", "大波", "大防"]
Ai_Learn(0)
print("平")
elif admin_action == "运" and pc_action == ["攻"]:
Ai_Learn(1)
print("Ai胜")
DelE()
elif admin_action == "运" and pc_action == ["防"]:
Ai_Learn(2)
print("平")
elif admin_action == "运" and pc_action == ["小波"]:
Ai_Learn(3)
print("Ai胜")
DelE()
elif admin_action == "运" and pc_action == ["大波"]:
Ai_Learn(4)
print("Ai胜")
DelE()
elif admin_action == "运" and pc_action == ["大防"]:
Ai_Learn(5)
print("平")
elif admin_action == "攻" and pc_action == ["运"]:# action = ["运", "攻", "防", "小波", "大波", "大防"]
print("人类胜利")
DelE()
elif admin_action == "攻" and pc_action == ["攻"]:
Ai_Learn(1)
print("平")
elif admin_action == "攻" and pc_action == ["防"]:
Ai_Learn(2)
print("平")
elif admin_action == "攻" and pc_action == ["小波"]:
Ai_Learn(3)
print("平")
elif admin_action == "攻" and pc_action == ["大波"]:
Ai_Learn(4)
print("平")
elif admin_action == "攻" and pc_action == ["大防"]:
Ai_Learn(5)
print("平")
elif admin_action == "防" and pc_action == ["运"]:# action = ["运", "攻", "防", "小波", "大波", "大防"]
Ai_Learn(0)
print("平")
elif admin_action == "防" and pc_action == ["攻"]:
Ai_Learn(1)
print("平")
elif admin_action == "防" and pc_action == ["防"]:
Ai_Learn(2)
print("平")
elif admin_action == "防" and pc_action == ["小波"]:
Ai_Learn(3)
print("Ai胜利")
DelE()
elif admin_action == "防" and pc_action == ["大波"]:
Ai_Learn(4)
print("Ai胜利")
DelE()
elif admin_action == "防" and pc_action == ["大防"]:
Ai_Learn(5)
print("平")
elif admin_action == "小波" and pc_action == ["运"]:# action = ["运", "攻", "防", "小波", "大波", "大防"]
print("人类胜利")
DelE()
elif admin_action == "小波" and pc_action == ["攻"]:
Ai_Learn(1)
print("平")
elif admin_action == "小波" and pc_action == ["防"]:
print("人类胜利")
DelE()
elif admin_action == "小波" and pc_action == ["小波"]:
Ai_Learn(3)
print("平")
elif admin_action == "小波" and pc_action == ["大波"]:
Ai_Learn(4)
print("平")
elif admin_action == "小波" and pc_action == ["大防"]:
Ai_Learn(5)
print("平")
elif admin_action == "大波" and pc_action == ["运"]:# action = ["运", "攻", "防", "小波", "大波", "大防"]
print("人类胜利")
DelE()
elif admin_action == "大波" and pc_action == ["攻"]:
Ai_Learn(1)
print("平")
elif admin_action == "大波" and pc_action == ["防"]:
print("人类胜利")
DelE()
elif admin_action == "大波" and pc_action == ["小波"]:
Ai_Learn(3)
print("平")
elif admin_action == "大波" and pc_action == ["大波"]:
Ai_Learn(4)
print("平")
elif admin_action == "大波" and pc_action == ["大防"]:
print("人类胜利")
DelE()
elif admin_action == "大防" and pc_action == ["运"]:# action = ["运", "攻", "防", "小波", "大波", "大防"]
Ai_Learn(0)
print("平")
elif admin_action == "大防" and pc_action == ["攻"]:
Ai_Learn(1)
print("平")
elif admin_action == "大防" and pc_action == ["防"]:
Ai_Learn(2)
print("平")
elif admin_action == "大防" and pc_action == ["小波"]:
Ai_Learn(3)
print("平")
elif admin_action == "大防" and pc_action == ["大波"]:
Ai_Learn(4)
print("平")
elif admin_action == "大防" and pc_action == ["大防"]:
Ai_Learn(5)
print("平")
def ai_energy_count():
global ai_energy, ai_action
print("Ai的之前的运为")
print(ai_energy)
while True:
if ai_action == ['运']:
ai_energy += 1
break
elif ai_action == ['攻']:
if ai_energy >= 1:
ai_energy -= 1
break
else:
ai_action = Pc_action(getData)
print("test1")
elif ai_action == ['防']:
break
elif ai_action == ['小防']:
break
elif ai_action == ['小波']:
if ai_energy >= 3:
ai_energy -= 3
break
else:
ai_action = Pc_action(getData)
print("test2")
elif ai_action == ['大防']:
if ai_energy >= 1:
ai_energy -= 1
break
else:
ai_action = Pc_action(getData)
print("test3")
elif ai_action == ['大波']:
if ai_energy >= 5:
ai_energy -= 5
break
else:
ai_action = Pc_action(getData)
print("test4")
print("Ai的之后的运为")
print(ai_energy)
while 1:
player_action()
str1 = stringTranslation(admin_energy)
str2 = stringTranslation(ai_energy)
getData = "%s%s" % (str1, str2)
if ai_energy == 0:
ai_action = ["运"]
elif ai_energy > 0 and admin_energy == 0:
choicelist = ["运", "攻", "小波", "大波"]
ai_action = r.choices(choicelist)
else:
ai_action = Pc_action(getData)
ai_energy_count()
print("Ai做出的手势为:%s" % ai_action)
compete(ai_action)
谢谢大佬指点!本人刚学python不是很熟看不出来是哪里入了死循环
大概就是print"Ai之前的运为”
如何输出了一个数就陷入死循环了 你的代码看起来有些复杂,但我可以看到一些可能导致你的代码进入死循环的情况。
一种可能的情况是在 player_action() 函数中。如果用户输入的行动不符合任何给定的选项,函数将不断提示用户重新输入。这在理论上可能导致一个无限循环,如果用户持续输入无效的行动。在这种情况下,你可以在函数中设置一个最大尝试次数,以防止无限循环。
第二个可能的问题出在你的 Pc_action() 函数和 Ai_Get_Data() 函数中。如果从文件中获取的数据不包含你期望的字符串,那么这两个函数都可能会陷入无限循环。你需要检查你的数据文件以确保所有的字符串都能被正确地找到。
此外,还有一个 Ai_Learn() 函数,它似乎是打开文件,修改它,然后再次保存。如果在修改数据的过程中发生错误,或者如果文件无法正确保存,这可能会导致程序无法正常退出。
在解决这个问题时,你可以尝试在代码中添加更多的错误检查和异常处理,以确保代码在遇到错误时能够优雅地退出,而不是陷入死循环。另外,考虑使用更好的数据结构来简化你的代码,这也可能会有所帮助。 isdkz 发表于 2023-5-24 23:42
你的代码看起来有些复杂,但我可以看到一些可能导致你的代码进入死循环的情况。
一种可能的情况是在 pla ...
问题似乎出在
def Ai_Get_Data(string):
file = open(r"AiLearn.txt", 'rb')
print("打开文件成功")
这里的“打开文件成功”并没有打印 Sudaowan 发表于 2023-5-24 23:49
问题似乎出在
def Ai_Get_Data(string):
file = open(r"AiLearn.txt", 'rb')
从你给出的代码中,如果你正在尝试打开一个文件,并且预期看到的 "打开文件成功" 没有打印,那么可能的原因是以下之一:
1. 你的程序没有达到这段代码。请确保你的程序执行到了这个方法。如果这个函数没有被调用,那么内部的代码自然不会执行。
2. "AiLearn.txt" 文件的路径可能不正确。在你的代码中,你尝试打开的文件的位置是当前工作目录下。如果文件不在那里, open函数将引发一个FileNotFoundError异常。
3. 可能存在其他未处理的异常,导致程序在打开文件之前就崩溃了。你可以尝试在你的代码中添加异常处理,以查看是否有异常发生。
请尝试在你的函数中添加一些异常处理,看看是否有任何错误。例如:
def Ai_Get_Data(string):
try:
file = open(r"AiLearn.txt", 'rb')
print("打开文件成功")
except Exception as e:
print("错误发生:", str(e))
这将捕获到函数中发生的任何异常,并打印出来。 isdkz 发表于 2023-5-24 23:52
从你给出的代码中,如果你正在尝试打开一个文件,并且预期看到的 "打开文件成功" 没有打印,那么可能的原 ...
仍旧会陷入循环,甚至try的内容都不执行
页:
[1]