python - Python- “Local x variable referenced before assignment”(关闭)

标签 python variables error-handling

我的python程序有问题,我花了太多时间尝试修复它,但我没有。我希望你能帮助我。

无论如何,问题出在:

def choose_winnerPvsP(p1,p2):

当我运行它时,Player vs Computer的部分工作正常,但是Player vs Player的部分却给我以下错误:“分配前引用了局部变量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!"

最佳答案

嗯调试器被高估了-我花了好几年没有在Python中使用调试器。只需在您的大if/elif语句中添加最终的else子句:

else:
    print "Did not find answer"
    print repr(player_choice), repr(computer_choice)

如果没有成功,则意味着您可能在其他地方有错字。无论如何,您当然可以添加更多的调试语句-如果需要,可以在每个elif子句中添加一个。

这就提出了另一点-您有很多if/elif子句。

您可能想研究诸如dict之类的带有可以加在一起的值或可以调用的函数的方法,以减少所需的if/else语句的数量。

关于python - Python- “Local x variable referenced before assignment”(关闭),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32155783/

相关文章:

python - 在eclipse中将python模块添加到pydev会导致导入错误?

python - 如何使用 Python 输出嵌套循环的结果

python - "overload the dot operator"有什么技巧吗?

循环内的javascript var声明

ruby-on-rails - 推送到Heroku时“different prefix”错误和“Lockfile is unreadable”错误

python - 根据键重新排列列表而不排序

ruby-on-rails - 如何取消初始化/取消定义 Ruby 中的变量,这样我就不会在控制台中收到警告?

vba - 使用变量引用另一个工作表中的单元格

error-handling - Checkit 与书架,如何捕获错误并停止 DB 执行?

node.js - 在Node.js中使用try..catch进行错误处理