def multi():
i = 1
x = input()
while i <= 9:
print(x, ' * ', i, ' = ', x*i)
i = i + 1
multi()
I'm learning the concept of a function, and I'm asking you a question because a random value came out in the instance part. I ran that code...
4 * 1 = 4
4 * 2 = 44
4 * 3 = 444
4 * 4 = 4444
4 * 5 = 44444
4 * 6 = 444444
4 * 7 = 4444444
4 * 8 = 44444444
4 * 9 = 444444444
The results are coming out like this. Actually, what I wanted was
4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
...
``It was something like this
Where did it go wrong?
You can think about it from the common sense line.
'a' * 3 = 'aaa'. ' I'm referring to writing.
4 * 3 = 12 but 4 * 3 = 444.
It's important to understand that numbers and letters are different things.
What's the solution? You can explicitly convert it to a number.
Please refer to the example below.
print('3' * 4)
print(int('3') * 4)
'3333'
12
© 2024 OneMinuteCode. All rights reserved.