Ad

How To Gain A Valid Input From User Using Loops?

- 1 answer

from sense_emu import SenseHat
sense = SenseHat()

text_red_prompt = input('How red do you want the text to be: _____ [Key Value between 0 - 255]'
text_red = int(text_red_prompt)

text_green_prompt = input('How green do you want the text to be: _____ [Key Value between 0 - 255]'
text_green = int(text_green_prompt)

How do I code using loops such that when a User were to input a number that is more than 255, the system will prompt the user to enter again before the system moves on to the text_green_prompt?

Ad

Answer

you could try a loop like below for each of you vars

validInput = False
while not validInput:
    text_red_prompt = input('How red do you want the text to be: _____ [Key Value between 0 - 255]')
    text_red = int(text_red_prompt)
    if text_red >= 0 and text_red <= 255:
        validInput = True
Ad
source: stackoverflow.com
Ad