for i in range(5):
name = input("Enter your name: ")
age = input("Enter your age: ")
gender = input("Enter your gender: ")
print("Name:", name)
print("Age:", age)
print("Gender:", gender)
print()
print("Thank you for providing your information!")
for
loop is used to iterate five times. Inside the loop, input
function is used to prompt the user for their name, age, and gender. The values entered by the user are stored in the variables name
, age
, and gender
.print
statements. A blank line is printed using print()
to separate the iterations.master paano naman pah while loopSure! Here's a Python program that asks the user for their name, age, and gender five times:
Python:for i in range(5): name = input("Enter your name: ") age = input("Enter your age: ") gender = input("Enter your gender: ") print("Name:", name) print("Age:", age) print("Gender:", gender) print() print("Thank you for providing your information!")
In this program, afor
loop is used to iterate five times. Inside the loop,input
function is used to prompt the user for their name, age, and gender. The values entered by the user are stored in the variablesname
,age
, andgender
.
After collecting the information, the program prints the entered values usingprint()
to separate the iterations.
Finally, the program prints a thank you message after the loop completes.
Certainly! Here's an example of the same program that uses a while loop instead of a for loop:master paano naman pah while loop
i = 0
while i < 5:
name = input("Enter your name: ")
age = input("Enter your age: ")
gender = input("Enter your gender: ")
print("Name:", name)
print("Age:", age)
print("Gender:", gender)
print()
i += 1
print("Thank you for providing your information!")
i
to 0 before the while loop. The loop will continue as long as i
is less than 5. Inside the loop, the program prompts the user for their name, age, and gender, and then prints the entered values. After that, we increment the value of i
by 1 using i += 1
to ensure that the loop will eventually terminate after 5 iterations.