Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Python List Exercise Feedback

Hey guys I was learning python and I was stuck on this one problem. I have attempted it but my output does not meet the correct answer. Any reasons why and how to fix it?

Problem: 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.

My code:

total=0
list_of_players=[]
player=raw_input("Enter a player (end to stop): ")
while player !="end":
 list_of_players.append(int(player))
 player=raw_input("Enter a player (end to stop): ")
for x in range(len(list_of_players)):
 total+=1
 if list_of_players[x]==0 and len(list_of_players)==total:
 print "The game is valid."
if list_of_players[x]!=0 or len(list_of_players)!=total:
 print "The game is not valid. "

The problem states that if I enter players 4,3,0,5,6,2,1: It should be valid. My program displays this as invalid. Any ideas how I can improve my program? I got this practice question from programmersheaven.com if anyone is curious! Thanks!

Answer*

Draft saved
Draft discarded
Cancel
2
  • ì = 0 will do. The above code is not executable per se, as the initial value for i will always be end instead of a number. Commented Apr 22, 2013 at 0:31
  • @cyroxx I had it as that before and then i changed it when I though it only had to consider the player, I forgot to change it back thanks I will fix that Commented Apr 22, 2013 at 0:38

lang-py

AltStyle によって変換されたページ (->オリジナル) /