Problem using Return (Python 3)

Asked

Viewed 39 times

-4

I’m starting now and I tried to do it according to my teacher’s explanation, but I’m not getting the job done. I want you to go back to the beginning if the user answers wrong instead of just ending.

def paintcalculator():
    print("What measurement unit do you use?")
    unit = int(input("What measurement unit do you use?\n1 - Meters \n2 - Centimeters \n3 - Inches \nOption number:"))
    
    if unit == 1:  #meters
        height = float(input('What is the  height of the wall? '))
        width = float(input('What is the width of the wall? '))
        layers = float(input('How many layers do you want to use? '))
        area = height * width
        liters = 2 * area * layers
        print('You will need aproximately {:.2f} liters of paint.'.format(liters)) 

    elif unit == 2:  #centimeters
        height = float(input('What is the  height of the wall? '))
        width = float(input('What is the width of the wall? '))
        layers = float(input('How many layers do you want to use? '))
        area = (height*0.01) * (width*0.01)
        liters = 2 * area * layers
        print('You will need aproximately {:.2f} liters of paint.'.format(liters)) 

    elif unit == 3:  #inches
        height = float(input('What is the  height of the wall? '))
        width = float(input('What is the width of the wall? '))
        layers = float(input('How many layers do you want to use? '))
        area = (height*0.0254) * (width*0.0254)
        liters = 2 * area * layers
        print('You will need aproximately {:.2f} liters of paint.'.format(liters)) 

    else:
        print('Please, answer correctly.')
        return paintcalculator()
  • Contrary to what the answer below suggested, call the function paintcalculator within itself is not the best option as this is an improper use of recursion and this can cause problems (in addition to being an unnecessary complication). It is best to use a loop (like the while for example). As it was not clear to me when to leave and when to continue, follow a suggestion/guess: https://ideone.com/bBFNp0

2 answers

1

Good afternoon

Good for what I do not think so cool to call the function inside itself, in case your application create an infinite repetition structure like whle True: would already be working, so just create an option to close the menu

follows the amendment made:

def paintcalculator():

while True:
    print("What measurement unit do you use?")
    unit = int(input("What measurement unit do you use?\n1 - Meters \n2 - Centimeters \n3 - Inches \nIf you want break this program press number [9] \n\nOption number: "))

    if unit == 1:  # meters
        height = float(input('What is the  height of the wall? '))
        width = float(input('What is the width of the wall? '))
        layers = float(input('How many layers do you want to use? '))
        area = height * width
        liters = 2 * area * layers
        print('You will need aproximately {:.2f} liters of paint.'.format(liters))

    elif unit == 2:  # centimeters
        height = float(input('What is the  height of the wall? '))
        width = float(input('What is the width of the wall? '))
        layers = float(input('How many layers do you want to use? '))
        area = (height * 0.01) * (width * 0.01)
        liters = 2 * area * layers
        print('You will need aproximately {:.2f} liters of paint.'.format(liters))

    elif unit == 3:  # inches
        height = float(input('What is the  height of the wall? '))
        width = float(input('What is the width of the wall? '))
        layers = float(input('How many layers do you want to use? '))
        area = (height * 0.0254) * (width * 0.0254)
        liters = 2 * area * layers
        print('You will need aproximately {:.2f} liters of paint.'.format(liters))

    elif unit == 9:
        break

    else:
        print('Please, answer correctly.')

paintcalculator()

so you just call your function when you want to use it.

hope I’ve helped!

0

First, I would like to inform you that you should explain in what exact scenario the error you refer occurs. Having said that, I realized that your problem occurs after the user chooses an option in the menu and does not respect the type of value entered.

Things to do to solve the problem and improve the code:

  1. Each menu option should match a function even if the code is simple.
  2. Use the mechanism of Try and except existing in python.
  3. Although the solution presented does not do so, a way should be thought out to solve the existing repetition in height, width and layers. Perhaps an auxiliary object that stores the information in its attributes.

Here is an example of a solution


def optMeters():
    height = float(input('What is the  height of the wall? '))
    width = float(input('What is the width of the wall? '))
    layers = float(input('How many layers do you want to use? '))
    area = height * width
    liters = 2 * area * layers
    print('You will need aproximately {:.2f} liters of paint.'.format(liters))


def optCentimeters():
    height = float(input('What is the  height of the wall? '))
    width = float(input('What is the width of the wall? '))
    layers = float(input('How many layers do you want to use? '))
    area = (height*0.01) * (width*0.01)
    liters = 2 * area * layers
    print(
        'You will need aproximately {:.2f} liters of paint.'.format(liters))


def optInches():
    height = float(input('What is the  height of the wall? '))
    width = float(input('What is the width of the wall? '))
    layers = float(input('How many layers do you want to use? '))
    area = (height*0.0254) * (width*0.0254)
    liters = 2 * area * layers
    print(
        'You will need aproximately {:.2f} liters of paint.'.format(liters))


def paintcalculator():
    try:
        unit = int(input(
            "What measurement unit do you use?\n1 - Meters \n2 - Centimeters \n3 - Inches \n4 - Exit \nOption number:"))
        if unit == 1:  # meters
            optMeters()

        elif unit == 2:  # centimeters
            optCentimeters()

        elif unit == 3:  # inches
            optInches()
        elif unit == 4:  # exit opt
            return
        else:
            print('Please, answer correctly.')
            paintcalculator()  # No return needed
    except:
        print('Invalid input detected')
        paintcalculator()



  • I tried to run your code, only what appears on the terminal is just where the file is on my pc, what do I have to do? I really liked yours, told me to use the function while True, I used and gave cerrto, but I much prefer the idea of turning each option into a function, or class, I do not know

  • @Lfharada Vc can very well combine the 2 things: separate into functions and use the while instead of calling the function within itself...

  • @Lfharada while is also a great option to control the flow of the code and just use a break to exit the loop, on the problem that describes should need to call the function in the code (thought that what presented was not the complete code). Try adding paintcalculator() at the end of the code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.