argument of type 'int' is not iterable ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

argument of type 'int' is not iterable ?

import random def deal_card(): cards=[11,2,3,4,5,6,7,8,9,10,10,10,10] card=random.choice(cards) return card """"Retruns a random cards""""" def calculate_score(cards): if 11 in sum(cards)==21 and len(cards)==2: return 0 if 11 in cards and sum(cards)>21: cards.remove(11) cards.append(1) return cards user_cards=[] computer_cards=[] is_game_over=False for _ in range(2): new_card=deal_card() user_cards.append(new_card) computer_cards.append(deal_card) while not is_game_over: user_score=calculate_score(user_cards) computer_score=calculate_score(computer_cards) print(f" Your cards:{user_cards}") print(f"Computer's First card:{computer_cards}") if user_score==0 or computer_score==0 or user_score>21: is_game_over=True else: user_should_deal=input("Type 'y ' to get another card, type 'n' to pass: ") if user_should_deal=="y": user_cards.append(deal_card()) while computer_score!=0 and computer_score<17: computer_cards.append(deal_card()) computer_score=calculate_score(computer_cards)

19th Nov 2021, 6:21 AM
shahid momin
shahid momin - avatar
7 Answers
+ 2
This line is likely the culprit: if 11 in sum(cards)==21 and len(cards)==2: sum(cards) evaluates to an integer, whereas the '11 in...' expression expects an iterable like a list or tuple, et al, to follow. Having a blunder like that also hints there may be more issues in the rest of the code, but I stopped reading at that line.
19th Nov 2021, 7:06 AM
Brian
Brian - avatar
+ 1
if you want to iterate like x times like if you need a for loop iterator you can simply for number in range(starting number, ending number, increase or i forgot what its called) anyways example for i in range(0,100,1): print("this print"+str(i)) this will print this is <the number of print> 100 times
19th Nov 2021, 1:09 PM
Ireneo language
0
Do you have any suitable code for it because I am beginner for it
19th Nov 2021, 6:38 AM
shahid momin
shahid momin - avatar
0
I don't know ?
19th Nov 2021, 6:41 AM
shahid momin
shahid momin - avatar
0
Did you know Angela yu she wrote this code
19th Nov 2021, 6:42 AM
shahid momin
shahid momin - avatar
0
Ok👍 thanks for answering
19th Nov 2021, 6:45 AM
shahid momin
shahid momin - avatar
0
The error given, "Argument of type int is not iterable" indicates that your code is trying to iterate an integer, float or boolean. Strings and lists are examples of iterable items because they are designed to be able to be inspected item by item. But a number is an entity. if you wish to break a number into iterable items, then convert it to a string
19th Nov 2021, 9:33 AM
Rik Wittkopp
Rik Wittkopp - avatar