简体   繁体   English

不能使用另一个函数的变量

[英]Can't use a variable from another function

import random
import oart
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
#generating two random nubers for both user and dealer
users_cards=[random.choice(cards),random.choice(cards)]
comp_cards=[random.choice(cards)]
usr_chance=True
comp_turn=False
print(oart.logo)
total_u=0
total_c=0
def blackjack():
    def usr_turn(user_chance,users_cards,comp_cards,comp_turn):
    while user_chance is True:
        #finding total 
        total_u=sum(users_cards)
        total_c=sum(comp_cards)
        #printing cards and totalscore
        def totalscore(users_cards,total_u,comp_cards,total_c):
            print(f"Your cards = {users_cards}. Current Score= {total_u}\nComputer's cards={comp_cards}. Current score={total_c}")
        totalscore(users_cards,total_u,comp_cards,total_c)
        #checking if the user/computer got a blackjack or not
        if int(total_u)==21:
            print(f"You won. You got a blackjack \n")
            return        
        elif int(total_c)==21:
            print(f"You lost. Dealer got a blackjack \n")
            return
        def draw():
            users_cards.append(random.choice(cards))
        def draw1():
            comp_cards.append(random.choice(cards))
        for i in users_cards:
            if i == 11:
                if total_u-10 <=21:
                    users_cards[users_cards.index(i)]=1
                elif total_u-10>21:
                    print("You lost. You gone over 21")
                    return
            break
        if total_u<21:
            if input("Press 'y' to draw another card and 'n' if u dont want to draw\n")=="y":
                draw()
                totalscore(users_cards,total_u,comp_cards,total_c)
                user_chance=True
                return
            else:
                while total_c<17:
                    draw1()
                    total_c=sum(comp_cards)
                totalscore(users_cards,total_u,comp_cards,total_c)
        elif total_u>21:
            print("You lost. You busted")
            return 
        elif total_u==21:
            print("You won. You got a backjack")
            return
    usr_turn(usr_chance,users_cards,comp_cards,comp_turn)
    def comparision():    
    def totalscore(users_cards,total_u,comp_cards,total_c):
            print(f"Your cards = {users_cards}. Current Score= {total_u}\nComputer's cards={comp_cards}. Current score={total_c}")
    if total_c>total_u:
        print("You lost. Dealer has a higher score")
        return
    elif total_c==total_u:
        totalscore(users_cards,total_u,comp_cards,total_c)    
        print("Tie")
        return
    elif total_u>total_c:
        print("You won. You have a higher score than dealer")
        return
  comparision()
blackjack()

i am making a blackjack game and to calculate the total of user cards and computer cards I used the variables total_u and total_c.我正在制作一个二十一点游戏并计算用户卡和计算机卡的总数,我使用了变量 total_u 和 total_c。 I initialized the total_u and total_c in the global and i modified it in the usr_turn() but after that i cannot use it in comparision(), the total_u and total_c in comparison returns 0 .我在全局中初始化了 total_u 和 total_c 并在 usr_turn() 中修改了它,但之后我不能在 comparision() 中使用它,比较中的 total_u 和 total_c 返回 0 。

thanks for any help谢谢你的帮助

Use the global keyword inside usr_turn :usr_turn使用global关键字:

global total_u
global total_c

total_u = sum(users_cards)
total_c = sum(comp_cards)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM