I am new to Python and have been teaching myself over the past few months. The book I am using teaches Python 2.7, while I am trying to learn Python in 3.4. I've become accustomed to using both now, but for the life of me I can't figure out how to exit this while loop with the enter key. The code appears below: total = 0
count = 0
data = eval(input("Enter a number or press enter to quit: "))
while data != "":
count += 1
number = data
total += number
average = total / count
data = eval(input("Enter a number or press enter to quit: "))
print("The sum is", total, ". ", "The average is", average)
I keep getting this error: Traceback (most recent call last):
File "/Users/Tay/Documents/Count & Average.py", line 10, in <module>
data = eval(input("Enter a number or press enter to quit: "))
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
I am able to get a modified version of this code to work in 2.7, but I would like to know how to do this in
3.4. I've searched around everywhere and can't seem to find an answer. This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3
|
| # http://stackoverflow.com/questions/7255463/exit-while-loop-by-user-hitting-enter-key
|
|
|
| while True:
|
| i = input("Enter text (or Enter to quit): ")
|
| if not i:
|
| print("excape") # Enter key to quit
|
| break
|
| print("Your input:", i)
| Summary: in this tutorial, you’ll learn about the Python break statement and how to use it to exit a loop prematurely. Introduction to the Python break statementSometimes, you want to terminate a for loop or a while loop
prematurely regardless of the results of the conditional tests. In these cases, you can use the break statement: break Code language: Python (python)
Typically, you use the break statement with the if statement to terminate a loop when a condition is True . Using Python break with for loopThe following shows how to use the break statement inside a for loop: for index in range(n):
# more code here
if condition:
break Code language: Python (python)
In this syntax, if the
condition evaluates to True , the break statement terminates the loop immediately. It won’t execute the remaining iterations. This example shows how to use the break statement inside a for loop: for index in range(0, 10):
print(index)
if index == 3:
break Code language: Python (python)
Output: 0
1
2
3 Code language: Python (python)
How it works. - The
for loop iterates over 10 numbers from 0 to 9 and displays each of them on the screen. - However, when the loop counter is 3, the
break statement terminates the loop immediately. Therefore, the
program shows only 4 numbers, from 0 to 3 on the screen.
When you use the break statement in a nested loop, it’ll terminate the innermost loop. For example: for x in range(5):
for y in range(5):
# terminate the innermost loop
if y > 1:
break
# show coordinates on the screen
print(f"({x},{y})") Code language: Python (python)
Output: (0,0)
(0,1)
(1,0)
(1,1)
(2,0)
(2,1)
(3,0)
(3,1)
(4,0)
(4,1) Code language: Python (python)
This example uses two for loops to show the coordinates from (0,0) to (5,5) on the screen. The break statement in the nested loop terminates the innermost loop when the y is greater than one. Therefore, you only see the coordinates whose y values are zero and
one. Using Python break statement with a while loopThe following shows how to use the break statement inside the while loop: while condition:
# more code
if condition:
break Code language: Python (python)
The following example uses the break statement inside a while loop. It’ll prompt you for entering your favorite color. The program will stop once you enter quit : print('-- Help: type quit to exit --')
while True:
color = input('Enter your favorite color:')
if color.lower() == 'quit':
break Code language: Python (python)
Output: -- Help: type quit to exit --
Enter your favorite color:red
Enter your favorite color:green
Enter your favorite color:blue
Enter your favorite color:quit Code language: Python (python)
How it works. - The
while True creates an indefinite loop. - Once you enter
quit , the condition color.lower() == 'quit'
evaluates True that executes the break statement to terminate the loop. - The
color.lower() returns the color in lowercase so that you can enter Quit , QUIT or quit to exit the program.
Summary- Use the Python
break statement to terminate a for loop or a while loop prematurely.
Did you find this tutorial helpful ?
How do you press Enter to exit in Python?
The input() function merely waits for you to enter a line of text (optional) till you press Enter. The sys. exit("some error message") is the correct way to terminate a program. This could be after the line with the input() function.
How do you exit a loop in Python?
Python provides two keywords that terminate a loop iteration prematurely: The Python break statement immediately terminates a loop entirely. Program execution proceeds to the first statement following the loop body. The Python continue statement immediately terminates the current loop iteration.
Can you break a for loop Python?
Practical Data Science using Python
The break statement can be used in both while and for loops. If you are using nested loops, the break statement stops the execution of the innermost loop and start executing the next line of code after the block.
How do you exit a loop if condition is met?
break terminates the execution of a for or while loop. Statements in the loop after the break statement do not execute. In nested loops, break exits only from the loop in which it occurs. Control passes to the statement that follows the end of that loop.
|