Stiglix
Stiglix

Reputation: 21

How to print total amount of 'weight' followed by print 'grams'

Simple Math, however displaying it in python has become tricky for me.

User inputs how much 'chocolate' they want.

Then they want to know how much (in grams) chocolate is needed to make the bar.

Assume 1 bar = 10 grams.

Answer comes back:

Total amount of grams needed is 1111111111

I got 10 1's rather than 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'

Upvotes: 0

Views: 61

Answers (3)

Shayan
Shayan

Reputation: 2471

Multiply 10 with choc_qty(int) instead of x(string).

When you multiply a string by an integer, Python returns a new string. This new string is the original string, repeated X number of times (that happened in your case).

Change line 3 to:

weight = choc_qty * 10

Upvotes: 0

Ishtiaque05
Ishtiaque05

Reputation: 451

x=input('Enter quantity of chocolate')
choc_qty=int(x)
weight= choc_qty *10
print('total amount of grams needed is',weight)

Here is your full code.

Upvotes: 1

Safak Ozdek
Safak Ozdek

Reputation: 1005

You have just a simple mistake, you are multiplying string value with 10. That's why you are ending up with "1111111111" because multiplication works like this in strings.

weight = (x) * (10)

Also you can remove redundant paranthesis. For easy solution just change line 3 to:

weight = choc_qty * 10

It should work.

Upvotes: 1

Related Questions