Python Forum
How to reject "invalid" input in Python3.7
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to reject "invalid" input in Python3.7
#1
Hello guys, so I'm taking my first course in Programming and our teacher assigned us to create a BMI calculator in Python, but I would like to know how to implement validation in my user inputs, that my code rejects input that isn't "allowed/invalid" e.g ("Yes Or no") ("Enter your age") and thus the code exits out.
while True:
    try:
        height = float(input("Input your height in meters: "))
        raise ValueError()
        print("Incorrect, Try again")
     else:
         break
         weight = float(input("Input your weight in kilogram: "))
         userBmi = round(weight / (height * height), 2)
right now, this section of code won't even run and yields a "SyntaxError". I would appreciate any help given and a exemplar of how you implemented it, thanks!
Reply
#2
I see an indentation error there. You have to be careful with the indentations.

Try this solution

while True:
    try:
        height = float(input("Input your height in meters: "))
        weight = float(input("Input your weight in kilogram: "))

        userBmi = round(weight / (height * height), 2)

        print("User BMI: {}".format(userBmi))
        break
    except ValueError:
        print("Incorrect, Try again")
Reply
#3
(Feb-08-2019, 12:02 PM)FranSPG Wrote: I see an indentation error there. You have to be careful with the indentations.

Try this solution

while True:
    try:
        height = float(input("Input your height in meters: "))
        weight = float(input("Input your weight in kilogram: "))

        userBmi = round(weight / (height * height), 2)

        print("User BMI: {}".format(userBmi))
        break
    except ValueError:
        print("Incorrect, Try again")
Hello, your solution worked, thanks!
However, (sorry) I have stumbled across another issue. I tried to replicate what you did with my other input questions but when I tested it with invalid input it simply continued, here is the section of the code that I have issues with.

userPreg=input(" Have you recently been involved in sexual activity and thus are now pregnant? Yes or No")
if userPreg == "No":
    pass
elif userPreg == "Yes":
    print("You're too active you testorene filled homo sapien")
    raise SystemExit
How would I implement something similar in this scenario? Sorry for bothering you
Reply
#4
What do you mean with invalid input ? You can validate the type of the input, like we do in the first script, we validated that the type of input must be a float, or you can validate that the input is the string yes or no.

If you want to validate the type of the input, like we've done in the first script, you need to put your input in a try and except estatement, and force the input to be a string.

Think about how to solve it, and if you can't I'll help you with the code.
Reply
#5
(Feb-08-2019, 12:52 PM)FranSPG Wrote: What do you mean with invalid input ? You can validate the type of the input, like we do in the first script, we validated that the type of input must be a float, or you can validate that the input is the string yes or no.

If you want to validate the type of the input, like we've done in the first script, you need to put your input in a try and except estatement, and force the input to be a string.

Think about how to solve it, and if you can't I'll help you with the code.

Thank you Fran I realized I made another indent error when I was implementing the "except" line, thanks for not spoon feeding me and actually making me think of what went wrong. Really appreciated :)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020