Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

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'

1 Answer

0 votes
by (25.1k points)

The issue in on the third line, instead of weight = (x)*10, you need to do: weight = choc_qty * 10 because x is a string and (x) * 10 will result in a string which is just x repeated 10 times, but choc_qty is an integer so it will produce the correct answer.

Related questions

Browse Categories

...