how we can use variables, which are described inside the loop, out side the loop in python












0














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')









share|improve this question
























  • 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 each float(line) to.
    – quamrana
    Nov 12 at 15:55
















0














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')









share|improve this question
























  • 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 each float(line) to.
    – quamrana
    Nov 12 at 15:55














0












0








0







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')









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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


















  • 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 each float(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












3 Answers
3






active

oldest

votes


















1














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])





share|improve this answer





















  • 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 for print(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



















0














You could try creating a vector gobs outside the loop, and filling it up within the loop over lines.
That should do.






share|improve this answer





























    0














    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)





    share|improve this answer























      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      1














      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])





      share|improve this answer





















      • 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 for print(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
















      1














      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])





      share|improve this answer





















      • 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 for print(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














      1












      1








      1






      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])





      share|improve this answer












      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])






      share|improve this answer












      share|improve this answer



      share|improve this answer










      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 for print(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










      • 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










      • 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













      0














      You could try creating a vector gobs outside the loop, and filling it up within the loop over lines.
      That should do.






      share|improve this answer


























        0














        You could try creating a vector gobs outside the loop, and filling it up within the loop over lines.
        That should do.






        share|improve this answer
























          0












          0








          0






          You could try creating a vector gobs outside the loop, and filling it up within the loop over lines.
          That should do.






          share|improve this answer












          You could try creating a vector gobs outside the loop, and filling it up within the loop over lines.
          That should do.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 15:55









          beteraba

          89114




          89114























              0














              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)





              share|improve this answer




























                0














                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)





                share|improve this answer


























                  0












                  0








                  0






                  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)





                  share|improve this answer














                  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)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 13 at 3:24

























                  answered Nov 12 at 15:55









                  Riley Steele Parsons

                  23116




                  23116






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Bressuire

                      Vorschmack

                      Quarantine