https://www.codecademy.com/paths/finance-python/tracks/introduction-to-python-for-finance/modules/calculating-financial-statistics/lessons/calculating-financial-statistics/exercises/introduction
All I have to say is that I honestly don’t know what to do on question 3 " Now, let’s format val
to have our function return a percentage. We want to:
Multiply val
by 100
Round the result to 1 decimal place
Return a formatted string that ends with a '%'
"
They want a formatted string. I don’t know if this is python 2 or 3 though.
in python 2 you might do something like: '{:.1f}%'.format(val*100)
in python 3 you can do something like '{}%'.format(val*100)
(can you also do this in 2? I’m not sure) also I omitted the rounding in this example… left as an exercise… i’m not lazy… (yes I am)
Here’s a nice article (that also includes f-strings… which are just another way to do a very similar thing): https://realpython.com/python-f-strings/
2 Likes
h1lo
October 21, 2020, 12:44am
#3
Also, if you need to round a number, you can use the round()
function.
number=3.44
rounded_num=round(number,1)
print(rounded_num)
>>>3.4
IDK if this is supported in Python 2.
2 Likes
‘{}%’.format(val*100)
This isnt working im not sure why. (I’m in python 3 btw) where do I add this, after the return?
You can
formatted_result = '{}%'.format(val*100)
return formatted_result
or
return '{}%'.format(val*100)
all assuming you have a val
with a value.
mtf
October 23, 2020, 9:22pm
#7
We don’t need to do any rounding if the return value is a formatted string.
return f"{100 * val:.1f}"
The rounding is done for us, and not like round()
, but the true up on 5, down on 4 type of rounding.
1 Like
can you copy and paste what you put? (make sure to format it with the </> button).
mtf
October 23, 2020, 10:03pm
#11
Above I left off the %
. Once that is added it passes for me and displays, 7.5%
.
<rate_of_return = 0.075
def display_as_percentage(val):
return ‘{}%’.format(val*100)>
system
closed
December 4, 2020, 2:14pm
#14
This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.