为什么乘法会多次重复这个数字? [英] Why does multiplication repeats the number several times?
本文介绍了为什么乘法会多次重复这个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道如何在 Python 中乘法.
如果我这样做:
价格 = 1 * 9
看起来像这样:
111111111
答案必须是9
(1x9=9
)
我怎样才能让它正确倍增?
解决方案
只有将整数与字符串相乘时,才会得到重复的字符串..
您可以使用 int()
工厂方法从整数的字符串形式创建整数..
- 如果两个操作数都是整数,您将得到它们的乘积为整数.
- 如果一个操作数是一个int,另一个是一个字符串,那么这个字符串将被重复int指定的次数.
I don't know how to multiply in Python.
If I do this:
price = 1 * 9
It will appear like this:
111111111
And the answer needs to be 9
(1x9=9
)
How can I make it multiply correctly?
解决方案
Only when you multiply integer with a string, you will get repetitive string..
You can use int()
factory method to create integer out of string form of integer..
>>> int('1') * int('9')
9
>>>
>>> '1' * 9
'111111111'
>>>
>>> 1 * 9
9
>>>
>>> 1 * '9'
'9'
- If both operands are ints, you will get multiplication of them as int.
- If one operand is an int and the other is a string, then the string will be repeated the number of times specified by the int.
这篇关于为什么乘法会多次重复这个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文