Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why does it pop an error when I already set a rule in while?

I already set if weight and height isn’t an integer, ask to user to input it again. but it pop: "ValueError: invalid literal for int() with base 10" instead". thanks for helping


while type(weight) != int and type(height) != int and weight not in weightrange and height not in heightrange:

        weight = int(input("Enter a weight number please (kg): "))

        height = int(input("Enter a height number please (cm): "))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’m going to jump in, because I think you’re on the wrong track. You must remember that things execute IN ORDER, from top to bottom. You can’t "establish a rule" and hope that the rule will be applied in the future. Python just doesn’t work that way. Some languages do, Python does not.

while True:
    weight = input("Enter a weight number please (kg): ")
    if weight.isdigit():
        weight = int(weight)
        if weight in weightrange:
            break
        else:
            print( "Weight not in range.")
    else:
        print( "Please enter an integer.")
while True:
    height = input("Enter a height number please (cm): ")
    if height.isdigit():
        height = int(height)
        if height in heightrange:
            break
        else:
            print( "Height not in range.")
    else:
        print( "Please enter an integer.")

If you were doing this for real, you would probably create a function to avoid typing all of that twice.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading