我正在尝试比较两个随机整数,但是它不起作用,当将{}转换为mode [0]时出现TypeError
if exec("doggies[mode[0][0]][0]") >= exec("doggies[mode[1][0]][0]"):
值:
mode = ["player", "computer"] #THESE MUST BE STRINGS
doggies = { n : [randint(1,5), randint(1,100), randint(1,10), randint(1,10)] for n in dogs}
dogs = []
file = open("dogs.txt")
for n in file:
n = n.strip()
dogs.append(n)
file.close()
player = ["marley", "barley"]
computer = ["ley the ma", "im bad at names"]
#these two lists above are supposed to have random names inside, but for now i'll just put them as fixed values so it's easier to understand
dogs.txt:
marley the barley
barley the marley
ley the ma
im bad at names
运行if exec("doggies[{}[0]][0]".format(mode[0])) >= exec("doggies[{}[0]][0]".format(mode[1])):
会得到:
TypeError:“ NoneType”和“ NoneType”的实例之间不支持“> =”
如果您仍然感到困惑,请按此分类。
if exec("doggies[{}[0]][0]".format(mode[0])) >= exec("doggies[{}[0]][0]".format(mode[1])):
应该立即执行
if doggies[player[0]][0] >= doggies[computer[0]][0]:
已简化
if doggies[marley][0] >= doggies[ley the ma][0]:
已简化
if 1 >= 2
1和2应该是介于1和5之间的随机数,但现在它们是固定数。