By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,728 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,728 IT Pros & Developers. It's quick & easy.

Can I store a variable inside of a function?

P: 6
I currently have a variable in one of my functions. A separate function after it utilizes the variable but cannot seem to use it and creates a syntax error. Is it possible to store a variable inside of a function?
Jun 7 '13 #1
Share this Question
Share on Google+
5 Replies

Expert Mod 2.5K+
P: 2,851
Can you post some code that demonstrates your issue?
Jun 7 '13 #2

P: 6
My code is a bit long so I'll sum it up here.
Expand|Select|Wrap|Line Numbers
  1. def weapon():
  2.    weapon = raw_input("What weapon would you like?")
  3. def forest_troll():
  4.    return "You stroke out with your " + weapon + "."
  5. weapon(x)
  6. forest_troll()
it results in a syntax error when i try to pull the value of the variable from the previous function.
Also, the spaces/indents didnt show up sorry
Jun 7 '13 #3

Expert Mod 2.5K+
P: 2,851
Return the weapon selection in function weapon() similar to:
Expand|Select|Wrap|Line Numbers
  1. >>> def weapon():
  2. ...     return raw_input("What weapon would you like?")
  3. ... 
  4. >>> def forest_troll(w):
  5. ...     return "You stroke out with your " + w + "."
  6. ... 
  7. >>> forest_troll(weapon())
  8. 'You stroke out with your Knife.'
  9. >>> 
Your code will indent properly by using code tags.
Jun 7 '13 #4

P: 6
I tried your suggested code but it didn't work for me?
Jun 7 '13 #5

Expert Mod 2.5K+
P: 2,851
What did not work about it? It does work as you can see. How did you attempt to apply it?
Jun 10 '13 #6

Post your reply

Sign in to post your reply or Sign up for a free account.