我刚刚开始使用 Python,我正在尝试将一个简单的石头剪刀布游戏修改为石头剪刀布蜥蜴 spock。结果,我现在需要将随机生成的计算机选择与表示丢失值的不是 1 而是 2 个字典项进行比较:
#!/usr/bin/python
import random
import time
rock = 1
paper = 2
scissors = 3
lizard = 4
spock = 5
names = { rock: "Rock", paper: "Paper", scissors: "Scissors", lizard: "Lizard", spock: "Spock"}
rules = { rock: [scissors, lizard], paper: [rock, spock], scissors: [paper, lizard], lizard: [paper, spock], spock: [rock, scissors]}
player_score = 0
computer_score = 0
def start():
print "Let's play a game of Rock, Paper, Scissors, Lizard, Spock"
while game():
pass
scores()
def game():
player = move()
computer = random.randint(1, 5)
result (player, computer)
return play_again()
def move():
while True:
print
player = raw_input("Rock = 1\nPaper = 2\nScissors = 3\nLizard = 4\nSpock = 5\nMake a move: ")
try:
player = int(player)
if player in (1,2,3,4,5):
return player
except ValueError:
pass
print "Oops! I didn't understand that. Please enter 1, 2, 3, 4, or 5."
def result(player, computer):
# print "1..."
# time.sleep(1)
# print "2..."
# time.sleep(1)
# print "3!"
# time.sleep(0.5)
print "Computer threw {0}!".format(names[computer])
global player_score, computer_score
for i in rules[player]:
if i == computer:
global outcome
outcome = "win"
if outcome == "win":
print "Your victory has been assured."
player_score += 1
elif player == computer:
print "Tie game."
else:
print "The computer laughs as you realise you have been defeated."
computer_score += 1
def play_again():
answer = raw_input("Would you like to play again? y/n: ")
if answer in ("y", "Y", "yes", "Yes", "Of course!"):
return answer
else:
print "Thank you very much for playing. See you next time!"
def scores():
global player_score, computer_score
print "HIGH SCORES"
print "Player: ", player_score
print "Computer: ", computer_score
if __name__ == '__main__':
start()
不幸的是,这段代码导致玩家总是赢……我做错了什么?
非常感谢您的帮助 :)