Objective
Develop a Python program that prompts the user for a number and a quantity, and displays that number repeated as many times as the user has specified. Here's an example:
Enter a number: 4
Enter a quantity: 5
44444
You must display it three times: first using "while" and then using "for".
Example Python Exercise
Show Python Code
# Prompt the user to enter a number
num = input("Enter a number: ")
# Prompt the user to enter a quantity
quantity = int(input("Enter a quantity: "))
# Display the number repeated as many times as specified using a while loop
i = 0
while i < quantity:
print(num, end="")
i += 1
print() # Move to the next line
# Display the number repeated as many times as specified using a for loop
for _ in range(quantity):
print(num, end="")
print() # Move to the next line
# Display the number repeated as many times as specified using string multiplication
print(num * quantity)
Output
Enter a number: 4
Enter a quantity: 5
44444
44444
44444
Share this Python Exercise