29/09/2024
25+ Python While Loop Questions and Answers:
1. Simple Countdown
Write a Python program that counts from 10 to 1 and print each number with the help of the while loop.
count = 10
while count > 0:
print(count)
count -= 1
print("Blast off!")
2. The sum of Positive Numbers
Create a Python while loop program that continuously asks the user for a positive number and keeps a running total. The loop must stop when the user enters a negative number.
total = 0
while True:
number = float(input("Enter a positive number (or a negative number to stop): "))
if number < 0:
break
total += number
print("Total sum of positive numbers:", total)
3. Password Validation
correct_password = "ProgrammingFunda@1"
user_input = ""
while user_input != correct_password:
user_input = input("Enter the password: ")
print("Access granted!")
4. Fibonacci Sequence
The Fibonacci Sequence in Python is one of the most asked questions during the Python interviews. I faced this question so many times in Python interviews.
Fibonacci Sequence:- The Fibonacci Sequence is a sequence of some numbers where one is the sum of two preceding numbers like 0, 1, 1, 2, 3, etc
Let’s write a Python code to generate the Fibonacci series in Python.
num = int(input("Enter a number:- "))
if num > 0:
i = 0
a = 0
b = 1
while i < num:
print(a, end=' ')
a, b = b, a + b
i = i + 1
Learn more about :- https://www.programmingfunda.com/python-while-loop-questions-and-answers/
💯Follow Programming Funda for more Python's content.
Thanks
Hi, In this article we are about to explore Python while loop questions and answers with the help of the examples.