Bottom Page

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!
Quote
#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")

Quote
#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
Quote
#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.
Quote
#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 :)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [split] Please help with SyntaxError: invalid syntax Mason 1 163 Apr-28-2019, 06:58 PM
Last Post: Yoriz
  SyntaxError: Invalid syntax in a while loop ludegrae 3 1,054 Dec-18-2018, 04:12 PM
Last Post: Larz60+
  SyntaxError: invalid syntax at run .py tuxo9999 10 1,186 Aug-23-2018, 03:58 PM
Last Post: Axel_Erfurt
  Homework: Invalid syntax using if statements chehortop 3 906 Mar-01-2018, 04:38 AM
Last Post: micseydel
  invalid syntax error penlemon 4 870 Jan-27-2018, 02:46 PM
Last Post: sparkz_alot
  Invalid syntax: string index out of range darkreaper1959 6 2,393 Jan-22-2017, 06:24 PM
Last Post: nilamo
  Invalid Syntax problem (?!?) harley_quinn 4 3,357 Nov-08-2016, 07:54 PM
Last Post: wavic

Forum Jump:


Users browsing this thread: 1 Guest(s)