How to stop asking for input after reaching a specific amount of elements in a list?

Question:

So I would to use python for a user to enter numbers in sequence lets say ratings

for example

1
2
99

and save those ratings as elements in a list

but I want to add two restrictions the user can enter only up to 30 ratings, by up to I mean that the user can enter only two ratings, three, twenty and so on and so fortu but no more than 30 ratings.

And I want to ratings value to be a two digit integer bigger than 0 and smaller than 100

So I would like to have something like:

“please enter your ratings:”

and if a user enters e.g 199 or ADFADF then I want to have an error message and ask to repeat a valid value

or if the user tries to enter more than 30 ratings I would like the program to print a second error message and stop asking of additional input (and save all previous input to the list).

Asked By: papajo

||

Answers:

For the validation section you can use printy, this library comes with a function that does that for you, first, install printy:
pip install printy

Then import this function
from printy import inputy

Now, you have to create a loop that ends when the user has entered up to 30 valid values, and also, another loop that checks whether the value is a two digits value or not. So, something like this:

from printy import inputy

ratings = []

while len(ratings) < 30:
    valid_rating = 100  # let's use an invalid value to launch the loop
    while valid_rating >= 100:
        valid_rating = inputy("Enter a rating", type="int", condition="+")
    ratings.append(valid_rating)

link to printy: https://github.com/edraobdu/printy

Answered By: Edgardo Obregón

Use continue and break situation wise to make a workable right program.

Answered By: Anuj Kumar
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.