Seriously stuck on question 3

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

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

Im in python3 no worries

1 Like

‘{}%’.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.

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

it didnt work idk why

its not working hmmmm

can you copy and paste what you put? (make sure to format it with the </> button).

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)>

Round the result to 1 decimal place