Skip to main content
 首页 » 编程设计

python之我需要将每个其他数字乘以 3 但不知道我做错了什么

2024年10月24日11cloudgamer

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。有了它,您还可以输入一个字符串,但您的脚本不会崩溃。