We were given this assignment:
"The game of Assassin is a simple game played on university campuses where each player is assigned a target to assassinate by simply saying “you’re dead” to them. Of course with thousands of people on campus and only a few dozen in the game you never know who is looking to assassinate you. Once you assassinate someone you take on the target they were looking for. If this happens to be you then you are the winner. To ensure that this works properly the targets must form a continuous “chain.” Write a program that allows the user to enter their target assignments and output whether it is valid or not. based on if there is a continuous “chain.” Each person in the list is represented by position in the list. The value at the position is their target. E.g.
0 1 2 3 4 5 6
4 3 0 5 6 2 1 Valid"
Here is my code:
people=[]
steps=[]
valid=True
while True:
person=int(input("Enter the target(-1 to exit):"))
if person==-1: break
people.append(person)
for i in range(len(people)):
if len(people[0])!=0:
valid=False
break
elif len(people[0])==0: break
steps.append(people[0])
for i in range(len(jumps)):
if jumps[i-1]==jumps[i]:
valid=False
if valid==False: #program MUST check if the flag is false before checking for length
print('invalid') #of the jumps list compared to the victims list.
elif len(jumps)==len(victims):
print('valid')
elif len(jumps)!=len(victims):
print('invalid')
Currently, I get this error:
File "C:/Users/Wisdom1/Desktop/ListAssign6.py", line 14, in
<module>
if len(people[0])!=0:
TypeError: object of type 'int' has no len()
I do not know why this is but if someone could explain the occurrence of this error statement to me, that would be greatly appreciated. Also, if there are any other errors that are in this code please let me know. The output that I would like to achieve would be that any sequence input-ed other than 4305621, the program will output that it is invalid. Thanks.
people
is a list. people[0]
however is an integer. And, as your error says, you can't use len
with integers since they are non-iterable.
If you want to see if the length of people
is not 0
, remove [0]
:
if len(people)!=0:
Or, if you want to see if people[0]
does not equal 0
, remove len
:
if people[0]!=0: