-2

I have written this piece of code where you can input the amount of chocolate needed in a recipe and it will return the amount in 5kg and 1kg pieces. What is wrong which this piece of code? I have got integers for the variables but it shows a syntax error!

kg = int(input("How much chocolate do you need?"))
fivekgs = kg // 5
onekgs = kg % 5
print ("You will need,"fivekgs" 5kg blocks and, "onekgs" 1kg blocks!")

Have i got the speech mark placements wrong or something?

Thanks.

  • 3
    Does this answer your question? [python string ' " ' : single double quote inside string](https://stackoverflow.com/questions/13651779/python-string-single-double-quote-inside-string) – ChrisGPT was on strike Mar 20 '20 at 13:57
  • Look at the syntax highlighting you get here (and hopefully in your editor). The colour change for `fivekgs` and `onekgs` is a clear indication that something weird is happening inside your string. – ChrisGPT was on strike Mar 20 '20 at 13:58
  • With the updated code it's clear that you're trying to interpolate variables, not nest quotes. See [Is there a Python equivalent to Ruby's string interpolation?](https://stackoverflow.com/q/4450592/354577) and [Python's many ways of string formatting — are the older ones (going to be) deprecated?](https://stackoverflow.com/q/13451989/354577) instead. – ChrisGPT was on strike Mar 20 '20 at 13:59
  • 1
    It looks like you want this: `"You will need, '{}' 5kg blocks and, '{}' 1kg blocks!".format(fivegs, onekgs)` – Andrew Mar 20 '20 at 14:01

2 Answers2

0

change it to:

print("You will need, 'fivekgs' 5kg blocks and, 'onekgs' 1kg blocks!")

Or:

print('You will need, "fivekgs" 5kg blocks and, "onekgs" 1kg blocks!')

Thomas
  • 1,214
  • 4
  • 18
  • 45
0

Try this:

print(f'You will need,{fivekgs} 5kg blocks and, {onekgs} 1kg blocks!')
xavi
  • 65
  • 5