为什么用户分数和计算机分数没有被相加?(我认为)

huangapple go评论61阅读模式
英文:

Why is user_score and computer_score not being added to? (i think)

问题

所以我正在编写一个二十一点游戏,我创建了一个名为user_score和computer_score的列表。我使用random模块从名为cards的列表中选择一个随机整数。但是当我使用.append()将从cards中随机选择的卡添加到user_card/computer_card时,似乎并没有将随机卡添加进去?这是我定义它的地方以及我使用random模块的地方:

    import random
    
    user_score = 0
    computer_score = 0
    
    cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
    
    user_cards = []
    computer_cards = []

    def deal_card():
        cards_left = 2
        while not cards_left == 0:
            random_user = random.choice(cards)
            random_computer = random.choice(cards)
            user_cards.append(random_user)
            computer_cards.append(random_computer)
            cards_left -= 1
            print(user_score, computer_score)

最后,这是我调用该函数的地方:

    deal_card()
    calculate_score(card_list=[user_score, computer_score])

calculate_score在这里定义:

    def calculate_score(card_list):
        user_score = sum(user_cards)
        computer_score = sum(computer_cards)
    
        if computer_cards.count(11) > 0 and computer_cards.count(10) > 0:
            computer_score = 0
        elif user_cards.count(11) > 0 and user_cards.count(10) > 0:
            user_score = 0  
        if user_cards.count(11) > 0:
            cards.remove(11)
            cards.append(1)
        elif computer_cards.count(11) > 0:
            cards.remove(11)
            cards.append(1)
        return user_score

PS: 我仍在学习Python,所以请不要变得太高级。

英文:

So I'm, coding a blackJack game,and I made a list called user_score and computer_score. I used the random module to choose a random int from a list called cards. But when I use .append() to add the random choice from cards, it doesn't appear to be adding the random card to user_card / computer_card? Here is where I define it, and where I use the random module:

import random

user_score = 0
computer_score = 0

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

user_cards = []
computer_cards = []



def deal_card():
cards_left = 2
while not cards_left == 0:
    random_user = random.choice(cards)
    random_computer = random.choice(cards)
    user_cards.append(random_user)
    computer_cards.append(random_computer)
    cards_left -= 1
    print(user_score, computer_score)

and finally, this is where I call the function:

deal_card()
calculate_score(card_list=[user_score, computer_score])

calculate_score is defined here:

def calculate_score(card_list):
user_score = sum(user_cards)
computer_score = sum(computer_cards)

if computer_cards.count(11) > 0 and computer_cards.count(10) > 0:
    computer_score = 0
elif user_cards.count(11) > 0 and user_cards.count(10) > 0:
    user_score = 0  
if user_cards.count(11) > 0:
    cards.remove(11)
    cards.append(1)
elif computer_cards.count(11) > 0:
    cards.remove(11)
    cards.append(1)
return user_score

PS: I'm still learning python, so please don't go to advanced

答案1

得分: 3

问题在于变量user_cardscomputer_cards不在deal_card()calculate_score()的范围内。正如前面的答案中提到的,解决这个问题的一种方法是使用全局变量。解决这个问题的另一种方式是在函数中通过传递和返回变量:

import random

# 将卡牌变量传递给函数
def deal_card(cards):
    cards_left = 2
    user_cards = []
    computer_cards = []
    while not cards_left == 0:
        random_user = random.choice(cards)
        random_computer = random.choice(cards)
        user_cards.append(random_user)
        computer_cards.append(random_computer)
        cards_left -= 1
        print(user_score, computer_score)
    # 返回user_cards和computer_cards数组
    return user_cards, computer_cards

# 将user_cards和computer_cards数组传递给函数
def calculate_score(user_cards, computer_cards):
    user_score = sum(user_cards)
    computer_score = sum(computer_cards)

    if computer_cards.count(11) > 0 and computer_cards.count(10) > 0:
        computer_score = 0
    elif user_cards.count(11) > 0 and user_cards.count(10) > 0:
        user_score = 0
    if user_cards.count(11) > 0:
        cards.remove(11)
        cards.append(1)
    elif computer_cards.count(11) > 0:
        cards.remove(11)
        cards.append(1)
    # 返回user_score和computer_score
    return user_score, computer_score

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

# 使用cards数组调用deal_card
user_cards, computer_cards = deal_card(cards)

# 使用从deal_cards返回的user_cards和computer_cards调用calculate_score
user_score, computer_score = calculate_score(user_cards, computer_cards)
英文:

The issue is that the variables user_cards and computer_cards are not in the scope of deal_card() or calculate_score(). As mentioned in the previous answers, one way to deal with this problem is by using global variables. Another way to solve this, would be by passing and returning variables in your functions:

import random

# Pass cards variable to function
def deal_card(cards):
    cards_left = 2
    user_cards = []
    computer_cards = []
    while not cards_left == 0:
        random_user = random.choice(cards)
        random_computer = random.choice(cards)
        user_cards.append(random_user)
        computer_cards.append(random_computer)
        cards_left -= 1
        print(user_score, computer_score)
    # return user_cards and computer_cards array
    return user_cards, computer_cards

# Pass user_cards and computer_cards array to function
def calculate_score(user_cards, computer_cards):
    user_score = sum(user_cards)
    computer_score = sum(computer_cards)

    if computer_cards.count(11) > 0 and computer_cards.count(10) > 0:
        computer_score = 0
    elif user_cards.count(11) > 0 and user_cards.count(10) > 0:
        user_score = 0
    if user_cards.count(11) > 0:
        cards.remove(11)
        cards.append(1)
    elif computer_cards.count(11) > 0:
        cards.remove(11)
        cards.append(1)
    # return user_score and computer_score
    return user_score, computer_score

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

# call deal_card with cards array
user_cards, computer_cards = deal_card(cards)

# call calculate_score with user_cards and computer_cards returned from deal_cards
user_score, computer_score = calculate_score(user_cards, computer_cards)

答案2

得分: 2

In Python, any variable that you set in a function is a separate local variable in that function. To access the global variable instead, you have to write a global statement like this:

def calculate_score(card_list):
    global user_score, computer_score
    user_score = sum(user_cards)
    computer_score = sum(computer_cards)
    ...

If you don't set a variable but only read it, it uses the global one, which is why you can access user_cards and computer_cards inside the function. cards.remove does not actually change the cards variable but just reads it to see which list to edit, and then edits that list, which is why it accesses the global cards.

英文:

In Python, any variable that you set in a function is a separate local variable in that function. To access the global variable instead, you have to write a global statement like this:

def calculate_score(card_list):
    global user_score, computer_score
    user_score = sum(user_cards)
    computer_score = sum(computer_cards)
    ...

If you don't set a variable but only read it, it uses the global one, which is why you can access user_cards and computer_cards inside the function. cards.remove does not actually change the cards variable but just reads it to see which list to edit, and then edits that list, which is why it accesses the global cards.

答案3

得分: 2

你没有更新全局变量user_scorecomputer_score。要解决这个问题,你需要在calculate_score函数内将这些变量声明为global。以下是更新后的calculate_score函数:

def calculate_score():
    global user_score
    global computer_score
    user_score = sum(user_cards)
    computer_score = sum(computer_cards)

    if computer_cards.count(11) > 0 and computer_cards.count(10) > 0:
        computer_score = 0
    elif user_cards.count(11) > 0 and user_cards.count(10) > 0:
        user_score = 0  
    if user_cards count(11) > 0:
        cards.remove(11)
        cards.append(1)
    elif computer_cards.count(11) > 0:
        cards.remove(11)
        cards.append(1)

此外,调用该函数时不需要传递任何参数。因为你正在使用全局变量,我认为你不需要向calculate_score函数传递任何参数。

deal_card()
calculate_score()
英文:

You are not updating the global variables user_score and computer_score. To fix this issue, you need to declare these variables as global within the calculate_score function. Following is the updated calculate_score function:

def calculate_score():
    global user_score
    global computer_score
    user_score = sum(user_cards)
    computer_score = sum(computer_cards)

    if computer_cards.count(11) > 0 and computer_cards.count(10) > 0:
        computer_score = 0
    elif user_cards.count(11) > 0 and user_cards.count(10) > 0:
        user_score = 0  
    if user_cards.count(11) > 0:
        cards.remove(11)
        cards.append(1)
    elif computer_cards.count(11) > 0:
        cards.remove(11)
        cards.append(1)

Also, call the function without passing any arguments. Since you are using the global variables I don't think you need to pass any arguments to calculate_score function.

deal_card()
calculate_score()

huangapple
  • 本文由 发表于 2023年3月31日 02:16:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75891664.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定