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 3,714 Jun-14-2023, 02:55 PM
Last Post: deanhystad
  [Solved] unkown (to me) function def parm "name1:name2" syntax. MvGulik 5 2,348 Nov-11-2022, 11:21 AM
Last Post: MvGulik
  meaning of -> syntax in function definition DrakeSoft 5 3,755 Apr-09-2022, 07:45 AM
Last Post: DrakeSoft
  SyntaxError: invalid syntax ?? korenron 15 10,329 Jan-25-2022, 11:46 AM
Last Post: korenron
  Invalid syntax with an f-string Mark17 7 17,462 Jan-14-2022, 04:44 PM
Last Post: Mark17
  invalid syntax in my class CompleteNewb 2 3,528 Dec-13-2021, 09:39 AM
Last Post: Larz60+
Star I'm getting syntax error while using input function in def. yecktmpmbyrv 1 2,901 Oct-06-2021, 09:39 AM
Last Post: menator01
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 4,484 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  Tuple generator, and function/class syntax quazirfan 3 7,049 Aug-10-2021, 09:32 AM
Last Post: buran
  Unexplained Invalid syntax Error cybertooth 5 5,858 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