Search code examples
pythonsyntaxsyntax-error

Why does my python code run perfectly fine on runestone but not on vscode?


def CreditPay(rate, payment, verbose):
    balance = 1000 # Amount currently owed
    month = 1  #Number of months
    paid = 0    # Amount paid so far

    while balance > payment:
        balance = balance + balance*rate/100 - payment
        paid += payment
        if verbose:
            print(f'Balance after month {month} is $ {balance}.')

        month += 1
    print(f"Final payment is $ {balance}")
    print(f'Final amount paid is $ {balance+paid}')
        
    return month

nmonths = CreditPay(2.5,100,False)
print("Number of months to pay off is", nmonths)

nmonths = CreditPay(2.5,100,True)
print("Number of months to pay off is", nmonths)

                                                   ^

SyntaxError: invalid syntax martin@Martins-Air ~ % python -u "/Users/martin/Downloads/assignment7" File "/Users/martin/Downloads/assignment7", line 19 print(f'Balance after month {month} is $ {balance}.') ^


Solution

  • What python interpreter are you using? f-string got introduced in python3. To select your python interpreter with Vscode open up the command line windows ctrl+shift+p or macos cmd+shift+p and then search for "python interpreter". the complete command should look like this

    >Python: Select Interpreter and then simply click on any Python 3.x.x
    

    Hope this helps!