我遇到了一個我的python程序問題,並且我花了太多時間嘗試修復它,但是我不能。我希望你能幫助我。Python - 「在分配之前引用的本地x變量」(CLOSED)
無論如何,問題是:
def choose_winnerPvsP(p1,p2):
當我運行播放器的一部分VS電腦炒鍋完美,但玩家對玩家的一部分給了我這個錯誤:「局部變量player_1score轉讓前參考「 我不明白爲什麼if不會被檢查。 PS:我是一個初學者,我知道代碼可以更緊湊,但現在我只想要這個東西工作。在此先感謝
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from random import *
from time import sleep
import sys
import os
os.system("cls") #ou "clear" se estiver em linux
print "******Welcome to Rock Paper Scissors Game!******\n"
playerOrComputer = str(raw_input("Would you like to play against a friend or a computer?(F or C) \n"))
p1_score = 0
p2_score = 0
def player_turn(nome_player):
print "What do you want to do, " + nome_player + "?"
pchoice = str(raw_input("ROCK (R) PAPER (P) SCISSORS (S) or Quit\n>> "))
if pchoice == "R" or pchoice == "r":
print "You chosed ROCK"
elif pchoice == "P" or pchoice == "p":
print "You chosed PAPER"
elif pchoice == "S" or pchoice == "s":
print "You chosed SCISSORS"
elif pchoice == "quit" or pchoice == "Quit":
sys.exit()
else:
print "I didn't understand! Please repeat."
player_turn()
return pchoice
def player_turn2p(nome_player1,p2):
print "What do you want to do, " + nome_player1 + "?"
pchoice1 = str(raw_input("ROCK (R) PAPER (P) SCISSORS (S)\n>> "))
if pchoice1 == "R" or pchoice1 == "r":
pchoice1 = "ROCK"
print "You chosed ROCK"
sleep(1)
os.system("cls")
elif pchoice1 == "P" or pchoice1 == "p":
pchoice1 = "PAPER"
print "You chosed PAPER"
sleep(1)
os.system("cls")
elif pchoice1 == "S" or pchoice1 == "s":
pchoice1 = "SCISSORS"
print "You chosed SCISSORS"
sleep(1)
os.system("cls")
elif pchoice1 == "quit" or pchoice == "Quit":
sys.exit()
else:
print "I didn't understand!"
player_turn2p(nome_player1,p2)
print "What do you want to do, " + p2 + "?"
pchoice2 = str(raw_input("ROCK (R) PAPER (P) SCISSORS (S)\n>> "))
if pchoice2 == "R" or pchoice2 == "r":
print p2 + " chosed ROCK\n" + nome_player1 + " chosed " + pchoice1
elif pchoice2 == "P" or pchoice2 == "p":
print p2 + " chosed PAPER\n" + nome_player1 + " chosed " + pchoice1
elif pchoice2 == "S" or pchoice2 == "s":
print p2 + " chosed SCISSORS\n" + nome_player1 + " chosed " + pchoice1
elif pchoice1 == "quit" or pchoice == "Quit":
sys.exit()
else:
print "I didn't understand!"
player_turn2p(nome_player1,p2)
return pchoice1, pchoice2
def computer_turn():
choicecomp = randint(1,3)
if choicecomp == 1:
choicecomp = "ROCK"
elif choicecomp == 2:
choicecomp = "PAPER"
elif choicecomp == 3:
choicecomp = "SCISSORS"
return choicecomp
#1-Rock 2-Paper 3-Scissors
def choose_winnerPvsP(p1,p2):
player_choice1, player_choice2= player_turn2p(p1,p2)
if player_choice1 == "R" and player_choice2 == "R" or player_choice1 == "r" and player_choice2 == "r" or player_choice1 == "R" and player_choice2 == "r" or player_choice1 == "r" and player_choice2 == "R":
player_1score = "lose"
player_2score = "win"
print "******It's a draw!******"
elif player_choice1 == "R" and player_choice2 == "P" or player_choice1 == "r" and player_choice2 == "p" or player_choice1 == "R" and player_choice2 == "p" or player_choice1 == "r" and player_choice2 == "P":
player_1score = "lose"
player_2score = "win"
print "******" + p2 + " wins!******"
elif player_choice1 == "R" and player_choice2 == "S" or player_choice1 == "r" and player_choice2 == "s" or player_choice1 == "R" and player_choice2 == "s" or player_choice1 == "r" and player_choice2 == "S":
player_1score = "win"
player_2score = "lose"
print "******" + p1 + " wins!******"
elif player_choice1 == "P" and player_choice2 == "R" or player_choice1 == "p" and player_choice2 == "r" or player_choice1 == "P" and player_choice2 == "r" or player_choice1 == "p" and player_choice2 == "R":
player_1score = "win"
player_2score = "lose"
print "******" + p1 + " wins!******"
elif player_choice1 == "P" and player_choice2 == "P" or player_choice1 == "p" and player_choice2 == "p" or player_choice1 == "P" and player_choice2 == "p" or player_choice1 == "p" and player_choice2 == "P":
player_1score = "draw"
player_2score = "draw"
print "******It's a draw!******"
elif player_choice1 == "P" and player_choice2 == "S" or player_choice1 == "p" and player_choice2 == "s" or player_choice1 == "P" and player_choice2 == "s" or player_choice1 == "p" and player_choice2 == "S":
player_1score = "lose"
player_2score = "win"
print "******" + p2 + " wins!******"
elif player_choice1 == "S" and player_choice2 == "R" or player_choice1 == "s" and player_choice2 == "r" or player_choice1 == "S" and player_choice2 == "r" or player_choice1 == "s" and player_choice2 == "R":
player_1score = "lose"
player_2score = "win"
print "******" + p2 + " wins!******"
elif player_choice1 == "S" and player_choice2 == "P" or player_choice1 == "s" and player_choice2 == "p" or player_choice1 == "S" and player_choice2 == "p" or player_choice1 == "s" and player_choice2 == "P":
player_1score = "win"
player_2score = "lose"
print "******" + p1 + " wins!******"
elif player_choice1 == "S" and player_choice2 == "S" or player_choice1 == "s" and player_choice2 == "s" or player_choice1 == "S" and player_choice2 == "s" or player_choice1 == "s" and player_choice2 == "S":
player_1score = "draw"
player_2score = "draw"
print "******It's a draw!******"
return player_1score, player_2score
def choose_winnerPvsC(player_name,player_choice, computer_choice):
if player_choice == "R" and computer_choice == "ROCK" or player_choice == "r" and computer_choice == "ROCK":
computer_score = "draw"
player_score = "draw"
print "******It's a draw!****** "
elif player_choice == "R" and computer_choice == "PAPER" or player_choice == "r" and computer_choice == "PAPER":
computer_score = "win"
player_score = "lose"
print "******I win!****** "
elif player_choice == "R" and computer_choice == "SCISSORS" or player_choice == "r" and computer_choice == "SCISSORS":
player_score = "win"
computer_score = "lose"
print "******" + player_name + " wins!****** "
elif player_choice == "P" and computer_choice == "ROCK" or player_choice == "p" and computer_choice == "ROCK":
player_score = "win"
computer_score = "lose"
print "******" + player_name + " wins!****** "
elif player_choice == "P" and computer_choice == "PAPER" or player_choice == "p" and computer_choice == "PAPER":
computer_score = "draw"
player_score = "draw"
print "******It's a draw!******"
elif player_choice == "P" and computer_choice == "SCISSORS" or player_choice == "p" and computer_choice == "SCISSORS":
computer_score = "win"
player_score = "lose"
print "******I win!****** "
elif player_choice == "S" and computer_choice == "ROCK" or player_choice == "s" and computer_choice == "ROCK":
computer_score = "win"
player_score = "lose"
print "******I win!****** "
elif player_choice == "S" and computer_choice == "PAPER" or player_choice == "s" and computer_choice == "PAPER":
player_score = "win"
computer_score = "lose"
print "******" + player_name + " wins!****** "
elif player_choice == "S" and computer_choice == "SCISSORS" or player_choice == "s" and computer_choice == "SCISSORS":
computer_score = "draw"
player_score = "draw"
print "******It's a draw!****** "
return player_score, computer_score
def loopPvsP():
p1score = 0
p2score = 0
while True:
player_1score, player_2score = choose_winnerPvsP(p1,p2)
if player_1score == "win":
p1score += 1
elif player_2score == "win":
p2score += 1
print p1 + ":",p1score
print p2 + ":",p2score
if p1score == 5:
print "-+-+-+-+-"+ p1.upper() + "IS THE BIG WINNER!!-+-+-+-+-"
break
elif p2score == 5:
print "-+-+-+-+-"+ p2.upper() + "IS THE BIG WINNER!!-+-+-+-+-"
break
def loopPvsC():
pscore = 0
cscore = 0
while True:
pchoice = player_turn(p1c)
choicecomp = computer_turn()
print "I chosed "+ choicecomp + "!"
player_score, computer_score = choose_winnerPvsC(p1c,pchoice, choicecomp)
if computer_score == "win":
cscore += 1
elif player_score == "win":
pscore += 1
print "Computer:",cscore
print p1c + ":",pscore
if pscore == 5:
print "-+-+-+-+-YOU ARE THE BIG WINNER!!-+-+-+-+-"
break
elif cscore == 5:
print "-+-+-+-+-I'M THE BIG WINNER!!-+-+-+-+-"
break
if playerOrComputer == "f" or playerOrComputer == "F":
p1 = str(raw_input("Player 1: "))
p2 = str(raw_input("Player 2: "))
os.system("cls")
loopPvsP()
elif playerOrComputer == "c" or playerOrComputer == "C":
p1c = str(raw_input("Player: "))
os.system("cls")
loopPvsC()
else:
print "That's not what I asked!"
使用調試器,所以你會知道爲什麼'if'內容沒有被執行。 – BartoszKP
我很抱歉,但我不知道如何使用調試器。但謝謝你的答案。我會看看我能做些什麼。 –
然後,這聽起來像是學習如何這樣做的最佳時機,除非您希望將所有調試問題都視爲SO問題。您可以從閱讀[Eric Lippert關於調試諸如您的小程序的這篇出色文章]開始。(http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。 –