Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

5 repeats of a for loop multiplication

I’m a beginner at python, and have been given this task:
Write a program that lets a user input numbers and have them multiplied. The user can use the program 5 times, then it should stop. Use at least a for loop and a function.

I believe the code here will ask for the numbers, multiply and give the answer, but how do I get it to run 5 times then stop asking for more numbers?

# get inputs
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))

# calculate product
product = 0
for i in range(1,num2+1):
    product=product+num1`  

# give answer
print("The Product of Number:", product)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

how do I get it to run 5 times then stop asking for more numbers?

  1. Get rid of the for i in range(1,num2+1) loop, since you don’t need a loop in order to calculate the product of two numbers.
  2. Add a for i in range(5) loop at the very top of your code, and then indent everything under it 4 spaces to the right.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading