0

When I run the following code, it prints the word 'None'. It should print the amount entered by the user. Why does it do this, and how do it fix it?

def amount(thing):
    amnt = int(input('How many ' + thing + ' would you like?'))

bolts_amount = amount('bolts')
print(bolts_amount)
james hughes
  • 597
  • 2
  • 5
  • 11

1 Answers1

2

You need to return a value from the function, otherwise it defaults to returning None

def amount(thing):
    amnt = int(input('How many ' + thing + ' would you like?'))
    return amnt

bolts_amount = amount('bolts')
print(bolts_amount)

Doing amnt = int(input('How many ' + thing + ' would you like?')) locally inside your function does not effect your result outside of the function. You need to return this value, so that when you call your function amount(), it will give you a value to assign to a variable, bolts_amount

TerryA
  • 58,805
  • 11
  • 114
  • 143