Python help with attempt | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Python help with attempt

I need help in solving the following in python: Write a python script that inputs 3 decimal numbers, a, b and c from the user. Calculate the result of the following equation and display the result. The output should be formatted the same as the sample run below. r= ((ab) + (bc) + (ca)) / (a+b+c)) Sample run: Enter a: 2.6 Enter b: 4.5 Enter c: 1.8 The result of the calculation is 2.75 My attempt was: a=str(input(‘Enter a: ‘)) b=str(input (‘Enter b: ‘)) c= str(input (‘Enter c: ‘)) print (“The result of the calculation is “ + ((a*b) + (b*c) + (c*a)) / (a+b+c)) (i keep on getting errors for the last print part and I dont know how to ask the program to take only 2 numbers after the decimal point for the result of the calculation).

29th Sep 2020, 11:44 AM
Sara Maskoun
Sara Maskoun - avatar
3 Answers
+ 3
A little formatting. a=float(input('Enter a: ')) b=float(input ('Enter b: ')) c=float(input ('Enter c: ')) r=((a*b) + (b*c) + (c*a)) / (a+b+c) print ("The result of the calculation is "+"{0:.2f}".format(r))
29th Sep 2020, 12:02 PM
Avinesh
Avinesh - avatar
+ 4
probably because you are multiplying a string by a string. the firat 3 lines in your attempt gets all the numbers, but turns them into strings. try: a = float(input("Enter a: ") ... Then you just need to round the answer. you could set that result equal to a variable, then by using round() or with some fancy formatting, you can drop off all the extra stuff after the decimal.
29th Sep 2020, 11:49 AM
Slick
Slick - avatar
+ 3
Convert input() to float with float(input("Enter something:")) You are converting input() to str.
29th Sep 2020, 11:48 AM
你知道規則,我也是
你知道規則,我也是 - avatar