Python: While Loop for Input Validation

preview_player
Показать описание
Quick video showing how you can use a while loop to validate user input in Python, prompting the user to fix their input if it doesn't meet specific criteria.

This can be handled more elegantly with recursive functions, but this is the simplest way to do it.
Рекомендации по теме
Комментарии
Автор

I like how straight forward your explanation is. Thank you for this very helpful video!

Michelle-zfzo
Автор

THANK YOU SO MUCH SIR! I was struggling to find a video like this because other videos were just talking about how to prompt the user only for wrong numbers, not for wrong words.

doctorgawd
Автор

Every tutorial focuses on Integers with While Loops and in truth are pretty easy to write but yours is the first that I've come across that uses Strings. This has helped a ton! Thanks!

kingkid
Автор

Hi all! If you want your code to proceed with other function after this while loop, it can be done also like this:

It can be done also like this:

while True:
if how_to_pay != "equally" and how_to_pay != "unequally":
how_to_pay = input("Please choose between equally and unequally ")
else:
break

Thank you Joshua for the tutorial. Awesome job

taisiyamysak
Автор

First info I found to solve my issue of validating for correct strings and repeating until the valid input was received. Thanks! 1 week programmer here.

CRWSHS
Автор

Thank you so much for this. I am learning python and its my very first day today. I just wanted an introduction and now am trying to write a riddle game that validates a user's input and displays if the answer is right or not. This is exactly what I was looking for. God bless you!

jaski
Автор

I was hoping to find a video regarding this topic and I got EXACTLY what I was looking for. Thanks so much.

theone
Автор

Great video, just what i was looking for. Thank you!

Peterplayingguitar
Автор

Helped me a lot searching for this video thanks !!

fero___
Автор

Thank you. Much appreciated. Very elemental and helpful to Python newbie. I used for coding rock, paper, scissors game.

Hevyosity
Автор

Thank you sir! This video is incredibly helpful.

seanharper
Автор

Dude thanks a lot! This was exactly what I was looking for!

animator
Автор

man im stupid when he typed yo mama i started cracking up i need help😂😂

luminous
Автор

This video was very helpful thank you so much

absoluteatrocity
Автор

Is there a more concise way of writing the while loop condition without having the repetition of 'and'? Awseome video btw

demiannecahiles
Автор

Totally stuck on this assignmnent: any hints? I'm not sure how to get it to properly validate
while True:
lower_range = int(input("Enter your lower range number (-80 - 90) --->"))

if lower_range < -80 or lower_range >90:
print("Re enter low range number")
else: print ("low range number accepted")

higher_range = int(input("Enter your higher range number (-80 - 90) --->"))
if higher_range < -80 or higher_range >90:
print ("Re enter high range number")
else: print ("High range number accepted")

if lower_range > higher_range:
print( "Low range number is more than high range number, ")
print ("please re-enter your high and low range")

number1 = int(input("Enter your first number--->"))
if number1 <= -80 or number1 >=90:
print("Reenter number 1, range limits are (-80 -90)")
else:
print("value accepted", number1)

number2 = int(input("Enter your second number--->"))
if number2 <= -80 or number2 >=90:
print("Reenter number 1, range limits are (-80 -90)")
else:
print("Value accepted")
sum1 = number1 + number2
sum2 = number1 - number2
sum3 = number1*number2
if number2 != 0:
sum4 = number1//number2
else:
print('cannot divide by 0')

# printing the sum
print ("Your lower range number is ", lower_range)
print ("Your upper range number is ", higher_range)
print ("Your first number is ", number1)
print ("Your second number is", number2)
print("The result of ", number1, "+", number2, "= ", sum1)
print("The result of ", number1, "-", number2, "= ", sum2)
print("The result of ", number1, "*", number2, "= ", sum3)
print("The result of ", number1, "/", number2, "= ", sum4)
print("Thanks for using our calculator")

carakintz
Автор

im trying to put this on a nested loop, but i cant seem to figure it out cause i only want specific part of code to repeat

TerukiVAL
Автор

U´RE a FUCKING EXPERT BRO
thank u for this tho...

baadbadubeats
Автор

Can you please explain why did you use 'and' instead of 'or' operator? I am really confused. I am new to Python.

osmanmohammad
Автор

Hi. Great tutorial. But I wonder how I’ll do this exact thing. But from an actual list [ ]. I have an assignment where I have to write a list. And then ask for an input answer. If the word is not on the list, the user has to try again to write a word that’s on the list. So basically; while variable is not on my_list:

evar