Python Forum
Function: SyntaxError: invalid syntax - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Function: SyntaxError: invalid syntax (/thread-24171.html)



Function: SyntaxError: invalid syntax - vejin - Feb-02-2020

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.


RE: Function: SyntaxError: invalid syntax - michael1789 - Feb-02-2020

Hint: brackets always go in pairs ;)


RE: Function: SyntaxError: invalid syntax - ThiefOfTime - Feb-02-2020

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