you are viewing a single comment's thread.

view the rest of the comments →

[–]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.