如何打印 'weight' 的总量然后打印 'grams'
How to print total amount of 'weight' followed by print 'grams'
简单的数学,但是在 python 中显示它对我来说变得很棘手。
用户输入他们想要多少'chocolate'。
然后他们想知道制作巧克力棒需要多少(以克为单位)巧克力。
假设 1 巴 = 10 克。
答案返回:
需要的总克数是 1111111111
我得到了 10 个 1 而不是 1 x 10。
x=input('Enter quantity of chocolate')
choc_qty=int(x)
weight=(x)*(10)
print(total amount of grams needed is',weight)
'Total amount of grams needed is 1111111111'
您只是犯了一个简单的错误,您将字符串值乘以 10。这就是为什么您以“1111111111”结尾的原因,因为乘法在字符串中是这样进行的。
weight = (x) * (10)
您也可以删除多余的括号。对于简单的解决方案,只需将第 3 行更改为:
weight = choc_qty * 10
应该可以。
x=input('Enter quantity of chocolate')
choc_qty=int(x)
weight= choc_qty *10
print('total amount of grams needed is',weight)
这是您的完整代码。
用 choc_qty(int) 代替 x(string) 乘以 10。
当您将一个字符串乘以一个整数时,Python returns 一个新的字符串。这个新字符串是原始字符串,重复了 X 次(发生在您的情况下)。
将第 3 行更改为:
weight = choc_qty * 10
简单的数学,但是在 python 中显示它对我来说变得很棘手。
用户输入他们想要多少'chocolate'。
然后他们想知道制作巧克力棒需要多少(以克为单位)巧克力。
假设 1 巴 = 10 克。
答案返回:
需要的总克数是 1111111111
我得到了 10 个 1 而不是 1 x 10。
x=input('Enter quantity of chocolate')
choc_qty=int(x)
weight=(x)*(10)
print(total amount of grams needed is',weight)
'Total amount of grams needed is 1111111111'
您只是犯了一个简单的错误,您将字符串值乘以 10。这就是为什么您以“1111111111”结尾的原因,因为乘法在字符串中是这样进行的。
weight = (x) * (10)
您也可以删除多余的括号。对于简单的解决方案,只需将第 3 行更改为:
weight = choc_qty * 10
应该可以。
x=input('Enter quantity of chocolate')
choc_qty=int(x)
weight= choc_qty *10
print('total amount of grams needed is',weight)
这是您的完整代码。
用 choc_qty(int) 代替 x(string) 乘以 10。
当您将一个字符串乘以一个整数时,Python returns 一个新的字符串。这个新字符串是原始字符串,重复了 X 次(发生在您的情况下)。
将第 3 行更改为:
weight = choc_qty * 10