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

(Beginner) Why is this not correct?

#Add the input number 4 times. Ex. 3+3+3+3
#If the input is 3, the output will be 12

num = int(input("Num: "))

for x in range(2):
    num += num

print(num)

Using an app called "Easy Coder" and
for some reason, the code above is not correct.

Is there a better way to do this? so the actual process of the code is("3+3+3+3) and not(3+3) + (3+3)

Edit: Sorry, I forgot to mention, that this is an exercise related to
looping.
The task:

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

Write a program that uses a for loop to calculate any
number X 4.
Hint – Add the number to a variable 4 times.

>Solution :

To add 4 times, use a separate variable for the total and loop 4 times instead of twice

num = int(input("Num: "))
total = 0

for _ in range(4):
    total += num

print(total)
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