'None' keeps appearing in the output - How to remove it?

This is an assignment, when I run the code it runs properly but keeps putting ‘None’ at the end.
Can anyone help me?

def option_A():
    fBondValue = float(input("\nEnter face value of bond: "))
    fIntr = float(input("Enter coupon interest rate: "))
    fCurrentMarketPrice = float(input("Enter current market price: "))
    iMaturity = int(input("Enter years until maturity: "))
    Intr = fIntr * 1000
    a = (fBondValue - fCurrentMarketPrice) / iMaturity
    b = (fBondValue + fCurrentMarketPrice) / 2
    YTM = (Intr + (a)) / (b)
    percentYTM = YTM * 100
    txt = "Approximate YTM: {:.2f}%"
    print(txt.format(percentYTM))

Most likely, it’s the way in which you are calling the function.

If you simply call it with option_A(), from the main script, then it should be fine. If you want it to return the text so that you can do something else with said, then have the last line as return txt.format(percentYTM) and then print(option_A()) (if you want to display the text) from the main script.

I followed your suggestion and replaced

print(txt.format(percentYTM))

with

return (txt.format(percentYTM))
print(option_A)

and even tried it with just:

return (txt.format(percentYTM))

but it no longer gives me the answer and still returns a ‘none’

option_A doesn’t have an explicit return statement, so it will return None to the caller.

How are you calling it? If you’re doing print(option_A()), then it will print None.

If you look closely, you’ll see that I put print(option_A())

Oh sorry, my mistake.
I tried with print(option_()) but it’s still giving me ‘none’ and is no longer giving me the answer that was originally in the print statement.

Now you’ve missed the A :slight_smile:

I’m calling option_A from another function (the ‘menu’ of the code)
In terms of option_A itself, then I’m not calling it, I just want option_A to do the calculation and print the answer.

:see_no_evil: omg.
Ok I tried with the A
It still doesn’t work.

To be clear, this is what you need, if you’re using the return statement:

def option_A():
    fBondValue = float(input("\nEnter face value of bond: "))
    fIntr = float(input("Enter coupon interest rate: "))
    fCurrentMarketPrice = float(input("Enter current market price: "))
    iMaturity = int(input("Enter years until maturity: "))
    Intr = fIntr * 1000
    a = (fBondValue - fCurrentMarketPrice) / iMaturity
    b = (fBondValue + fCurrentMarketPrice) / 2
    YTM = (Intr + (a)) / (b)
    percentYTM = YTM * 100
    txt = "Approximate YTM: {:.2f}%"
    return txt.format(percentYTM)


print(option_A())

Yes, I used the return statement like that.

But the thing is, I can’t put print(option_A()) outside of the def option_A(): because then the code just begins with option_A() straight away instead of going through the menu.

Okay. Well in that case:

def option_A():
    fBondValue = float(input("\nEnter face value of bond: "))
    fIntr = float(input("Enter coupon interest rate: "))
    fCurrentMarketPrice = float(input("Enter current market price: "))
    iMaturity = int(input("Enter years until maturity: "))
    Intr = fIntr * 1000
    a = (fBondValue - fCurrentMarketPrice) / iMaturity
    b = (fBondValue + fCurrentMarketPrice) / 2
    YTM = (Intr + (a)) / (b)
    percentYTM = YTM * 100
    txt = "Approximate YTM: {:.2f}%"
    print(txt.format(percentYTM))

… then call your function with option_A()

Oh yes, that worked!
But then option_A() starts again. Is there a way to stop this?

You’ve nailed the function, so it seems to me that the ‘driver’ (the code that drives your app) needs to be looked into; your menu code and the likes of.

The code that is printing None is not the code you have shown us. You need to look at the “menu” code and see what it is doing, and why it is printing None.

So would you like me to put up the whole code?

Ok, thank you so much for your suggestions!

1 Like

Maybe all of the code would be overkill; we have one function and it seems to me that you know how to code, so just the driver code should do, if you’re still stuck.

How many lines of code would that be?

It’s not a lot just 40 lines.
Edit: but there are comments so I would say about 35 ish

Go for it :slight_smile:

1 Like