Reputation: 17
#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:
Write a program that uses a for loop to calculate any number X 4.
Hint - Add the number to a variable 4 times.
Upvotes: 0
Views: 351
Reputation: 780974
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)
Upvotes: 3
Reputation: 7
You can always create another variable to store the total, or you could simply multiply the number initially by 4 instead of using a loop. Otherwise, like you said, your code is actually doing:
3 + 3 = 6
6 + 6 = 12
since you overwrite the initially supplied number.
Upvotes: -1