【问题标题】:Python - "Local x variable referenced before assignment" (CLOSED)Python - “分配前引用的局部 x 变量”(已关闭)
【发布时间】:2015-11-16 07:28:21
【问题描述】:

我的 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!"

【问题讨论】:

  • 使用调试器,你就会知道为什么if的内容没有被执行。
  • 很抱歉,我不知道如何使用调试器。但感谢您的回答。我会看看我能做什么。
  • 那么这听起来像是学习如何做到这一点的最佳时机,除非您当然希望将所有调试问题都作为 SO 问题提出。你可以从阅读this excellent article by Eric Lippert about debugging small programs such as yours.开始。
  • @JoãoPinhal 使用调试器对于学习编程至关重要。如果您要求其他人为您解决此类问题,您将一无所获。
  • 好的,谢谢您的建议。

标签: python variables error-handling


【解决方案1】:

嗯。调试器被高估了——我好几年都没有在 Python 中使用过调试器。只需在你的大 if/elif 语句中添加最后一个 else 子句:

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

如果没有命中,那么这意味着您可能在其他地方有错字。不管怎样,你当然可以添加更多的调试语句——如果需要,每个 elif 子句一个。

这又引出了另一点——你有很多 if/elif 子句。

您可能想要研究诸如带有可以相加的值的 dicts 或可以调用的函数之类的东西,以减少您需要的 if/else 语句的数量。

【讨论】:

  • "dicts" 是 "dictionaries" 的简写——键/值映射对象
猜你喜欢
  • 2018-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多