-4

Im stuck with this very simple code were I'm trying to create a function that takes a parameter and adds 1 to the result and returns it but somehow this code gives me no results. (I've called the function to see if it works.)

Somebody please help me since I'm very new to python :)

def increment(num):
    num += 1
a = int(input("Type a number "))
increment(a)`

I changed it to

def increment(num):
    return num + 1
a = int(input("Type a number "))
increment(a)`

but still no results are showing after I enter a number, Does anybody know?

Miles
  • 100
  • 1
  • 10
John
  • 27
  • 1
  • 9

2 Answers2

-1

You need to ensure you return the num in the increment function and assign it to a. Return: def increment(num): return num + 1

Assign: a = increment(a)

Georgina S
  • 467
  • 2
  • 9
  • if they are passing through parameter a as num wouldnt you do a = increment(a), where increment returns num + 1 and not use a global. I am confused as what you are trying to say by the example. Anyway hopefully I have edited answer to make it more explicit. – Georgina S Apr 19 '16 at 13:09
  • ah my bad! I was trying to reformat my comment on the other answer not yours – Mixone Apr 19 '16 at 13:14
  • That's alright, I'm still learning so just getting feedback where I can! – Georgina S Apr 19 '16 at 13:16
  • we never do stop learning haha – Mixone Apr 19 '16 at 13:18
-1

You need to return some value or it never will appear.

def increment(num):
    return num + 1
a = int(input("Type a number "))
increment(a)
Marc Cabos
  • 39
  • 4
  • this is incorrect, you are not modifying the variable outside the scope of the function, you would need to use the global keyword – Mixone Apr 19 '16 at 12:53
  • Example of it: [NoReturn + Return + Global](http://postimg.org/image/gmvxkrhdh/) – Mixone Apr 19 '16 at 12:59