1

I'm learning Python and have hit a snag in this code. I have even pulled down the author's code and it has the same problem. :(

After importing the "random" module and the "games" module, which includes the "ask_number()" function and the class definition for a "Player," we have:

again = None
while again != "n":
    players = []
    num = games.ask_number(question = "How many players? (2-5): ", low = 2, high = 5)
    for i in range(num):
        name = input("Player name: ")
        score = random.randrange(100) + 1
        player = games.Player(name, score)
        players.append(player)

The "ask_number()" function looks like this:

def ask_number(question, low, high):
    """Ask for a number within a range."""
    response = None
    while response not in range(low, high):
        response = int(input(question))
    return response

When the program is run, however, the question "How many players? (2-5):" appears ad infinitum, regardless of what number is input as a response. Clearly it seems some kind of erroneous loop has been set up, but I can't figure out what it is for the life of me (that's why I'm an "absolute beginner," haha!).

Thanks in advance for returning my sanity to me! :)

EDITED: Since I thought the problem was merely with the syntax of the ask_number() function, I didn't want to append a lot of extraneous code. Learned that lesson! :) This is the full loop, so it does seem that again has a changeable value. (Note that the "ask_yes_no()" function is also in the "games" module.)

again = None
while again != "n":
    players = []
    num = games.ask_number(question = "How many players? (2-5): ", low = 2, high = 5)
    for i in range(num):
        name = input("Player name: ")
        score = random.randrange(100) + 1
        player = games.Player(name, score)
        players.append(player)
    print("\nHere are the game results:")
    for player in players:
        print(player)
    again = games.ask_yes_no("\nDo you want to play again? (y/n): ")
6
  • If you are using Python 2.7, you should use raw_input instead of input Commented Dec 29, 2013 at 21:55
  • you never set the variable again equal to "n". this results in your while loop iterating on into infinity and the "how many players" prompt to keep reappearing Commented Dec 29, 2013 at 21:59
  • Also note that 5 isn't actually in range(2, 5), as that's only 2-4. Commented Dec 29, 2013 at 22:55
  • That looks like it should work. Can you confirm that you tried to enter 2, 3, or 4 as the number of players and that you never see the "Player name: " prompt? Commented Dec 30, 2013 at 0:09
  • Confirmed, Anym. It just cycles endlessly on the how many players question, despite entering 2, 3, or 4 as the answer. Commented Dec 30, 2013 at 0:40

2 Answers 2

2

you are saying

while again != "n":

but you never set again! Because again never equals 'n' it never exits the loop

Sign up to request clarification or add additional context in comments.

Comments

0

You are never changing the value of again. In order to fix this you could add this

again = raw_input("Play again?: ")

at the end of the while loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.