0

私はPythonで始めたばかりで、単純なじゃんけんゲームをじゃんけんトカゲのスポックに変更しようとしています。その結果、ランダムに生成されたコンピューターの選択を、値が失われていることを示す 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()

残念ながら、このコードでは常にプレイヤーが勝ってしまいます...何が間違っているのでしょうか?

ご協力ありがとうございました:)

4

2 に答える 2

1

グローバルとして持ってoutcomeいますが、「勝つ」以外に設定することはありません。したがって、一度勝てば、 の値outcomeは常に「勝つ」になります。

def result(player, computer):
    outcome = ""

outcome他の場所では使用しないため、とにかくグローバルにする理由はありません。

その変数はまったく必要ありません。これをHyperboreusが回答で述べたことと組み合わせると、result他のすべてが同じで、次のようにメソッドを開始できます。

def result(player, computer):
    print "Computer threw {0}!".format(names[computer])
    global player_score, computer_score
    if computer in rules[player]:
        print "Your victory has been assured."
        ...
于 2013-10-14T23:10:00.593 に答える