我刚刚开始使用Python并编写了一个简单的“Rock,Paper,Scissors”游戏,但它对玩家和ai选择的比较方式对我来说没有意义。他们的赢/输条件似乎与我设定的相反。
#Rock Paper Scissors
import random
print("Welcome to Rock, Paper, Scissors!")
print()
print()
choices = ["rock", "paper", "scissors"]
choices[0] < choices[1]
choices[1] < choices[2]
choices[2] < choices[0]
def game():
player_choice = input("What will you choose?: " ).lower()
print()
ai_choice = random.choice(choices)
who_chose = print("You chose",player_choice,"and the computer chose",ai_choice)
if player_choice not in choices:
print("Sorry, that is not a valid option, please choose again.")
print()
game()
elif player_choice > ai_choice:
who_chose
print("The computer wins!")
print()
elif player_choice == ai_choice:
who_chose
print("It was a tie!")
print()
else:
who_chose
print("You Win!")
print()
play_again = input("Do you want to play again? (y/n): ").lower()
if play_again in ["yes", "y"]:
game()
else:
print("Thanks for playing")
game()
我在这里设置了他们的层次结构:
choices[0] < choices[1]
choices[1] < choices[2]
choices[2] < choices[0]
但是当我比较它们时,它似乎在这里落后,但它的作用与岩石剪刀的规则相符:
elif player_choice > ai_choice:
who_chose
print("The computer wins!")
print()
答案 0 :(得分:0)
choices[0] < choices[1]
choices[1] < choices[2]
choices[2] < choices[0]
&LT;是一个布尔运算符,意味着它尝试比较值并确定它是真还是假,它不是赋值。尝试在控制台中输入其中一个语句。 Python按字母顺序解释字符串的层次结构。
现在我能想到的最简单的方法就是创建一个真值表。
#rock, paper, scissors
t=[0, 1, 0; #rock
0, 0, 1; #paper
1, 0, 0] #scissors
索引如下:
t[player_choice,ai_choice]
如果它返回1,那么你就赢了。