bools in python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vajratkarviraj
    New Member
    • Jun 2007
    • 15

    bools in python

    question from a newbie:
    can somebody help me in converting this c++ code to python...
    [CODE=c]
    bool flag;
    do{
    flag=0;
    for(int i=0;i<n;i++){
    double s1=xold[i];
    double s2=xnew[i];
    e[i]= s1-s2;
    if (e[i]<0) {e[i]=0-e[i];}
    if(e[i]>prec){flag=1 ;}

    for(int i=0;i<n;i++){
    xold[i]=xnew[i];
    }

    }
    }while (flag);
    [/CODE]
    basically the bool thing is a prob... also if i hav created a function(say solve(parameter s)) within a function(say display(paramet ers)).... then the python code goes sumwat like this:
    Code:
    def display(parameter1, parameter2):
    
    {code of display}
    
    solve(parameter3, parameter4):
    
    {code of solve and it gives me output its output in the form of a number}
    
    #now i want to return control to display
    
    {remaining code of display continues...}
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by vajratkarviraj
    question from a newbie:
    can somebody help me in converting this c++ code to python...
    [CODE=c]
    bool flag;
    do{
    flag=0;
    for(int i=0;i<n;i++){
    double s1=xold[i];
    double s2=xnew[i];
    e[i]= s1-s2;
    if (e[i]<0) {e[i]=0-e[i];}
    if(e[i]>prec){flag=1 ;}

    for(int i=0;i<n;i++){
    xold[i]=xnew[i];
    }

    }
    }while (flag);
    [/CODE]<snip>
    I'm not so hot with C, but it looks like the while will never execute due to flag=0, but (or maybe it executes once because while() is evaluated last) - I'll show the latter:[CODE=python]n = 4 # counter for for()
    flag = 0
    while True: # Note that python is indentation based
    for i in range(n): # use xrange() for huge numbers
    s1 = xold[i]
    s2 = xnew[i]
    e[i] = s1 - s2
    if e[i] < 0: # colon starts a block, next de-dent ends it
    e[i] = 0 - e[i]
    if e[i] > prec:
    flag=1
    # comments can go on any indent level - etc...
    if not flag:
    break[/CODE]
    That is, if I'm reading the C part correctly. Close, anyway

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by bartonc
      I'm not so hot with C, but it looks like the while will never execute due to flag=0, but (or maybe it executes once because while() is evaluated last) - I'll show the latter:[CODE=python]n = 4 # counter for for()
      flag = 0
      while True: # Note that python is indentation based
      for i in range(n): # use xrange() for huge numbers
      s1 = xold[i]
      s2 = xnew[i]
      e[i] = s1 - s2
      if e[i] < 0: # colon starts a block, next de-dent ends it
      e[i] = 0 - e[i]
      if e[i] > prec:
      flag=1
      # comments can go on any indent level - etc...
      if not flag:
      break[/CODE]
      That is, if I'm reading the C part correctly. Close, anyway
      Anything that has a non-zero value will evaluate as True.
      bool is a subclass of int so you can do many things with python bools that other languages wouldn't dream of doing (including list indexing):[CODE=python]timeToStop = True
      print ['still going', 'stopping'][timeToStop][/CODE]Kind of nifty, huh?

      Comment

      • Smygis
        New Member
        • Jun 2007
        • 126

        #4
        Originally posted by vajratkarviraj
        question from a newbie:
        can somebody help me in converting this c++ code to python...
        [CODE=c]
        bool flag;
        do{
        flag=0;
        for(int i=0;i<n;i++){
        double s1=xold[i];
        double s2=xnew[i];
        e[i]= s1-s2;
        if (e[i]<0) {e[i]=0-e[i];}
        if(e[i]>prec){flag=1 ;}

        for(int i=0;i<n;i++){
        xold[i]=xnew[i];
        }

        }
        }while (flag);
        [/CODE]
        basically the bool thing is a prob... also if i hav created a function(say solve(parameter s)) within a function(say display(paramet ers)).... then the python code goes sumwat like this:
        Code:
        def display(parameter1, parameter2):
        
        {code of display}
        
        solve(parameter3, parameter4):
        
        {code of solve and it gives me output its output in the form of a number}
        
        #now i want to return control to display
        
        {remaining code of display continues...}
        First of, Only becouse C++ lets you make your code 100% unreadable does not mean you shuld do it.

        [code=cpp]
        bool flag;
        do{
        flag = 0;
        for(int i = 0; i < n; i++)
        {
        double s1 = xold[i];
        double s2 = xnew[i];

        e[i] = s1 - s2;

        if (e[i] < 0)
        e[i] = 0 - e[i];

        if(e[i] > prec)
        flag = 1;

        for(int i = 0; i < n; i++)
        xold[i] = xnew[i];
        }
        }while(flag);[/code]

        And as for your second Q,
        [code=python]
        def solver(arg1, arg2):
        return arg1 ** arg2 # Ofcource you will replace this with whatever you want it to do

        def display(no1, no2):
        # Code
        solvednum = solver(no2, no1)
        # more code
        [/code]

        Simply create multiple functions.

        Comment

        Working...