Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to break a loop over user input

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())

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading