Why doesn’t it just give the list itself?

I had this function

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>def calc_score(player):
"""Calculates the score of the specified player (player)"""
score = 0
if player == "user":
for card in range(0, len(user_cards)):
score += user_cards[int(card)]
elif player == "comp":
for card in range(0, len(comp_cards)):
score += comp_cards[int(card)]
else:
return
return score
</code>
<code>def calc_score(player): """Calculates the score of the specified player (player)""" score = 0 if player == "user": for card in range(0, len(user_cards)): score += user_cards[int(card)] elif player == "comp": for card in range(0, len(comp_cards)): score += comp_cards[int(card)] else: return return score </code>
def calc_score(player):
    """Calculates the score of the specified player (player)"""
    score = 0
    if player == "user":
        for card in range(0, len(user_cards)):
            score += user_cards[int(card)]
    elif player == "comp":
        for card in range(0, len(comp_cards)):
            score += comp_cards[int(card)]
    else:
        return
    return score

and when it was called it gave me this error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>line 27, in calc_score
score += user_cards[int(card)]
TypeError: unsupported operand type(s) for +=: 'int' and 'list'
</code>
<code>line 27, in calc_score score += user_cards[int(card)] TypeError: unsupported operand type(s) for +=: 'int' and 'list' </code>
line 27, in calc_score
    score += user_cards[int(card)]
TypeError: unsupported operand type(s) for +=: 'int' and 'list'

I debugged it in thonny and it turns out it said that ['placeholder1', 'placeholder2'][0] just gave the list itself.

I even tested it with seemingly equivalent code and it worked.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>user_cards = [11,10]
comp_cards = [10,8]
score = 0
if "user" == "user":
for card in range(0, len(user_cards)):
score += user_cards[int(card)]
elif "user" == "comp":
for card in range(0, len(comp_cards)):
score += comp_cards[int(card)]
else:
print()
print(score)
</code>
<code>user_cards = [11,10] comp_cards = [10,8] score = 0 if "user" == "user": for card in range(0, len(user_cards)): score += user_cards[int(card)] elif "user" == "comp": for card in range(0, len(comp_cards)): score += comp_cards[int(card)] else: print() print(score) </code>
user_cards = [11,10]
comp_cards = [10,8]

score = 0
if "user" == "user":
    for card in range(0, len(user_cards)):
        score += user_cards[int(card)]
elif "user" == "comp":
    for card in range(0, len(comp_cards)):
        score += comp_cards[int(card)]
else:
    print()
print(score)

Output: 21

I checked the variables and lists’ values and they were normal too.

I couldn’t find anything wrong.

Lastly here is the whole script (as of posting this answer):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import random
# Define Variables
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
user_cards = []
comp_cards = []
user_score = 0
comp_score = 0
# Define Functions
def give_card(num):
"""Returns random cards of a certain amount (num)"""
num = int(num)
cards_give = []
for card in range(0, num):
cards_give.append(random.choice(cards))
return cards_give
def calc_score(player):
"""Calculates the score of the specified player (player)"""
score = 0
if player == "user":
for card in range(0, len(user_cards)):
score += user_cards[int(card)]
elif player == "comp":
for card in range(0, len(comp_cards)):
score += comp_cards[int(card)]
else:
return
return score
user_cards.append(give_card(2))
comp_cards.append(give_card(2))
user_score = calc_score("user")
comp_score = calc_score("comp")
</code>
<code>import random # Define Variables cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] user_cards = [] comp_cards = [] user_score = 0 comp_score = 0 # Define Functions def give_card(num): """Returns random cards of a certain amount (num)""" num = int(num) cards_give = [] for card in range(0, num): cards_give.append(random.choice(cards)) return cards_give def calc_score(player): """Calculates the score of the specified player (player)""" score = 0 if player == "user": for card in range(0, len(user_cards)): score += user_cards[int(card)] elif player == "comp": for card in range(0, len(comp_cards)): score += comp_cards[int(card)] else: return return score user_cards.append(give_card(2)) comp_cards.append(give_card(2)) user_score = calc_score("user") comp_score = calc_score("comp") </code>
import random

# Define Variables

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
user_cards = []
comp_cards = []
user_score = 0
comp_score = 0

# Define Functions

def give_card(num):
    """Returns random cards of a certain amount (num)"""
    num = int(num)
    cards_give = []
    for card in range(0, num):
        cards_give.append(random.choice(cards))
    return cards_give


def calc_score(player):
    """Calculates the score of the specified player (player)"""
    score = 0
    if player == "user":
        for card in range(0, len(user_cards)):
            score += user_cards[int(card)]
    elif player == "comp":
        for card in range(0, len(comp_cards)):
            score += comp_cards[int(card)]
    else:
        return
    return score

user_cards.append(give_card(2))
comp_cards.append(give_card(2))
user_score = calc_score("user")
comp_score = calc_score("comp")

3

Your problem is here:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>user_cards.append(give_card(2))
</code>
<code>user_cards.append(give_card(2)) </code>
user_cards.append(give_card(2))

user_cards is a list, and give_card(2) returns another list of two numbers. Appending a list to a list makes the appended list itself be an element of the resulting list:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>list = [1, 2]
list.append([3,4])
print(list) # prints [1, 2, [3, 4]]
</code>
<code>list = [1, 2] list.append([3,4]) print(list) # prints [1, 2, [3, 4]] </code>
list = [1, 2]
list.append([3,4])
print(list) # prints [1, 2, [3, 4]]

What you need instead is

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>user_cards.extend(give_card(2))
</code>
<code>user_cards.extend(give_card(2)) </code>
user_cards.extend(give_card(2))

or

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>user_cards += give_card(2)
</code>
<code>user_cards += give_card(2) </code>
user_cards += give_card(2)

This will produce what you require in user_cards because the extend function or the += operator appends each element of the given list.

By the way, instead of iterating through indexes of a list, it’s more readable to iterate through its elements:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> for card in user_cards:
score += card
</code>
<code> for card in user_cards: score += card </code>
    for card in user_cards:
        score += card

But even better would be to use the sum function:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> score = sum(user_cards)
</code>
<code> score = sum(user_cards) </code>
    score = sum(user_cards)

2

Use the return value of give_card instead of appending to list and for card in range(0, len(comp_cards)): returns an index of comp_cards, you could use the value directly:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import random
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
user_cards = []
comp_cards = []
user_score = 0
comp_score = 0
# Define Functions
def give_card(num):
"""Returns random cards of a certain amount (num)"""
num = int(num)
cards_give = []
for card in range(0, num):
cards_give.append(random.choice(cards))
return cards_give
def calc_score(player):
"""Calculates the score of the specified player (player)"""
score = 0
if player == "user":
for card in user_cards:
score += card
elif player == "comp":
for card in comp_cards:
score += card
else:
return
return score
user_cards = give_card(2)
comp_cards = give_card(2)
user_score = calc_score("user")
comp_score = calc_score("comp")
print(user_score)
print(comp_score)
</code>
<code>import random cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] user_cards = [] comp_cards = [] user_score = 0 comp_score = 0 # Define Functions def give_card(num): """Returns random cards of a certain amount (num)""" num = int(num) cards_give = [] for card in range(0, num): cards_give.append(random.choice(cards)) return cards_give def calc_score(player): """Calculates the score of the specified player (player)""" score = 0 if player == "user": for card in user_cards: score += card elif player == "comp": for card in comp_cards: score += card else: return return score user_cards = give_card(2) comp_cards = give_card(2) user_score = calc_score("user") comp_score = calc_score("comp") print(user_score) print(comp_score) </code>
import random

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
user_cards = []
comp_cards = []
user_score = 0
comp_score = 0

# Define Functions


def give_card(num):
    """Returns random cards of a certain amount (num)"""
    num = int(num)
    cards_give = []
    for card in range(0, num):
        cards_give.append(random.choice(cards))
    return cards_give


def calc_score(player):
    """Calculates the score of the specified player (player)"""
    score = 0
    if player == "user":
        for card in user_cards:
            score += card
    elif player == "comp":
        for card in comp_cards:
            score += card
    else:
        return
    return score


user_cards = give_card(2)
comp_cards = give_card(2)
user_score = calc_score("user")
comp_score = calc_score("comp")
print(user_score)
print(comp_score)

Out:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>14
15
</code>
<code>14 15 </code>
14
15

The problem is at the line:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>user_cards.append(give_card(2))
comp_cards.append(give_card(2))
</code>
<code>user_cards.append(give_card(2)) comp_cards.append(give_card(2)) </code>
user_cards.append(give_card(2))
comp_cards.append(give_card(2))

you can update the code to use extend and also rather than using for loop to add it to the score, use sum() on the list.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import random
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
user_cards = []
comp_cards = []
user_score = 0
comp_score = 0
# Define Functions
def give_card(num):
"""Returns random cards of a certain amount (num)"""
num = int(num)
cards_give = []
for card in range(0, num):
cards_give.append(random.choice(cards))
return cards_give
def calc_score(player):
"""Calculates the score of the specified player (player)"""
score = 0
if player == "user":
score = sum(user_cards)
elif player == "comp":
score = sum(comp_cards)
else:
return
return score
user_cards.extend(give_card(2))
comp_cards.extend(give_card(2))
print(user_cards)
print(comp_cards)
user_score = calc_score("user")
comp_score = calc_score("comp")
print(user_score)
print(comp_score)
</code>
<code>import random cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] user_cards = [] comp_cards = [] user_score = 0 comp_score = 0 # Define Functions def give_card(num): """Returns random cards of a certain amount (num)""" num = int(num) cards_give = [] for card in range(0, num): cards_give.append(random.choice(cards)) return cards_give def calc_score(player): """Calculates the score of the specified player (player)""" score = 0 if player == "user": score = sum(user_cards) elif player == "comp": score = sum(comp_cards) else: return return score user_cards.extend(give_card(2)) comp_cards.extend(give_card(2)) print(user_cards) print(comp_cards) user_score = calc_score("user") comp_score = calc_score("comp") print(user_score) print(comp_score) </code>
import random
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
user_cards = []
comp_cards = []
user_score = 0
comp_score = 0

# Define Functions

def give_card(num):
    """Returns random cards of a certain amount (num)"""
    num = int(num)
    cards_give = []
    for card in range(0, num):
        cards_give.append(random.choice(cards))
    return cards_give


def calc_score(player):
    """Calculates the score of the specified player (player)"""
    score = 0
    if player == "user":
        score = sum(user_cards)
    elif player == "comp":
        score = sum(comp_cards)
    else:
        return
    return score

user_cards.extend(give_card(2))
comp_cards.extend(give_card(2))
print(user_cards)
print(comp_cards)

user_score = calc_score("user")
comp_score = calc_score("comp")
print(user_score)
print(comp_score)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật