python - 我需要将所有其他数字乘以 3,但不知道我做错了什么

标签 python math numbers

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/

相关文章:

php - 检测两个数字范围是否冲突

c# - 将 double 转换为字符串,保留格式 - C#

python - 获取 Python 中整数所需的字节大小

python - cmd控制台游戏;减少眨眼

python - 使用 QLineEdit 更改 QTableWidget 标题标签

java - 具有给定长度单元测试的随机字符串

java - 在给定整数的叉积中生成一行

regex - 从 MSBuild 中的字符串中提取数字

python - 如何将模板包含到 django 包中?

python - 类型错误 - Stem() 缺少 1 个必需的位置参数 : 'word'