Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Number Series
#1
This was the given question :

Write a Python program that prints all the numbers from 0 to 6 except 3 and 6.
Note : Use 'continue' statement.
Expected Output : 0 1 2 4 5

This is my code :

for i in range(6):
    if i == 3:
        continue
    elif i == 6:
        continue
    else 
        print i 
and error output :

Error:
CaiGengYangs-MacBook-Pro-2:Lesson 1 caigengyang$ bash Question8.py Question8.py: line 5: syntax error near unexpected token `(' Question8.py: line 5: `for i in range(6):'
Please advise thanks
Reply
#2
If that's on line five, what's above it? Often times, syntax errors result from missing brackets above the point where the error is noted.
Reply
#3
Some observations:


- range(6) is 0, 1, 2, 3, 4, 5 so no need to check i == 6 (or you should use range(7))

- else misses : (parenthesis)

- is it Python 2? In Python 3 you need to use print(i)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
It still doesnt work and gives the same error : the error is in the first line of the code " for i in range(7): " , but i can't see what's wrong with it !

for i in range(7):
    if i == 3:
        continue
    elif i == 6:
        continue
    else: 
        print(i)
Output error :

Error:
Qn8.py: line 5: syntax error near unexpected token `(' Qn8.py: line 5: `for i in range(7):'
Reply
#5
In snippet you provided this is row #1. Error message states that this is actually row #5. What is on previous four rows?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
(Jan-17-2019, 10:22 AM)perfringo Wrote: In snippet you provided this is row #1. Error message states that this is actually row #5. What is on previous four rows?


Nothing ... there is nothing on the previous 4 rows, really weird ....
Reply
#7
Do you use shebang in you .py file?

Your problem description seems to be similar to the one in StackOverflow
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
(Jan-17-2019, 01:49 PM)perfringo Wrote: Do you use shebang in you .py file?

Your problem description seems to be similar to the one in StackOverflow

No

(Jan-17-2019, 01:55 PM)MrGoat Wrote:
(Jan-17-2019, 01:49 PM)perfringo Wrote: Do you use shebang in you .py file?

Your problem description seems to be similar to the one in StackOverflow

No

Still doesnt work whether I use shebang or not
Reply
#9
I have no problem with your code, it works.

Which editor do you use to save Qn8.py?
Reply
#10
(Jan-17-2019, 01:55 PM)MrGoat Wrote: Still doesnt work whether I use shebang or not

(Jan-17-2019, 05:53 AM)MrGoat Wrote:
Error:
CaiGengYangs-MacBook-Pro-2:Lesson 1 caigengyang$ bash Question8.py Question8.py: line 5: syntax error near unexpected token `(' Question8.py: line 5: `for i in range(6):'

Without python shebang your command bash Question8.py will run it as a bash script.
Try python Question8.py or use proper python2 shebang.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  find the sum of a series of values that equal a number ancorte 1 461 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  Convert quarterly time series to monthly time series donnertrud 1 5,102 May-22-2020, 10:16 AM
Last Post: pyzyx3qwerty

Forum Jump:

User Panel Messages

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