473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Can I store a variable inside of a function?

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
5 1538
bvdet
2,851 Expert Mod 2GB
Can you post some code that demonstrates your issue?
Jun 7 '13 #2
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
bvdet
2,851 Expert Mod 2GB
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
@bvdet
I tried your suggested code but it didn't work for me?
Jun 7 '13 #5
bvdet
2,851 Expert Mod 2GB
What did not work about it? It does work as you can see. How did you attempt to apply it?
Jun 10 '13 #6

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

Similar topics

1
by: Otto Porter | last post by:
/*Given*/ CREATE TABLE ( IDENTITY (1, 1) NOT NULL , NULL , (2) NULL , NULL , CONSTRAINT PRIMARY KEY CLUSTERED (
4
by: Er Galv?o Abbott | last post by:
Greetings. I have a function that does some pattern matching with JS's RegEx and I'm trying to use a variable inside of it. Nothing that I've done worked, so please help me. Here is the func:...
7
by: lallous | last post by:
Hello, Why is the static variable inside a member function static for all instances and not static for the current instance of the object? -- Elias
8
by: nescio | last post by:
hello, i have got a javascript that opens a new window. when i call the javascript function i want to ad a variable. what you see on the pop up depends on the variable. but the variable in...
10
by: Raghavendra RAV | last post by:
Hi, I need to store an image from a Graphics object(.NET) inside a xml data island. Anyone might have came accross or have an idea how to do this. Please share. Thanks & Regards, Raghu, CSS...
7
by: Action | last post by:
in c++ if i put static int count = 0; in a method; count can only accessed inside the method and retain it's value. is there equilvant thing in C#? thx
1
by: Matt Jensen | last post by:
Howdy I'm hoping to put a variable inside a variable name. In JavaScript, you would use eval(myvar+i) or something like that. What is the C# equiavlent of eval? Or how do I do it in C# .NET? ...
0
by: Dave | last post by:
Hi , I have the following where I need to append a string variable inside a hyperlink template, inside a datalist. The variable 'categoryList' is the culprit. No matter where this variable is...
7
by: nospam | last post by:
Hello All, Is it possible to place a variable inside of a textbox and have it viewed as the value? Or even read in as a string. Here's the problem... I have a text file I would like to read in...
4
Dheeraj Joshi
by: Dheeraj Joshi | last post by:
Hi, I was wondering is there any technique available, so we can access the global variable inside a function if we have a local variable inside the function with the same name as global variable. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.