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
#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


Messages In This Thread
RE: How to reject "invalid" input in Python3.7 - by Bruizeh - Feb-08-2019, 12:14 PM

Forum Jump:

User Panel Messages

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