Stuck on Continue Statement: Solving a Python Coding Problem

In summary, the conversation is about someone who is using the book "A Byte of Python" and is stuck on the Continue Statement. They provide their code and the error they are getting. They also mention that they have spent a lot of time trying to figure it out and looking for help online. Finally, they mention that they were following an example from a website but still couldn't figure it out. Another person then points out that the issue might be with the input function they are using. The original person thanks them and admits that it was a simple fix.
  • #1
mockingbird
3
0
I Have been using A Byte of Python, and I am now stuck on the Continue Statement. Here is my code.

#!/usr/bin/python
#Filename: continue1.py

while True:
s = input('Enter something : ')
if s == 'quit':
break
if len(s) < 3:
print('Too Small')
continue
print('Input is of sufficient Length')
#Do other kinds of Processing here...

I keep getting this...

andrew@andrew-laptop:~$ python continue1.py
Enter something : a
Traceback (most recent call last):
File "continue1.py", line 5, in <module>
s = input('Enter something : ')
File "<string>", line 1, in <module>
NameError: name 'a' is not defined

I am completely new at this, but I have 1. Spent more than two hours trying to figure this out. 2. Looked online for longer than an hour to find an example of what I have done wrong. 3. Stared at this problem until my eyes have bled.
I hope someone can/will help me figure this out. I am using gedit that came with my version of Ubuntu. I don't know if that will change anything.
 
Technology news on Phys.org
  • #2


Well my code didn't come out properly, here is the link that I am imitating. The Break Statement and the Continue Statement are the ones that aren't working.

http://www.swaroopch.com/notes/Python_en:Control_Flow [Broken]

I am pretty sure that I am putting everything in the way he is telling me.
 
Last edited by a moderator:
  • #4


Hey! Thanks man, I glad you pointed that out to me. Like I said, was probably a really easy, simple, stupid fix. I feel embarassed now, but like I said I was copying his code, and couldn't figure anything out. Really Appreciate you helping a Noob out.
 

1. What is the purpose of a continue statement in Python?

A continue statement is used in Python to skip over certain steps in a loop or iteration and continue to the next iteration. It allows for more control over the flow of a program and can help to avoid certain conditions or values.

2. How do I use a continue statement in my Python code?

To use a continue statement, simply include the keyword "continue" in your code within a loop or iteration where you want to skip a certain step. This will cause the loop to immediately jump to the next iteration without executing any further statements in the current iteration.

3. Can a continue statement be used in any type of loop in Python?

Yes, a continue statement can be used in any type of loop, including for loops, while loops, and nested loops. It can also be used in list comprehensions and generator expressions.

4. How can a continue statement help me solve a coding problem?

A continue statement can be useful in solving a coding problem by allowing for more precise control over the flow of a program. It can help to skip over certain steps or values that may cause errors or unwanted results, and can also be used to optimize code by avoiding unnecessary computations.

5. Are there any alternative ways to achieve the same result as a continue statement?

Yes, there are alternative ways to achieve similar results as a continue statement, such as using conditional statements or breaking out of a loop. However, a continue statement provides a more concise and efficient way to skip over certain steps in a loop or iteration.

Similar threads

  • Programming and Computer Science
Replies
2
Views
786
  • Programming and Computer Science
Replies
8
Views
847
Replies
5
Views
829
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
14
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
12
Views
992
  • Programming and Computer Science
Replies
18
Views
2K
Back
Top