UnboundLocalError: локальная переменная 'player_card', на которую ссылаются до назначения - PullRequest
0 голосов
/ 28 октября 2018

Я делаю текстовую игру teen-patti, и произошла ошибка UnboundLocalError.Я новичок в Python, так что извините за мой нечистый код.Все ваши подсказки будут очень полезными.И самое главное, я действительно хочу понять, почему и как произошла эта ошибка, потому что, как я понимаю, я понятия не имею об этой ошибке.

#####Coding Teen-patti in text-Based####
global total_amount
total_amount=850
play_again='play'

Rule_text="""\t Welcome to the Game, "The Three card Collision". This game is coded by python programmer , Sushrant Rijal. He reserves all the intellectual right for this 
\n\tThe game main concept is derived from famous cards game "Teen Patti", however some changes are done,so you can see some difference here. Rules are:
1. \n\tFirst the game will check Trail.(i.e. if all cards are same, and if so, the sequence will start like A is the highest followed by K,Q,J,10...2.
2. \n\tIf first constraints fail to determine winner, progam will check if they contain a same pair of cards and if both of them contains same pair of card, winner will determine by seeing which pair sums more and in so A will get the value of 15, followed by K=13,Q=12,J=11,10.....2
3. \n\tIf both condition fails, program will go for comparison of highest single card between them and by any chance, the highest card also match the program will go for last check.
4. \n\tIn last check, we will sum of the value of card as listed in option 3 and compare in both, and if these condition also mathced, the game will be tied.
### \n\t your initial balance will be $850 and the least amount you can bet is 1$
"""
def tutsornot():
    temp=input("Do you want help????").casefold()
    if temp.startswith('y'):
        print(Rule_text)
import time
import random
from random import shuffle
user_name=input("Hey user... what's your kind name\n")
print(f'\n\n\nGood luck {user_name}. Have a great game!')
time.sleep(1)
global player_card
global computer_card
player_card=[1,2,3]
computer_card=[1,2,3]
global Winner
Winner=''
def bank_statement(x):
    total_amount+=x
    return total_amount
def get_bet_amount():
    global bet_amount
    try:
        bet_amount=int(input("Enter the bet amount.(don't include dollar sign)\n"))
        if bet_amount<1:
            print('Oops1 Invalid Input.\nRemember minimum bet amount is 1, and only positive amount valids. ')
            get_bet_amount()
    except:
        print("Oops! Invalid Input.\nRemember only integers are valid\n")
        time.sleep(4)
        get_bet_amount()
    else:
        print('Bet Amount Registered... ')
        time.sleep(2)
    return bet_amount
def card_process():
    print('Card process function\n')
    global player_card
    global computer_card
    cards_in_deck="23456789AQKJ"*4
    cards_in_deck=cards_in_deck.split()
    ten=[10,10,10,10]
    cards_in_deck=cards_in_deck+ten
    random.shuffle(cards_in_deck)
    for i in range(3):
        player_card[i]=random.choice(cards_in_deck)
        computer_card[i]=random.choice(cards_in_deck)
        random.shuffle(cards_in_deck)
        return player_card and computer_card
    print(player_card)
    print(computer_card)
def trail_check():
    for i in range(1):
        if player_card[i]==player_card[i+1]==player_card[i+2] and computer_card[i]==computer_card[i+1]==computer_card[i+2]:
            pair_check()
        elif player_card[i]==player_card[i+1]==player_card[i+2] and not(computer_card[i]==computer_card[i+1]==computer_card[i+2]):
            Winner='Player'
        elif not(player_card[i]==player_card[i+1]==player_card[i+2]) and computer_card[i]==computer_card[i+1]==computer_card[i+2]:
            Winner='Computer'
        else:
            pair_check()
    return Winner
def pair_check():
    for i in range(1):
            if ((player_card[i]==player_card[i+1] or player_card[i+2]) or (player_card[i+1]==player_card[i+2])) and ((computer_card[i]==computer_card[i+1] or computer_card[i+2]) or (computer_card[i+1]==computer_card[i+2])):
                    highest_card_check()
            elif ((player_card[i]==player_card[i+1] or player_card[i+2]) or (player_card[i+1]==player_card[i+2])) and not((computer_card[i]==computer_card[i+1] or computer_card[i+2]) or (computer_card[i+1]==computer_card[i+2])):
                    Winner='Player'
            elif not((player_card[i]==player_card[i+1] or player_card[i+2]) or (player_card[i+1]==player_card[i+2])) and ((computer_card[i]==computer_card[i+1] or computer_card[i+2]) or (computer_card[i+1]==computer_card[i+2])):
                    Winner='Computer'
            else:
                    highest_card_check()
    return Winner
def highest_card_check():
    alpha=['A','K','Q','J']
    global player_card_backup
    global computer_card_backup
    for i in range(3):
            if player_card[i]=='A':
                    player_highest_card='A'
            elif player_card[i]=='K':
                    player_highest_card='K'
            elif player_card[i]=='Q':
                    player_highest_card='Q'
            elif player_card[i]=='J':
                    player_highest_card='J'
            else:
                    for i in range(len(player_card)):
                            if  player_card[i] in alpha:
                                    player_card.remove(player_card[i])
                    player_highest_card_temp=max(int(player_card[0]),int(player_card[1]),int(player_card[2]))
    for i in range(3):
            if computer_card[i]=='A':
                    computer_highest_card='A'
            elif computer_card[i]=='K':
                    computer_highest_card='K'
            elif computer_card[i]=='Q':
                    computer_highest_card='Q'
            elif computer_card[i]=='J':
                    computer_highest_card='J'
            else:
                    for i in range(len(computer_card)):
                            if  computer_card[i] in alpha:
                                    computer_card.remove(computer_card[i])
                    computer_highest_card_temp=max(int(computer_card[0]),int(computer_card[1]),int(computer_card[2]))
    if player_highest_card_temp == computer_highest_card_temp:
            final_check_sum_check()
            player_card = player_card_backup
            computer_card = computer_card_backup
    else:
            if player_highest_card=='A':
                    Winner='Player'
            elif computer_highest_card=='A':
                    Winner='Computer'
            elif player_highest_card=='K':
                    winnner='Player'
            elif computer_highest_card=='K':
                    winner='Computer'
            elif player_highest_card=='Q':
                    winnner='Player'
            elif computer_highest_card=='Q':
                    winner='Computer'
            elif player_highest_card=='J':
                    winnner='Player'
            elif computer_highest_card=='J':
                    winner='Computer'
            else:
                    if int(player_highest_card) > int(computer_highest_card):
                            Winner='Player'
                    elif int(computer_highest_card)>int(player_highest_card):
                            Winner='Computer'
                    else:
                            final_check_sum_check()
                            Winner=''
                            player_card=player_card_backup
                            computer_card=computer_card_backup
    return Winner

def final_check_sum_check():
    num_list=[2,3,4,5,6,7,8,9]
    player_score=0
    computer_score=0
    for i in range(3):
            if player_card[i] in num_list:
                    player_score=player_score+int(player_card[i])
            else:
                    if player_card[i]=='A':
                            player_score+=15
                    elif player_card[i]=='K':
                            player_score+=13
                    elif player_card[i]=='Q':
                            player_score+=12
                    elif player_card[i]=='J':
                            player_score+=11
            if computer_card[i] in num_list:
                    computer_score=computer_score+int(computer_card[i])
            else:
                    if computer_card[i]=='A':
                            computer_score+=15
                    elif computer_card[i]=='K':
                            computer_score+=13
                    elif computer_card[i]=='Q':
                            computer_score+=12
                    elif computer_card[i]=='J':
                            computer_score+=11
    if player_score == computer_score:
            Winner='Tie'
    elif player_score > computer_score:
            Winner='Player'
    elif computer_score > player_score:
            Winner='Computer'
    return Winner
def result_printer():
    Winner.casefold()
    if Winner.startswith('p'):
            print('You  Wins\n')
            bank_statement(bet_amount*2)
            print('Amount Debited in your Account:{bet_amount*2}\n and your new balance is: {total_amount} ')
    elif Winner.startswith('c'):
            print('You lose\n')
            bank_statement(-bet_amount)
            print('Amount Credited from your Account:{bet_amount}\n and your new balance is:{total_amount}')
    else:
            print('Match Tied')
            print('No affect in your amount\n. Your current balance is: {total_amount}')
    print(player_card,computer_card)
    time.sleep(6)
    play_again=input("Play Again???").casefold()
    if play_again.startswith('y'):
            play_again=='exit'
def start():
    tutsornot()
    get_bet_amount()
    card_process()
    trail_check()
    pair_check()
    highest_card_check()
    final_check_sum_check()
    result_printer()
start()
...