Repeated Value - Python Programming Exercise

In this exercise, you will develop a Python program that prompts the user for a number and a quantity, then displays that number repeated as many times as the user specifies. You will practice using both while loops and for loops to repeat an operation multiple times, which is a fundamental concept in Python programming. By completing this exercise, you will improve your understanding of loop structures in Python, specifically how to repeat tasks using while and for. This will help you create flexible Python programs that can handle dynamic input and perform repetitive tasks efficiently.

 Category

Mastering Flow Control

 Exercise

Repeated Value

 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

 Copy 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

 More Python Programming Exercises of Mastering Flow Control

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Access Code

    In this exercise, you will develop a Python program that prompts the user to enter their login and password (both must be integer numbers). The p...

  •  Access Code V2

    In this exercise, you will develop a Python program that prompts the user for their login and password, both of which must be integers. The progr...

  •  Multiple Divisions

    In this exercise, you will develop a Python program that prompts the user for two numbers and displays their division and remainder. If 0 is entered as ...

  •  Multiple Times Tables (Using While)

    In this exercise, you will develop a Python program that displays multiplication tables from 2 to 6 using nested "while" loops. The program will iterate throug...

  •  Quadrilateral

    In this exercise, you will develop a Python program that prompts the user to enter a number and a width, and then displays a square of that width using ...

  •  Pause & Proceed

    In this exercise, you will develop a Python program that writes the even numbers from 10 to 20, both inclusive, except 16. The program will implement three different methods...