-1

So I just started to learn how to code. I want to write a piece of code that detects which of the try inputs was entered incorrectly, and return them to that line of code.

from fractions import Fraction
import math
def Calc() :
        try :
            Length1 = abs(float(Fraction(input("First Rectangle Length:\n")))) 
            Width1 = abs(float(Fraction(input("First Rectangle Width:\n"))))
            Length2 = abs(float(Fraction(input("Second Rectangle Length:\n"))))
            Width2 = abs(float(Fraction(input("Second Rectangle Width:\n"))))
        except :
            print ("please enter numeric values")
            
        
    
    RECTANGLE2 = Length2 * Width2
    RECTANGLE1 = Length1 * Width1

    if RECTANGLE1 > RECTANGLE2 :
        print (f"The First Rectangle Is Bigger! It's Area Is:\n{RECTANGLE1}")
    elif RECTANGLE1 == RECTANGLE2 :
        print (f"The Rectangles Are The Same! Their Area Is:\n{RECTANGLE1}")
    else :
        print (f"The Second Rectangle Is Bigger! It's Area Is:\n{RECTANGLE2}")

The code is designed to ask the user for the area of 2 rectangles and determine which one is bigger. If someone were to input a str on Length2, I want to return them to ONLY length2. I don't want my code to re-ask them Length1 and Width1.

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • Please update your question with the code you have tried. – quamrana Sep 02 '21 at 16:41
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Matthias Sep 02 '21 at 16:43

3 Answers3

1

You can use a function and loop for something like this. Here, it will only return a value that is valid

def get_valid_input(input_msg):
    while True:
        try:
            ret_val = abs(float(Fraction(input(input_msg)))) 
            return ret_val
        except:
            print("Invalid Number!")
length1 = get_valid_input("First Rectangle Lenght:\n")
#Do other work here
BTables
  • 4,413
  • 2
  • 11
  • 30
  • That worked perfectly. I do have a question. (input_msg) I don't understand why the code doesn't work without it. Could you explain why adding it is necessary? – Tyler Papa Sep 02 '21 at 17:03
  • `input_msg` is a parameter passed to the function `get_valid_input`. In turn, it is passed onto the `input` statement. This lets you reuse the function with different input prompts. – BTables Sep 02 '21 at 17:07
1

Just do it in a loop, and introduce some initial values to make it possible to know you've already changed this value:

from fractions import Fraction
import math
def Calc():
    length_1 = width_1 = length_2 = width_2 = None
    while True:
        try:
            if length_1 is None:
                length_1 = abs(float(Fraction(input("First Rectangle Length:\n")))) 
            if width_1 is None:
                width_1 = abs(float(Fraction(input("First Rectangle Width:\n"))))
            if length_2 is None:
                length_2 = abs(float(Fraction(input("Second Rectangle Length:\n"))))
            if width_2 is None:
                width_2 = abs(float(Fraction(input("Second Rectangle Width:\n"))))
            break
        except Exception:
            print ("please enter numeric values")
    
    RECTANGLE2 = length_2 * width_2
    RECTANGLE1 = length_1 * width_1

    if RECTANGLE1 > RECTANGLE2 :
        print (f"The First Rectangle Is Bigger! It's Area Is:\n{RECTANGLE1}")
    elif RECTANGLE1 == RECTANGLE2 :
        print (f"The Rectangles Are The Same! Their Area Is:\n{RECTANGLE1}")
    else :
        print (f"The Second Rectangle Is Bigger! It's Area Is:\n{RECTANGLE2}")
madbird
  • 1,326
  • 7
  • 11
0

I suspect you won't get what you're looking for from a single try/except, but there are several ways to accomplish your goal.

Perhaps try defining a function which does the try/except/retry loop for a single number generically then run that function for each of your desired values?

BoredTweak
  • 28
  • 1
  • 6