how we can use variables, which are described inside the loop, out side the loop in python
Below is my code. Here, I am trying to read the variable gobs(x) from an input file and then I want to use it for other calculations, eg., computing error(x). But, I found, I can read it from input file properly within the loop, but when trying to use it outside the loop, only the first data is getting transferred. For all 100 data, which I read as gobs(x) inside the loop, it is showing the value of last data only, when I am using it outside the loop.
code started below
constant = 99
x0=50
z0=5
def gsyn (x):
return (constant*z0)/(z0**2+(x-x0)**2)
with open ('Grav_H_Cyln_v3_output.txt') as finp:
lines=finp.readlines()
for line in lines:
g=float(line)
x=line
def gobs (x):
return g
print (gobs(x)) # here, gobs(x) is printing properly
def error(x):
return (gsyn(x)-gobs(x))
for i in range (1, 100, 1):
x=i
print (error(x)) # here, only the first value of gobs(x) is coming
print ('stop')
python
add a comment |
Below is my code. Here, I am trying to read the variable gobs(x) from an input file and then I want to use it for other calculations, eg., computing error(x). But, I found, I can read it from input file properly within the loop, but when trying to use it outside the loop, only the first data is getting transferred. For all 100 data, which I read as gobs(x) inside the loop, it is showing the value of last data only, when I am using it outside the loop.
code started below
constant = 99
x0=50
z0=5
def gsyn (x):
return (constant*z0)/(z0**2+(x-x0)**2)
with open ('Grav_H_Cyln_v3_output.txt') as finp:
lines=finp.readlines()
for line in lines:
g=float(line)
x=line
def gobs (x):
return g
print (gobs(x)) # here, gobs(x) is printing properly
def error(x):
return (gsyn(x)-gobs(x))
for i in range (1, 100, 1):
x=i
print (error(x)) # here, only the first value of gobs(x) is coming
print ('stop')
python
loops do not create a scope in python, functions do
– Chris_Rands
Nov 12 at 15:52
You are recreatinggobs
in each iteration..
– Maor Refaeli
Nov 12 at 15:52
It sounds like you want to process data that starts off in a file. As you read the data from the file, you should store it somewhere. Perhapsgobs
should be a list that you append eachfloat(line)
to.
– quamrana
Nov 12 at 15:55
add a comment |
Below is my code. Here, I am trying to read the variable gobs(x) from an input file and then I want to use it for other calculations, eg., computing error(x). But, I found, I can read it from input file properly within the loop, but when trying to use it outside the loop, only the first data is getting transferred. For all 100 data, which I read as gobs(x) inside the loop, it is showing the value of last data only, when I am using it outside the loop.
code started below
constant = 99
x0=50
z0=5
def gsyn (x):
return (constant*z0)/(z0**2+(x-x0)**2)
with open ('Grav_H_Cyln_v3_output.txt') as finp:
lines=finp.readlines()
for line in lines:
g=float(line)
x=line
def gobs (x):
return g
print (gobs(x)) # here, gobs(x) is printing properly
def error(x):
return (gsyn(x)-gobs(x))
for i in range (1, 100, 1):
x=i
print (error(x)) # here, only the first value of gobs(x) is coming
print ('stop')
python
Below is my code. Here, I am trying to read the variable gobs(x) from an input file and then I want to use it for other calculations, eg., computing error(x). But, I found, I can read it from input file properly within the loop, but when trying to use it outside the loop, only the first data is getting transferred. For all 100 data, which I read as gobs(x) inside the loop, it is showing the value of last data only, when I am using it outside the loop.
code started below
constant = 99
x0=50
z0=5
def gsyn (x):
return (constant*z0)/(z0**2+(x-x0)**2)
with open ('Grav_H_Cyln_v3_output.txt') as finp:
lines=finp.readlines()
for line in lines:
g=float(line)
x=line
def gobs (x):
return g
print (gobs(x)) # here, gobs(x) is printing properly
def error(x):
return (gsyn(x)-gobs(x))
for i in range (1, 100, 1):
x=i
print (error(x)) # here, only the first value of gobs(x) is coming
print ('stop')
python
python
edited Nov 12 at 16:38
asked Nov 12 at 15:46
LRoy
11
11
loops do not create a scope in python, functions do
– Chris_Rands
Nov 12 at 15:52
You are recreatinggobs
in each iteration..
– Maor Refaeli
Nov 12 at 15:52
It sounds like you want to process data that starts off in a file. As you read the data from the file, you should store it somewhere. Perhapsgobs
should be a list that you append eachfloat(line)
to.
– quamrana
Nov 12 at 15:55
add a comment |
loops do not create a scope in python, functions do
– Chris_Rands
Nov 12 at 15:52
You are recreatinggobs
in each iteration..
– Maor Refaeli
Nov 12 at 15:52
It sounds like you want to process data that starts off in a file. As you read the data from the file, you should store it somewhere. Perhapsgobs
should be a list that you append eachfloat(line)
to.
– quamrana
Nov 12 at 15:55
loops do not create a scope in python, functions do
– Chris_Rands
Nov 12 at 15:52
loops do not create a scope in python, functions do
– Chris_Rands
Nov 12 at 15:52
You are recreating
gobs
in each iteration..– Maor Refaeli
Nov 12 at 15:52
You are recreating
gobs
in each iteration..– Maor Refaeli
Nov 12 at 15:52
It sounds like you want to process data that starts off in a file. As you read the data from the file, you should store it somewhere. Perhaps
gobs
should be a list that you append each float(line)
to.– quamrana
Nov 12 at 15:55
It sounds like you want to process data that starts off in a file. As you read the data from the file, you should store it somewhere. Perhaps
gobs
should be a list that you append each float(line)
to.– quamrana
Nov 12 at 15:55
add a comment |
3 Answers
3
active
oldest
votes
This seems like a very odd solution to what is fundamentally a very simple problem. Make gobs
a dictionary so you can set or retrieve gobs[x]
at will.
gobs = dict()
with open ('Grav_H_Cyln_v3_output.txt') as finp:
lines=finp.readlines()
for line in lines:
g=float(line)
gobs[line] = g
print (gobs[line])
Thank you so much., after making gobs a dictionary, I can see the data outside the loop. But, it is not coming as sub-scripted variable like gobs(1), gobs(2) etc. If I am giving print gobs[line], it is giving error. If i give print gobs, it is printing all 100 values. I need to do operation one by one, that is gsyn(1)-gobs(1), gsyn(2)-gobs(2), like that. How I can do that? Please help.
– LRoy
Nov 12 at 17:24
Use square brackets around an array or dictionary subscript, i.e.gsyn(1) - gobs[x]
etc
– tripleee
Nov 12 at 17:26
What error do you get forprint(gobs[line])
?
– tripleee
Nov 12 at 17:27
I tried the below
– LRoy
Nov 12 at 17:35
for i in range (1, 100, 1): line=i print (gobs[line])
– LRoy
Nov 12 at 17:36
|
show 4 more comments
You could try creating a vector gobs
outside the loop, and filling it up within the loop over lines
.
That should do.
add a comment |
Instead of reassigning the value of x
on each iteration of your loop, append i
to a list that is declared outside of the if-block scope.
x =
for i in range (1, 100, 1):
x.append(i)
print(x)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265565%2fhow-we-can-use-variables-which-are-described-inside-the-loop-out-side-the-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This seems like a very odd solution to what is fundamentally a very simple problem. Make gobs
a dictionary so you can set or retrieve gobs[x]
at will.
gobs = dict()
with open ('Grav_H_Cyln_v3_output.txt') as finp:
lines=finp.readlines()
for line in lines:
g=float(line)
gobs[line] = g
print (gobs[line])
Thank you so much., after making gobs a dictionary, I can see the data outside the loop. But, it is not coming as sub-scripted variable like gobs(1), gobs(2) etc. If I am giving print gobs[line], it is giving error. If i give print gobs, it is printing all 100 values. I need to do operation one by one, that is gsyn(1)-gobs(1), gsyn(2)-gobs(2), like that. How I can do that? Please help.
– LRoy
Nov 12 at 17:24
Use square brackets around an array or dictionary subscript, i.e.gsyn(1) - gobs[x]
etc
– tripleee
Nov 12 at 17:26
What error do you get forprint(gobs[line])
?
– tripleee
Nov 12 at 17:27
I tried the below
– LRoy
Nov 12 at 17:35
for i in range (1, 100, 1): line=i print (gobs[line])
– LRoy
Nov 12 at 17:36
|
show 4 more comments
This seems like a very odd solution to what is fundamentally a very simple problem. Make gobs
a dictionary so you can set or retrieve gobs[x]
at will.
gobs = dict()
with open ('Grav_H_Cyln_v3_output.txt') as finp:
lines=finp.readlines()
for line in lines:
g=float(line)
gobs[line] = g
print (gobs[line])
Thank you so much., after making gobs a dictionary, I can see the data outside the loop. But, it is not coming as sub-scripted variable like gobs(1), gobs(2) etc. If I am giving print gobs[line], it is giving error. If i give print gobs, it is printing all 100 values. I need to do operation one by one, that is gsyn(1)-gobs(1), gsyn(2)-gobs(2), like that. How I can do that? Please help.
– LRoy
Nov 12 at 17:24
Use square brackets around an array or dictionary subscript, i.e.gsyn(1) - gobs[x]
etc
– tripleee
Nov 12 at 17:26
What error do you get forprint(gobs[line])
?
– tripleee
Nov 12 at 17:27
I tried the below
– LRoy
Nov 12 at 17:35
for i in range (1, 100, 1): line=i print (gobs[line])
– LRoy
Nov 12 at 17:36
|
show 4 more comments
This seems like a very odd solution to what is fundamentally a very simple problem. Make gobs
a dictionary so you can set or retrieve gobs[x]
at will.
gobs = dict()
with open ('Grav_H_Cyln_v3_output.txt') as finp:
lines=finp.readlines()
for line in lines:
g=float(line)
gobs[line] = g
print (gobs[line])
This seems like a very odd solution to what is fundamentally a very simple problem. Make gobs
a dictionary so you can set or retrieve gobs[x]
at will.
gobs = dict()
with open ('Grav_H_Cyln_v3_output.txt') as finp:
lines=finp.readlines()
for line in lines:
g=float(line)
gobs[line] = g
print (gobs[line])
answered Nov 12 at 15:55
tripleee
88.3k12123179
88.3k12123179
Thank you so much., after making gobs a dictionary, I can see the data outside the loop. But, it is not coming as sub-scripted variable like gobs(1), gobs(2) etc. If I am giving print gobs[line], it is giving error. If i give print gobs, it is printing all 100 values. I need to do operation one by one, that is gsyn(1)-gobs(1), gsyn(2)-gobs(2), like that. How I can do that? Please help.
– LRoy
Nov 12 at 17:24
Use square brackets around an array or dictionary subscript, i.e.gsyn(1) - gobs[x]
etc
– tripleee
Nov 12 at 17:26
What error do you get forprint(gobs[line])
?
– tripleee
Nov 12 at 17:27
I tried the below
– LRoy
Nov 12 at 17:35
for i in range (1, 100, 1): line=i print (gobs[line])
– LRoy
Nov 12 at 17:36
|
show 4 more comments
Thank you so much., after making gobs a dictionary, I can see the data outside the loop. But, it is not coming as sub-scripted variable like gobs(1), gobs(2) etc. If I am giving print gobs[line], it is giving error. If i give print gobs, it is printing all 100 values. I need to do operation one by one, that is gsyn(1)-gobs(1), gsyn(2)-gobs(2), like that. How I can do that? Please help.
– LRoy
Nov 12 at 17:24
Use square brackets around an array or dictionary subscript, i.e.gsyn(1) - gobs[x]
etc
– tripleee
Nov 12 at 17:26
What error do you get forprint(gobs[line])
?
– tripleee
Nov 12 at 17:27
I tried the below
– LRoy
Nov 12 at 17:35
for i in range (1, 100, 1): line=i print (gobs[line])
– LRoy
Nov 12 at 17:36
Thank you so much., after making gobs a dictionary, I can see the data outside the loop. But, it is not coming as sub-scripted variable like gobs(1), gobs(2) etc. If I am giving print gobs[line], it is giving error. If i give print gobs, it is printing all 100 values. I need to do operation one by one, that is gsyn(1)-gobs(1), gsyn(2)-gobs(2), like that. How I can do that? Please help.
– LRoy
Nov 12 at 17:24
Thank you so much., after making gobs a dictionary, I can see the data outside the loop. But, it is not coming as sub-scripted variable like gobs(1), gobs(2) etc. If I am giving print gobs[line], it is giving error. If i give print gobs, it is printing all 100 values. I need to do operation one by one, that is gsyn(1)-gobs(1), gsyn(2)-gobs(2), like that. How I can do that? Please help.
– LRoy
Nov 12 at 17:24
Use square brackets around an array or dictionary subscript, i.e.
gsyn(1) - gobs[x]
etc– tripleee
Nov 12 at 17:26
Use square brackets around an array or dictionary subscript, i.e.
gsyn(1) - gobs[x]
etc– tripleee
Nov 12 at 17:26
What error do you get for
print(gobs[line])
?– tripleee
Nov 12 at 17:27
What error do you get for
print(gobs[line])
?– tripleee
Nov 12 at 17:27
I tried the below
– LRoy
Nov 12 at 17:35
I tried the below
– LRoy
Nov 12 at 17:35
for i in range (1, 100, 1): line=i print (gobs[line])
– LRoy
Nov 12 at 17:36
for i in range (1, 100, 1): line=i print (gobs[line])
– LRoy
Nov 12 at 17:36
|
show 4 more comments
You could try creating a vector gobs
outside the loop, and filling it up within the loop over lines
.
That should do.
add a comment |
You could try creating a vector gobs
outside the loop, and filling it up within the loop over lines
.
That should do.
add a comment |
You could try creating a vector gobs
outside the loop, and filling it up within the loop over lines
.
That should do.
You could try creating a vector gobs
outside the loop, and filling it up within the loop over lines
.
That should do.
answered Nov 12 at 15:55
beteraba
89114
89114
add a comment |
add a comment |
Instead of reassigning the value of x
on each iteration of your loop, append i
to a list that is declared outside of the if-block scope.
x =
for i in range (1, 100, 1):
x.append(i)
print(x)
add a comment |
Instead of reassigning the value of x
on each iteration of your loop, append i
to a list that is declared outside of the if-block scope.
x =
for i in range (1, 100, 1):
x.append(i)
print(x)
add a comment |
Instead of reassigning the value of x
on each iteration of your loop, append i
to a list that is declared outside of the if-block scope.
x =
for i in range (1, 100, 1):
x.append(i)
print(x)
Instead of reassigning the value of x
on each iteration of your loop, append i
to a list that is declared outside of the if-block scope.
x =
for i in range (1, 100, 1):
x.append(i)
print(x)
edited Nov 13 at 3:24
answered Nov 12 at 15:55
Riley Steele Parsons
23116
23116
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265565%2fhow-we-can-use-variables-which-are-described-inside-the-loop-out-side-the-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
loops do not create a scope in python, functions do
– Chris_Rands
Nov 12 at 15:52
You are recreating
gobs
in each iteration..– Maor Refaeli
Nov 12 at 15:52
It sounds like you want to process data that starts off in a file. As you read the data from the file, you should store it somewhere. Perhaps
gobs
should be a list that you append eachfloat(line)
to.– quamrana
Nov 12 at 15:55