Area Calculator _ Error line 11


#1

Hi everyone,

The last hour I’ve been unable to find the mistake the terminal is pointing out, while trying to test out the validity of the first half of the Area Calculator exercise.

I could totally use a second pair of eyes. Please advice accordingly, thanks in advance…

“”"
Area Calculator
“”"

print ‘Calculation in action!’

option = raw_input("Enter C or Circle for Circle or T or Triangle for Triangle: ")

if option == ‘C’ or option == ‘Circle’:
radius = float(raw_input('Enter radius: '))
area = 3.14 * radius ** 2
print ‘Area is %s’ % area


#2

Were we instructed to use 3.14 or pi?

from math import pi

#3

Hi mtf, thanks for replying.

Apparently they have changed the instructions into a more streamlined exercise.


#4

Please post a link to the exercise. Thanks.


#5

You got it!

https://www.codecademy.com/courses/learn-python/projects/area-calculator


#6

This is a free form project so no lesson checker

I would reduce the inputs to just C or T and accept both lower and upper case.

if option.lower in 'ct':

For greater precision use the PI constant,

from math import pi

If you use a float to multiply, the input does not need to be cast to float. Let the user input anything they wish.

print ‘Area is %s’ % area

is old school. Suggest use .format or an f-string (Python 3.6 or higher).

https://realpython.com/python-f-strings/


#7

@mtf thanks a lot for your time & the help.

I followed your suggestions to: from math import pi, reduced C,T inputs in lower.(), erased the float cast and used f-string method instead of %s,

but unfortunately it’s still reflecting the same error in line_11: area = pi * radius ** 2


#8

Please post your revised code and we can test it. I don’t think the LE is 3.6 or higher. May have to revert to str.format().


#9

“”"
Area Calculator
“”"
from math import pi
print ‘Calculation in action!’

option = raw_input("Enter C or T for Circle or Triangle respectively: ")

if (option.lower()) == ‘C’:
radius = raw_input('Enter radius: ')
area = pi * radius ** 2
print f"Area is {area}"

#I also tried “Area is %f” %area


#10
 if option.lower() == 'c':

Aside

If you want it to be obvious that pi is a constant,

>>> from math import pi as PI
>>> PI
3.141592653589793
>>> 

#11

Thanks, I corrected it, but still… line_11 is doomed!


#12

I don’t think you can use f-strings in the LE. Use str.format(), instead.

Redo all your indentaion using spaces, 2 or 4, but 4 are more obvious.

$ python AreaCalculator.py
2.7.12 (default, Nov 12 2018, 14:36:49)
[GCC 5.4.0 20160609]
Calculator is starting on 18/12/2018 at 23:57
Enter C for Circle or T for Triangle:c
Enter the radius:1
The pie is baking...
The area is 3.14.
Don't forget to include the correct units!
Exiting...
$

#13

Ok, after I redid the identations it worked, we’ve made some progress:

Tried these 2 scenarios and in both I get an error regarding the exponentiation **


#14

My bad… The input is a string that needs to be cast to a float. Fifty lashes with a wet noodle.


#15

Haha no worries, let me correct it real quick.

Update: Wooooohooooo!!! It worked!


#16

One last point… If we want to permit the user to type,

CIRCLE

we can by zeroing in on the first character.

if option[0].lower() == 'c':

#17

Good point!

Thanks again for the help, very much appreciated.

It’s my 1st week learning Python and it’s been exciting with all the support. Looking forward to reach a more advanced level and start asking the “real” questions :smiley:


#18

All questions are real (as pertains to lessons and code in general) from where I stand. The really tough ones are more the challenge to answer. Keep up the groove and you’ll meet some experts in the tougher tracks.