
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - How to convert this while loop to for loop?
Usin count() function in itertools module gives an iterator of evenly spaced values. The function takes two parameters. start is by default 0 and step is by default 1. Using defaults will generate infinite iterator. Use break to terminate loop.
import itertools percentNumbers = [ ] finish = "n" num = "0" for x in itertools.count() : num = input("enter the mark : ") num = float(num) percentNumbers.append(num) finish = input("stop? (y/n) ") if finish=='y':break print(percentNumbers)
Sample output of the above script
enter the mark : 11 stop? (y/n) enter the mark : 22 stop? (y/n) enter the mark : 33 stop? (y/n) y [11.0, 22.0, 33.0]
- Related Articles
- How to convert a Python for loop to while loop?
- Difference between for loop and while loop in Python
- How does Python while loop work?
- How to emulate a do-while loop in Python?
- How can I write in order with for loop or while loop?
- How to use for loop in Python?
- How to use ‘while loop’ in Java?
- How to use C# do while loop?
- do…while loop vs. while loop in C/C++
- Convert a nested for loop to a map equivalent in Python
- For Versus While Loop in C
- Difference Between for and while loop
- How to break a for loop in Python?
- Java while loop
- How to use ‘do while loop’ in Java?

Advertisements