我的代码抛出了一个我不明白的错误

时间:2017-05-08 16:18:09

标签: python-3.x

所以,我一直在研究游戏之轮的python版本,到目前为止这里是代码

from random import choice
def spin_the_wheel():
    wheel = ["BANKRUPT", 650, "FREE PLAY", 700, "LOSE A TURN", 800, 500, 650, 500, 900, "BANKRUPT", 2500, 500, 900, 700, 600, 800, 500, 700, 500, 600, 550, 500, 900]
    return choice(wheel)
def make_text_blank(text):
    l = list(text)
    for i in range(len(text)):
        if text[i].isalpha():
            l[i] = "_"
    return "".join(l)
def wheel_of_fortune():
    print("The category for this puzzle is game show")
    text = "WHEEL OF FORTUNE"
    blank_text = make_text_blank(text)
    print(blank_text)
    print("Player 1 is up first")
    while blank_text != text:
        letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
        player1(text, blank_text, letters, 0, 0, 0)
def player1(text, blank_text, letters, player1amount, player2amount, player3amount):
    prize = spin_the_wheel()
    if prize == "BANKRUPT":
        print("Bankrupt")
        player1amount = 0
        print("We go to player 2 now")
        player2(text, blank_text, 0, player2amount, player3amount)
    elif prize == "LOSE A TURN":
        print("Lose a turn")
        print("We go to player 2 now")
        player2(text, blank_text, player1amount, player2amount, player3amount)
    elif prize == "FREE PLAY":
        print("Free Play")
        letter = input("Guess a letter: ").upper()
        if letter not in text:
            letters.remove(letter)
            print("There are no " + letter + "'s")
            print("We go to player 2 now")
            player2(text, blank_text, letters, player1amount, player2amount, player3amount)
        elif letter not in letters:
            print("That letter's already been called")
            print("We go to player 2 now")
            player2(text, blank_text, letters, player1amount, player2amount, player3amount)
        else:
            letters.remove(letter)
            if text.count(letter) == 1:
                print("There is 1 " + letter)
                for i in range(len(text)):
                    if text[i] == letter:
                        blank_text = blank_text.replace(blank_text[i], letter)
                print(blank_text)
                player1amount += 500
                def do():
                    choice = input("Would you like to spin the wheel, buy a vowel, or solve the puzzle?: ")
                    if choice == "spin the wheel":
                        print("OK, spin the wheel")
                        player1(text, blank_text, letters, player1amount, player2amount, player3amount)
                    elif choice == "buy a vowel":
                        vowel = input("OK, buy a vowel: ").upper()
                        if vowel not in text:
                            letters.remove(vowel)
                            print("There are no " + vowel + "'s")
                            print("We go to player 2 now")
                            player2(text, blank_text, letters, player1amount, player2amount, player3amount)
                        elif vowel not in letters:
                            print("That vowel's already been called")
                            print("We go to player 2 now")
                            player2(text, blank_text, letters, player1amount, player2amount, player3amount)
                        else:
                            letters.remove(vowel)
                            if text.count(vowel) == 1:
                                print("There is 1 " + vowel)
                                for i in range(len(text)):
                                    if text[i] == vowel:
                                        blank_text = blank_text.replace(blank_text[i], vowel)
                                print(blank_text)
                                do()
                            else:
                                n = text.count(vowel)
                                print("There are " + str(n) + " " + vowel + "'s")
                                for i in range(len(text)):
                                    if text[i] == vowel:
                                        blank_text = blank_text.replace(blank_text[i], vowel)
                                print(blank_text)
                                do()
                    elif choice == "solve the puzzle":
                        solution = input("OK, solve the puzzle: ").upper()
                        if solution == text:
                            print("That's right")
                            print(text)
                            print("Player 1 amount: $" + str(player1amount))
                            print("Player 2 amount: $" + str(player2amount))
                            print("Player 3 amount: $" + str(player3amount))
                        else:
                            print("That's not right")
                            print("We go to player 2 now")
                            player2(text, blank_text, letters, player1amount, player2amount, player3amount)
                do()
            else:
                n = text.count(letter)
                print("There are " + str(n) + " " + vowel + "'s")
                for i in range(len(text)):
                    if text[i] == letter:
                        blank_text = blank_text.replace(blank_text[i], letter)
                print(blank_text)
                p = 500 * n
                player1amount += p
                def do():
                    choice = input("Would you like to spin the wheel, buy a vowel, or solve the puzzle?: ")
                    if choice == "spin the wheel":
                        print("OK, spin the wheel")
                        player1(text, blank_text, letters, player1amount, player2amount, player3amount)
                    elif choice == "buy a vowel":
                        vowel = input("OK, buy a vowel: ").upper()
                        if vowel not in text:
                            letters.remove(vowel)
                            print("There are no " + vowel + "'s")
                            print("We go to player 2 now")
                            player2(text, blank_text, letters, player1amount, player2amount, player3amount)
                        elif vowel not in letters:
                            print("That vowel's already been called")
                            print("We go to player 2 now")
                            player2(text, blank_text, letters, player1amount, player2amount, player3amount)
                        else:
                            letters.remove(vowel)
                            if text.count(vowel) == 1:
                                print("There is 1 " + vowel)
                                for i in range(len(text)):
                                    if text[i] == vowel:
                                        blank_text = blank_text.replace(blank_text[i], vowel)
                                print(blank_text)
                                do()
                            else:
                                n = text.count(vowel)
                                print("There are " + str(n) + " " + vowel + "'s")
                                l = list(blank_text)
                                for i in range(len(text)):
                                    if text[i] == vowel:
                                        blank_text = blank_text.replace(blank_text, letter)
                                print(blank_text)
                                do()
                    elif choice == "solve the puzzle":
                        solution = input("OK, solve the puzzle: ").upper()
                        if solution == text:
                            print("That's right")
                            print(text)
                            print("Player 1 amount: $" + str(player1amount))
                            print("Player 2 amount: $" + str(player2amount))
                            print("Player 3 amount: $" + str(player3amount))
                        else:
                            print("That's not right")
                            print("We go to player 2 now")
                            player2(text, blank_text, letters, player1amount, player2amount, player3amount)
                do()

所以,这里的问题是,每次我猜一个字母,选择选项&#34;购买元音&#34;,并猜测文本中的元音,它会抛出这个错误:< / p>

Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    wheel_of_fortune()
  File "C:\Users\Harry\Documents\randompythonprograms\wheeloffortune.py", line 19, in wheel_of_fortune
    player1(text, blank_text, letters, 0, 0, 0)
  File "C:\Users\Harry\Documents\randompythonprograms\wheeloffortune.py", line 97, in player1
    do()
  File "C:\Users\Harry\Documents\randompythonprograms\wheeloffortune.py", line 82, in do
    blank_text = blank_text.replace(blank_text[i], vowel)
UnboundLocalError: local variable 'blank_text' referenced before assignment

我似乎无法弄清楚导致错误的原因,因为代码很大,所以也许你们其中一个人可以搞清楚

0 个答案:

没有答案