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


Messages In This Thread
How to reject "invalid" input in Python3.7 - by Bruizeh - Feb-08-2019, 11:06 AM

Forum Jump:

User Panel Messages

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