简体   繁体   中英

How to stop while loop from repeating?

Okay so I am trying to make a game of sticks with python. I am asking the user the amount of sticks for him to input.

I am supposed to include a condition where if the user does input a number between 10 and 100, it will ask him over and over again until he gives in a number between that amount. However, my code ends whenever I do input a number between that such amount.

Also, my initial problem is how to ask the player how many sticks will they take after they select the like amount. It just repeats back to the same question, asking them how many sticks do they wish to have in the game. My initial problem is how to advance to the next part.

print("Welcome to the game of sticks, choose wisely...")
sticks = int(input("Choose the number of sticks(10-100 ): "))

while(sticks >= 10 and sticks <= 100 ):
    print("There are %d sticks on the board." % sticks)
    sticks = int(input("Choose the number of sticks(10-100 ): "))

take = int(input("How many sticks will you take?(1-3): "))
while(take >= 1 and take <= 3):
    print(sticks - take)
    take = int(input("How many sticks will you take?(1-3): "))

Does anyone have any familiarity with programming a sticks game? Please don't give me the whole output, just tell me what's wrong. What should I do to make it work?

On this line you test if sticks is a valid number and if it is you stay in the while loop. This seems to be your logic error. You are essentially staying in your while loop when you enter a valid number when you want it the other way around.

#your code
while(sticks >= 10 and sticks <= 100 ):
     print("There are %d sticks on the board." % sticks)
     sticks = int(input("Choose the number of sticks(10-100 ): "))

in the example below you test if the input is valid and if it is you don't enter the loop. If the input isn't valid you stay in the loop until a valid number is entered.

#updated code
while(sticks < 10 or sticks > 100 ):
    print("There are %d sticks on the board." % sticks)
    sticks = int(input("Choose the number of sticks(10-100 ): "))

Your first while asks how many sticks you want. you need an if statement that says if the number is acceptable, break, else, do it again. The second while loop asks how many to take out. It shouldn't repeat. Try using if else statements

You use break

Try This: https://wiki.python.org/moin/WhileLoop

For example

while(sticks >= 10 and sticks <= 100 ):
print("There are %d sticks on the board." % sticks)
sticks = int(input("Choose the number of sticks(10-100 ): "))
      break

take = int(input("How many sticks will you take?(1-3): "))
while(take >= 1 and take <= 3):
print(sticks - take)
take = int(input("How many sticks will you take?(1-3): "))
    break

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM