- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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]
- How to convert a Python for loop to while loop?
- How does Python while loop work?
- Java while loop
- For Versus While Loop in C
- Difference Between for and while loop
- How can I write in order with for loop or while loop?
- do…while loop vs. while loop in C/C++
- How to emulate a do-while loop in Python?
- How to use C# do while loop?
- How to use for loop in Python?
- How to create nested while loop in C#?
- How to break a for loop in Python?
- What are the differences between while loop and do-while loop in Java?