I have to check a finished tic tac toe game and I have to make sure that each row that the user inputs has 3 symbols. I'm having trouble doing this:
for i in ttt:
if i < len(3):
print("invalid board - too few symbols")
elif i > len(3):
print("invalid board - too many symbols")
else:
continue
ttt is the appended rows.
I get this message when I run it: if i < len(3): TypeError: object of type 'int' has no len()
The function len() doesn't take a value of type int as an argument: the argument may be a sequence (string, tuple or list) or a mapping (dictionary).
In this situation, you need to find the length of i using len(), and compare it to the integer 3.
resource: https://docs.python.org/2/library/functions.html#len
Related
i have to write a function that takes as an input a list and if to Consecutive numbers have the same sighn then they both go to a same list in lists if not a new list in list is opened for example:
[2,5,-3,-1,-1,3,-2,-2]->[[2,5]],[-3,-1,-1],[3],[-2,-2]]
this is my code :
def num_8(lst):
my_lst=[[lst[0]]]
j=0
for i in range(1,len(lst)):
if (lst[i]> 0 and lst[i-1]>0) or (lst[i]<0 and lst[i-1]<0):
my_lst[j].append(lst[i])
print(my_lst)
else:
j=j+1
my_lst[j].append([lst[i]])
return my_lst
print(num_8([2,5,-3,-1,-1,3,-2,-2]))
but i keep on getting
my_lst[j].append([lst[i]])
IndexError: list index out of range
and i dont know were i get out of range :( thanks
The error is with the else part.
my_lst[j] does not exisit at that point(The len of my_lst is j -1)
So you'll have to append the first number to my_lst and not my_lst[j] (Similar to what you have done in the first line of the function)
my_lst.append([lst[i]])
I'm still a beginner guys, so I don't know if what I'm doing is right...
In the code below I have a global Dictionary for player 1 and player 2. I then have a Function whereby player 1 chooses if he wishes to play as "x" or "o"... he/she is then assigned the choice, is there a way for me to automatically assign the opposite choice to the other player?
If this is a silly question, I apologize, I don't know
player1 = {"key1" : " "}
player2 = {"key2" : " "}
def start_game():
print('Welcome to Tic Tac Toe')
y = input("Player 1, select your marker (x/o): ")
only_choice = ['x', 'o']
if y in only_choice:
player1['key1'] = y
So I was able to run part of a program doing below (using tuples)
def reverse_string():
string_in = str(input("Enter a string:"))
length = -int(len(string_in))
y = 0
print("The reverse of your string is:")
while y != length:
print(string_in[y-1], end="")
y = y - 1
reverse_string()
The output is:
Enter a string:I Love Python
The reverse of your string is:
nohtyP evoL I
I am still thinking how for the program to reverse the position of the words instead of per letter.
The desired output will be "Phython Love I"
Is there anyway that I will input a string and then convert it to a tuple similar below:
So If I enter I love Phyton, a certain code will do as variable = ("I" ,"Love", "Python") and put additional codes from there...
Newbie Programmer,
Mac
i want to calculte the specfic numbers of words from the given sentence...words are alredy save in my dictonary and sentence is will be input from user.....
Here is my code.
from collections import Counter
Find_word= raw_input('Write Sentence:')
wordTosearch=['is', 'am']
sentence= Find_word.split()
cnt = Counter(sentence)
for k in sentence:
if k in wordTosearch:
print k, wordTosearch[k]
if cnt[wordTosearch]>1:
print "aggresive"
else:
print "Not agressive"
from collections import Counter
Find_word= raw_input('Write Sentence:')
sentence= Find_word.split()
cnt = Counter(sentence)
wordTosearch=['is', 'am']
for k in wordTosearch:
print k, cnt[k]
if all( cnt[x] > 1 for x in wordTosearch ):
print "aggresive"
else:
print "Not agressive"
I dont know what the bottom part should do though.
wordTosearch is a list of words.
The following is iterating through that list of words:
if k in wordTosearch:
print k, wordTosearch[k] # <----
where k is a word, and wordTosearch[k] is the attempt to access a list value by a string key which gives you "TypeError: list indices must be integers, not str".
You can not access list values by string indices as Python lists are "numbered" sequences
I used the split() function to convert string to a list time = time.split() and this is how my output looks like :
[u'1472120400.107']
[u'1472120399.999']
[u'1472120399.334']
[u'1472120397.633']
[u'1472120397.261']
[u'1472120394.328']
[u'1472120393.762']
[u'1472120393.737']
Then I tried accessing the contents of the list using print time[1] which gives the index out of range error (cause only a single value is stored in one list). I checked questions posted by other people and used print len(time). This is the output for that:
1
[u'1472120400.107']
1
[u'1472120399.999']
1
[u'1472120399.334']
1
[u'1472120397.633']
1
[u'1472120397.261']
1
[u'1472120394.328']
1
[u'1472120393.762']
1
[u'1472120393.737']
I do this entire thing inside a for loop because I get logs dynamically and have to extract out just the time.
This is part of my code:
line_collect = lines.collect() #spark function
for line in line_collect :
a = re.search(rx1,line)
time = a.group()
time = time.split()
#print time[1] #index out of range error which is why I wrote another for below
for k in time :
time1 = time[k]#trying to put those individual list values into one variable but get type error
print len(time1)
I get the following error :
time1 = time[k]
TypeError: list indices must be integers, not unicode
Can someone tell me how to read each of those single list values into just one list so I can access each of them using a single index[value]. I'm new to python.
My required output:
time =['1472120400.107','1472120399.999','1472120399.334','1472120397.633','1472120397.261','1472120394.328','1472120393.762','1472120393.737']
so that i can use time[1] to give 1472120399.999 as result.
Update: I misunderstood what you wanted. You have the correct output already and it's a string. The reason you have a u before the string is because it's a unicode string that has 16 bits. u is a python flag to distinguish it from a normal string. Printing it to the screen will give you the correct string. Use it normally as you would any other string.
time = [u'1472120400.107'] # One element just to show
for k in time:
print(k)
Looping over a list using a for loop will give you one value at a time, not the index itself. Consider using enumerate:
for k, value in enumerate(time):
time1 = value # Or time1 = time[k]
print(time1)
Or just getting the value itself:
for k in time:
time1 = k
print(time1)
--
Also, Python is zero based language, so to get the first element out of a list you probably want to use time[0].
Thanks for your help. I finally got the code right:
newlst = []
for line in line_collect :
a = re.search(rx1,line)
time = a.group()
newlst.append(float(time))
print newlst
This will put the whole list values into one list.
Output:
[1472120400.107, 1472120399.999, 1472120399.334, 1472120397.633,
1472120397.261, 1472120394.328, 1472120393.762, 1472120393.737]