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
4
  • The exercise states I can only use what I have learned so far, and that does not include max () or set (). Is there another way I can do this problem? Commented Apr 21, 2013 at 21:34
  • Also, may you provide a code sample of how "if 0 in list_of_players" would look in this program? Commented Apr 21, 2013 at 21:44
  • Could you post a link to where exactly the exercise can be found (maybe by adding it to your question)? This would help to see what you should have learned so far. Commented Apr 21, 2013 at 22:20
  • The link is a direct download from programmersheaven.com. I have learned if statements, loops, and lists. Commented Apr 21, 2013 at 22:30

lang-py

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