所以基本上我一直在用python做一个AI岩布剪刀脚本,它可以工作,但只选择了纸,我需要它来选择不同的东西。问题在第29行和第46行之间。可能有一个简单的解决方案,但我似乎找不到它。请帮我编写noobie代码。
import random
import numpy as np
import matplotlib.pyplot as plt
x = 1
paper = 0
rock = 0
scissors = 0
plist = []
result = ""
def percentage(item, result):
total = rock + paper + scissors
result = item / total * 10
while x == 1:
print(" ")
ui = input("Rock, Paper, Scissors: ")
if ui == "Rock":
rock += 1
elif ui == "Paper":
paper += 1
elif ui == "Scissors":
scissors += 1
plist.append(ui)
plist = ["Rock", "Scissors", "Rock", "Rock", "Paper"]
if plist[0] == "Rock":
aio = "Paper"
plist.pop(0)
elif plist[0] == "Paper":
aio = "Scissors"
plist.pop(0)
elif plist[0] == "Scissors":
aio = "Rock"
plist.pop(0)
print(" ")
print("AI chose " + aio)
print(" ")
if aio == "Rock" and ui == "Paper":
print("You won")
elif aio == "Paper" and ui == "Scissors":
print("You won")
elif aio == "Scissors" and ui == "Rock":
print("You won")
elif aio == "Paper" and ui == "Rock":
print("You lost")
elif aio == "Scissors" and ui == "Paper":
print("You lost")
elif aio == "Rock" and ui == "Scissors":
print("You lost")
elif aio == "Paper" and ui == "Paper":
print("You tied")
elif aio == "Scissors" and ui == "Scissors":
print("You tied")
elif aio == "Rock" and ui == "Rock":
print("You tied")发布于 2021-09-03 16:11:48
不清楚你的AI到底应该做什么,但现在它总是选择Paper,因为在每个循环中你总是这样做:
plist = ["Rock", "Scissors", "Rock", "Rock", "Paper"]
if plist[0] == "Rock":
aio = "Paper"对于这段代码,plist[0]只能是Rock --不管您以前给它分配了什么,每次进行比较之前都要将它重新分配给这个静态列表(从它pop和append到它都没有关系,因为在下次查看它之前会覆盖整个内容)。由于不清楚plist的用途,我建议直接摆脱它,并使用random.choice来挑选AI的投掷:
import random
while True:
ui = input("\nRock, Paper, Scissors: ")
aio = random.choice(["Rock, Paper, Scissors"])
print(f"\nAI chose {aio}\n")
if aio == "Rock" and ui == "Paper":
print("You won")
elif aio == "Paper" and ui == "Scissors":
print("You won")
elif aio == "Scissors" and ui == "Rock":
print("You won")
elif aio == "Paper" and ui == "Rock":
print("You lost")
elif aio == "Scissors" and ui == "Paper":
print("You lost")
elif aio == "Rock" and ui == "Scissors":
print("You lost")
elif aio == "Paper" and ui == "Paper":
print("You tied")
elif aio == "Scissors" and ui == "Scissors":
print("You tied")
elif aio == "Rock" and ui == "Rock":
print("You tied")
else:
print(f"You picked {ui}, which was not one of the choices.")发布于 2021-09-03 16:10:56
在每个循环中都定义了plist,因此plist[0]始终为'Rock'。
如果从循环中删除plist = ["Rock", "Scissors", "Rock", "Rock", "Paper"],它应该可以正常工作。
https://stackoverflow.com/questions/69047772
复制相似问题