我刚刚开始使用Python,并写了一个简单的“石头,布,剪刀”游戏,但它比较玩家和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()发布于 2017-01-12 22:28:25
choices[0] < choices[1]
choices[1] < choices[2]
choices[2] < choices[0]<是一个布尔运算符,这意味着它试图比较这些值并确定它是真是假,它不是一个赋值。尝试在控制台中输入其中一条语句。Python按字母顺序解释字符串的层次结构。
我现在能想到的做你想做的事情的最简单的方法就是创建一个真值表。
#rock, paper, scissors
t=[0, 1, 0; #rock
0, 0, 1; #paper
1, 0, 0] #scissors并建立索引,如下所示:
t[player_choice,ai_choice]如果它返回1,则表示您赢了。
https://stackoverflow.com/questions/41615472
复制相似问题