Python Forum

Full Version: Number Series
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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
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.
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)
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):'
In snippet you provided this is row #1. Error message states that this is actually row #5. What is on previous four rows?
(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 ....
Do you use shebang in you .py file?

Your problem description seems to be similar to the one in StackOverflow
(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
I have no problem with your code, it works.

Which editor do you use to save Qn8.py?
(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.
Pages: 1 2