背景信息,如果您只想查看下面的代码问题,就不必阅读:
我希望每个人都熟悉棍棒或“尼姆”的游戏。如果没有,你设置一个开始数量的棍子( 10到50之间)和抽签(1-3支),直到没有任何棍子,宣布谁拉了最后一根棍子输家。在我的编程课中,我们还包括了对抗人工智能的选项。但是,人工智能不再是一个随机选择一个数字1-3的虚拟人.现在他从他的每一个回合中吸取教训。
Implementation
这是我现在正在处理的代码。
choice=random.randint(1,maxchoice) #computer picks a random number
bucketnum=sticks #bucket to take ball from
pullnum=choice #ball to take
for i in bucket:
for bucket[bucketnum] in i:
bucketnum.pop(pullnum)
print(bucket[bucketnum])我要把球拿出来的水桶,本质上就是剩下的棍子数,我只是在找一个特定的桶,在清单上找不到,然后把球拿出来。现在我收到一条错误消息,上面说bucketnum.pop(pullnum) - 'int‘对象没有'pop’的属性?这是桶代码(列表中的列表):
bucket=[]
for j in range(51):
bucket.append([1,2,3])我可能会完全混淆,但如果有人有任何意见,甚至有任何问题需要澄清,请回答。谢谢大家。
编辑:这里还有一些代码,对不起,我不想添加变量的定义,等等。
if option==2:
sticks=""
while True:
try:
sticks=int(input("Enter the number of sticks to begin with: "))
if sticks>=10 and sticks<=50:
print("Alright, there are",sticks,"sticks in the pile.")
break
else:
print("You mut enter an integer. (10-50)")
except ValueError:
print("You must enter an integer.")
player1=True
while sticks>0:
maxchoice=min(3,sticks)
choice=-1
countbucket=0
if player1:
while choice<1 or choice>maxchoice:
try:
choice=int(input("Player 1, how many sticks would you like to take? (1-3): "))
if choice>=1 and choice<=3:
sticks-=choice
print("There are",sticks,"sticks remaining.")
else:
print("You must enter an integer from 1-3.")
except ValueError:
print("You must enter an integer.")
player1=not player1
else:
choice=random.randint(1,maxchoice)
bucketnum=sticks
pullnum=choice
for i in bucket:
for bucket[bucketnum] in i:
bucketnum.pop(pullnum)
print(bucket[bucketnum])
sticks-=1
print("Computer drew",choice,"stick(s). There are",sticks,"sticks remaining.")
player1=not player1
if player1==False:
print("Player 1 took the last stick.\nComputer wins!")
else:
print("Player 1 wins!")这是我的程序中的选项2,因为选项1是玩家1与玩家2。很明显,在AI智能的实现上我并没有走多远,这有点棘手。
摘录的不是所有的代码。我不是在问如何在这一点上完成这个任务,虽然执行这个新的智能AI代码的技巧会很有帮助,但在这种情况下,它更多的是一个重点是找出列表索引。
发布于 2014-10-31 22:14:49
看起来,您将内部for循环中的变量赋值为“bucketbucketnum”。令我惊讶的是,这并不是语法错误,但我认为这并不是你真正想要做的。
如果您正在处理一个嵌套列表,并且列表中的位置对应于剩下的枝数,那么您希望按位置对该列表进行索引,以获得存储桶,而不是迭代该列表以找到它。
如果你这样想的话
buckets = [[1,2,3], ..., ..., ...]然后桶是桶在桶列表中的位置。因此,在您的情况下,如果您想抓取桶'26‘棒,您可以访问它通过索引桶的数字。
buckets[25] # 25 since you're counting from 0+在这一点上,您有问题的桶,并可以弹出选择。
bucket = buckets[25]
bucket.pop(pullnum)发布于 2022-01-17 08:52:15
您没有定义选项,也没有导入随机库。
https://stackoverflow.com/questions/26683992
复制相似问题