-3
mood = raw_input("Enter your mood: ").lower()
def setMood(mood):
    mood = "awesome"
    return mood
if mood != "awesome":
    setMood(mood)
    print "Yor mood is now %s!" % mood
else: print "You were awesome anyway!"

Why does this return the original input, not the one overwritten in the function? And how to get around this?

UPDATE!!!

Solution:

mood = raw_input("Enter your mood: ").lower()
def setMood(mood):
    mood = "awesome"
    return mood
if mood != "awesome":
    mood = setMood(mood)
    print "Yor mood is now %s!" % mood
else: print "You were awesome anyway!"
  • This doesn't make any sense. `mood` inside `setMood` is local and is not the same variable as `mood` on line 1. Just because they are named the same doesn't mean they are the same. And what good is that function. It's not actually doing anything. Just replace `setMood` after your `if` to `mood = "awesome"`. – putvande Aug 08 '15 at 09:11
  • You could add `global mood` to tell Python that `mood` inside `setMood()` refers to the global instead of a local variable. – Ulrich Eckhardt Aug 08 '15 at 09:14
  • Actually id like to have a function that overwrites a global variable. Even though you are right and did answer my question in the first place, my question still stands – Kőhalmy Zoltán Aug 08 '15 at 09:15
  • Global mood gives the error of mood is global and local at the same time – Kőhalmy Zoltán Aug 08 '15 at 09:17
  • You need to check out the [official Python tutorial](https://docs.python.org/3.4/tutorial/index.html) and learn about saving values returned by functions. – TigerhawkT3 Aug 08 '15 at 09:29

1 Answers1

0

the mood outside setMood is global but the one inside it is local. Refer to the official python documentation for more information.