Python Forum
invalid syntax in line 5. Help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
invalid syntax in line 5. Help
#1
if string 1 length is greater than print true else false. but there is a problem can u please help me

invalid syntax in line 5

1- string1=input("Enter a word: ")
2- string2=input("Please enter another word: ")
3- if len(string1) > len(string2):
4- print("True")
5- else:
6- print("false")
Reply
#2
If you were to indent lines 4 and 6, the syntax would be ok.
Paul
Reply
#3
Please wrap code in Python tags (see the Python button in the editor).

Most likely you have an indentation error. This code runs:
string1=input("Enter a word: ")
string2=input("Please enter another word: ")

if len(string1) > len(string2):
    print("True")
else:
    print("false")

print("True" if len(string1) > len(string2) else "false")
You can also use a ternary expression like that in the bottom line.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print(data) is suddenly invalid syntax db042190 6 1,119 Jun-14-2023, 02:55 PM
Last Post: deanhystad
  SyntaxError: invalid syntax ?? korenron 15 5,569 Jan-25-2022, 11:46 AM
Last Post: korenron
  Invalid syntax with an f-string Mark17 7 7,555 Jan-14-2022, 04:44 PM
Last Post: Mark17
  invalid syntax in my class CompleteNewb 2 1,844 Dec-13-2021, 09:39 AM
Last Post: Larz60+
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 3,092 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  Line 42 syntax error..Help!!1 patpython 4 2,606 Sep-01-2021, 03:22 PM
Last Post: deanhystad
  Unexplained Invalid syntax Error cybertooth 5 3,175 Aug-02-2021, 10:05 AM
Last Post: cybertooth
  [split] SyntaxError: invalid syntax Code_X 3 2,703 May-04-2021, 05:15 PM
Last Post: Yoriz
  Syntax error on line 29 manwithjeans 3 2,771 Feb-27-2021, 08:28 AM
Last Post: perfringo
  Invalid syntax error - need help fixing calgk01 3 3,226 Feb-23-2021, 08:41 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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