Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Cannot assign a string to int

nattylinuxnattylinux Member Posts: 6
edited January 1 in Python

Whenever I try to assign a string to an int I get the following message:
Traceback (most recent call last):
File "poker.py", line 61, in
test_value1 = int (test_value)#convert the string to int
TypeError: int() argument must be a string or a number, not 'list'

My problem is on the very last line of code everything else works fine.
``
#program to assign suits to 13 cards numbered 1-13 representing Ace to King

import random

list_suits=['H','S','D','C']#Heart,Spade,Diamond,Clubs

list_values=['1','2','3','4','5','6','7','8','9','10','11','12','13']

final_list = []

for i in range (0,4):

for j in range(0,13):#adding card values and suits together
    final_list.append(list_suits[i]+'_'+list_values[j]) 

#shuffle the list
random.shuffle(final_list)

#choose the first 5 cards from the shuffled list as the first cards dealt to the user
print 'Cards Dealt\n'
print 'card1',final_list[0]
print 'card2',final_list[1]
print 'card3',final_list[2]
print 'card4',final_list[3]
print 'card5',final_list[4]

#split card names into two parts - suit and value
dealt_suit_and_value=[]

for m in range(0,5):
dealt_suit_and_value.append(final_list[m].split("_"))

#lists to hold the suit and value of dealt cards after they've been split into two parts
hold_value=[]
hold_suit=[]

#loop to fill the lists with their respective value and suit

for n in range(0,5):

c=dealt_suit_and_value[n]#value section
c=c[1:]
<a href="/search?Search=%23print&Mode=like">#print</a> 'C is:',c
<a href="/search?Search=%23c&Mode=like">#c</a> = int(c)
hold_value.append(c)
print '****Value:',hold_value[n]

d=dealt_suit_and_value[n]#suit section
d=d[:-1]
<a href="/search?Search=%23print&Mode=like">#print</a> 'D is:',d
hold_suit.append(d)
print 'Suit',hold_suit[n]

test_value = hold_value[0] #assign the first item in the list to a variable
print 'test value',test_value #to check the value of the variable
test_value1 = int (test_value)#convert the string to int
``

Comments

Sign In or Register to comment.