Python Forum

Full Version: Function: SyntaxError: invalid syntax
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

Please see the following (I want to ascertain the monthly rent amount of an apartment by entering the weekly amount & play around with functions):


def monthly():

x = float(input("Please enter the weekly rent amount: ")
print("The monthly rent amount is " +x * 52 / 12)

monthly()


I get the following error message:

File "/home/pc/PycharmProjects/begin/function.py", line 4
print("The monthly rent amount is " +x * 52 / 12)
^
SyntaxError: invalid syntax

Process finished with exit code 1

Any help is much appreciated.
Hint: brackets always go in pairs ;)
I was kind of confused at the beginning because your program has a huge problem which would result in a TypeError not a SyntaxError, took me some moments to see, that you are missing a ) in line 3.
The next problem you will run into is that the + operator is not allowed between strings and numbers, so "test: " + 1 will result in a TypeError, there are different approaches to get the number in the string. The best one would be format.
print(f"The monthly rent amount is {x * 52 / 12}")
here the part between the curly brackets will be filled with your calculation and you do not have to worry about the data type you are putting in there. The easiest way, at least at the beginning would be to cast the value you are recieving to string
print("The monthly rent amounnt is " + str(x * 52 / 12))