Python Forum
Function: SyntaxError: invalid syntax
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function: SyntaxError: invalid syntax
#1
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.
Reply
#2
Hint: brackets always go in pairs ;)
Reply
#3
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))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print(data) is suddenly invalid syntax db042190 6 1,183 Jun-14-2023, 02:55 PM
Last Post: deanhystad
  [Solved] unkown (to me) function def parm "name1:name2" syntax. MvGulik 5 1,058 Nov-11-2022, 11:21 AM
Last Post: MvGulik
  meaning of -> syntax in function definition DrakeSoft 5 1,947 Apr-09-2022, 07:45 AM
Last Post: DrakeSoft
  SyntaxError: invalid syntax ?? korenron 15 5,711 Jan-25-2022, 11:46 AM
Last Post: korenron
  Invalid syntax with an f-string Mark17 7 7,778 Jan-14-2022, 04:44 PM
Last Post: Mark17
  invalid syntax in my class CompleteNewb 2 1,895 Dec-13-2021, 09:39 AM
Last Post: Larz60+
Star I'm getting syntax error while using input function in def. yecktmpmbyrv 1 1,960 Oct-06-2021, 09:39 AM
Last Post: menator01
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 3,164 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  Tuple generator, and function/class syntax quazirfan 3 3,865 Aug-10-2021, 09:32 AM
Last Post: buran
  Unexplained Invalid syntax Error cybertooth 5 3,245 Aug-02-2021, 10:05 AM
Last Post: cybertooth

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020