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
最佳答案
你可以这样做:
codes = []
i = 0
while True:
try:
codes.append(int(input("Please input your Barcode {}: ".format(i))) * 3)
if i == 6: break
i += 1
except ValueError:
print("Something went wrong!")
print(codes)
在其周围添加一个 try-catch 语句并尝试将您的输入转换为 int。这样您也可以输入一个字符串,但您的脚本不会崩溃。
关于python - 我需要将所有其他数字乘以 3,但不知道我做错了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35635951/