print("Welcome")
barcode1 = input("Please enter your first digit")
barcode2 = input("Please enter your second digit")
barcode3 = input("Please enter your third digit")
barcode4 = input("Please enter your fourth digit")
barcode5 = input("Please enter your fifth digit")
barcode6 = input("Please enter your sixth digit")
barcode7 = input("Please enter your seventh digit")
barcode1 = barcode1*3
print(barcode1)
这个数不是乘以3,而是111
这里令人困惑的现象是python支持字符串乘法和整数乘法!对于没有经验的人来说,这看起来很混乱,但实际上这是一个很好的特性。可以执行以下操作:
如您所见,将字符串相乘会将其内容重复
n
次,其中n
是它相乘的数字。你知道吗在本例中,您希望将一个数值相乘,但是
input
函数返回的是一个字符串值而不是一个数值。这意味着当您对它进行乘法时,python将执行字符串乘法,而不是执行数字乘法。你知道吗只需使用
int
方法将input
的结果转换为整数。或者,您甚至可以编写一个函数来接受用户的数字输入。你知道吗你可以这样做:
在它周围添加一个try-catch语句,并尝试将您的输入转换为int。你知道吗
将
barcode1
从字符串更改为整数,例如:相关问题 更多 >
编程相关推荐