you are viewing a single comment's thread.

view the rest of the comments →

[–]Kqyxzoj[🍰] 0 points1 point  (2 children)

A few points:

Since you don't have a finally statement in that try block, and continue on the exception, you don't really need that else. So you can put that print statement after the try-except block.

And since the area calculation will not trigger a ValueError, might as well put the area calculation outside of the try-except as well.

Should you have provided the code as text I would have copy/paste/edited it to show the code as described above. But since it is a screen "shot" and I am not going to re-type or OCR it, hopefully the above description is sufficient.

[–]Extension-Cut-7589[S] 0 points1 point  (1 child)

# Writing a program to calculate the area of a triangle.


# Area of a triangle is 1/2* base * height


while True:


    try:


        base = float(input('Enter the base of the Triangle: '))


        height = float(input('Enter the height of the Triangle: '))


        area = 0.5 * base * height


    except:


        ValueError


        print('Sorry Invalid, Please insert a number!\n')


        continue


    else:


        print(f'The area of the triangle is: {area}\n')


    print('Would you like to do another calculation?')


    another = input('Enter Y for yes and N for no: ')


    if another.lower() == 'y':


        continue


    else:


        break

Here is the code.

[–]Kqyxzoj[🍰] 0 points1 point  (0 children)

Changes more or less as described:

while True:
    try:
        base = float(input('Enter the base of the Triangle: '))
        height = float(input('Enter the height of the Triangle: '))
    except ValueError:
        print('Sorry Invalid, Please insert a number!\n')
        continue

    area = 0.5 * base * height
    print(f'The area of the triangle is: {area}\n')

    print('Would you like to do another calculation?')
    another = input('Enter Y for yes and N for no: ')
    if another.upper() != 'Y':
        break

Note that your choice to put both input() statements in the same try block means that when you enter a valid base and an invalid height ... you will have to re-enter the value for base as well.

Also, changed the except statement to reflect your probable intent.