Home > OS >  How to break a loop over user input
How to break a loop over user input

Time:12-19

I've recently started studying python and I have this issue with a problem. I need to break a loop over user input. Pretty much what i need right now is: To break the grams_per_cat input loop when it reaches the cat_count. Pretty much the excercise is cat_count is the number of cats, grams per cat is how much a cat eats per day for each cat. So when it hits for example cat_count is 6 so after 6 inputs i want to break the input loop.

cat_count = int(input())

while True:
    grams_per_cat = int(input())

CodePudding user response:

Define a counter and use it to break on a certain condition:

cat_count = int(input())
# Our counter
i = 0
while True:
    if i >= cat_count:
       # Interrupt the loop if we reached cat_count
       break
    grams_per_cat = int(input())
    # Increase counter
    i  = 1
  • Related