Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Syntax error in if line
#1
hello, I am very new to coding in general and python is my first. I am trying to figure out what is wrong with this, but I can't. I always get syntax error on line 2, but to my inexperienced eye it looks fine, please help. (Also, I am really just playing so the code is a bit silly)

lazy = int(raw_input("How lazy are you? (write number 1-100)")
if lazy < 20:
   print("Go RUN RUN RUN")
elif < 45:
   print("we can still go running, let's grab something and go")
elif < 70:
   print("okay, let's just go for a walk")
elif <= 100:
   print("Hm, let's just grab some nice coffee")
else:
   print("hey, that's too much lazy, you were supposed to be max lazy 100, you lazy girl!")
if lazy < 20:
               ^
SyntaxError: invalid syntax
Reply
#2
lazy = int(raw_input("How lazy are you? (write number 1-100)")
The Syntax Error is in line 1. You should close parenthesis:

lazy = int(raw_input("How lazy are you? (write number 1-100): "))
If you start learning Python, you should avoid ancient version from the last century.
Use Python 3.6.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Thank you very much. As a matter of fact, I use Python 3.6.2. When I fixed the line 1 I got another error in line 4

lazy = int(input("How lazy are you? (write number 1-100) "))
if lazy < 20:
   print("Go RUN RUN RUN")
elif < 45:
   print("we can still go running, let's grab something and go")
elif < 70:
   print("okay, let's just go for a walk")
elif <= 100:
   print("Hm, let's just grab some nice coffee")
else:
   print("hey, that's too much lazy, you were supposed to be max lazy 100, you lazy girl!")
Error:
File "D:\Python\Inge.py", line 4    elif < 45:         ^ SyntaxError: invalid syntax
Reply
#4
First you've used raw_input, which doesn't exist since Python 3. So this was a marker that you may be using Python 2.7.
All elif statements are wrong. You're comparing a number to what? A comparison operator needs always two operands.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
Thank you. The thing is that I try to follow various tutorials and not all of them specify version of Python, I will do better.

As I understand using just IF would give me 3 outputs for the value 25, because it's less than 45, 70 and 100. I thought this can be avoided by using elif and you will get just one output for the value 25, which is the desired "we can still go running, let's grab something and go".

This is what I was trying to copy:
[Image: fRVHuk]
Did I understand everything wrong?

The code now works as I wanted it to. No idea how it happened.

lazy = int(input("How lazy are you? (write number 1-100) "))
if lazy < 20:
    print("Go RUN RUN RUN")
elif lazy < 45:
    print("we can still go running, let's grab something and go")
elif lazy < 70:
    print("okay, let's just go for a walk")
elif lazy <= 100:
    print("Hm, let's just grab some nice coffee")
else:
    print("hey, that's too much lazy, you were supposed to be max lazy 100, you lazy girl!")
Reply
#6
Maybe you've used before just standalone if statements. Then every block is checking the condition for it's own.
If you're using if..elif..elif..elif..else there will be only executed one block.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,015 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 335 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,454 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,137 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,395 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  syntax error question - string mgallotti 5 1,251 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,194 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 848 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,778 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Pandas - error when running Pycharm, but works on cmd line zxcv101 1 1,321 Jun-18-2022, 01:09 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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