473,396 Members | 2,108 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.

bools in python

question from a newbie:
can somebody help me in converting this c++ code to python...
Expand|Select|Wrap|Line Numbers
  1. bool flag;
  2. do{        
  3. flag=0;      
  4. for(int i=0;i<n;i++){
  5.         double s1=xold[i];
  6.         double s2=xnew[i];  
  7.         e[i]= s1-s2;
  8.         if (e[i]<0) {e[i]=0-e[i];}
  9.         if(e[i]>prec){flag=1;}
  10.  
  11. for(int i=0;i<n;i++){
  12.         xold[i]=xnew[i];
  13.         }
  14.  
  15. }
  16. }while (flag);
  17.  
basically the bool thing is a prob... also if i hav created a function(say solve(parameters)) within a function(say display(parameters)).... then the python code goes sumwat like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. def display(parameter1, parameter2):
  3.  
  4. {code of display}
  5.  
  6. solve(parameter3, parameter4):
  7.  
  8. {code of solve and it gives me output its output in the form of a number}
  9.  
  10. #now i want to return control to display
  11.  
  12. {remaining code of display continues...}
  13.  
Jul 11 '07 #1
3 1688
bartonc
6,596 Expert 4TB
question from a newbie:
can somebody help me in converting this c++ code to python...
Expand|Select|Wrap|Line Numbers
  1. bool flag;
  2. do{        
  3. flag=0;      
  4. for(int i=0;i<n;i++){
  5.         double s1=xold[i];
  6.         double s2=xnew[i];  
  7.         e[i]= s1-s2;
  8.         if (e[i]<0) {e[i]=0-e[i];}
  9.         if(e[i]>prec){flag=1;}
  10.  
  11. for(int i=0;i<n;i++){
  12.         xold[i]=xnew[i];
  13.         }
  14.  
  15. }
  16. }while (flag);
  17.  
<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:
Expand|Select|Wrap|Line Numbers
  1. n = 4 # counter for for()
  2. flag = 0
  3. while True:   # Note that python is indentation based
  4.     for i in range(n):  # use xrange() for huge numbers
  5.         s1 = xold[i]
  6.         s2 = xnew[i]
  7.         e[i] = s1 - s2
  8.         if e[i] < 0:   # colon starts a block, next de-dent ends it
  9.             e[i] = 0 - e[i]
  10.         if e[i] > prec:
  11.             flag=1
  12.        # comments can go on any indent level - etc...
  13.     if not flag:
  14.         break
That is, if I'm reading the C part correctly. Close, anyway
Jul 11 '07 #2
bartonc
6,596 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. n = 4 # counter for for()
  2. flag = 0
  3. while True:   # Note that python is indentation based
  4.     for i in range(n):  # use xrange() for huge numbers
  5.         s1 = xold[i]
  6.         s2 = xnew[i]
  7.         e[i] = s1 - s2
  8.         if e[i] < 0:   # colon starts a block, next de-dent ends it
  9.             e[i] = 0 - e[i]
  10.         if e[i] > prec:
  11.             flag=1
  12.        # comments can go on any indent level - etc...
  13.     if not flag:
  14.         break
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):
Expand|Select|Wrap|Line Numbers
  1. timeToStop = True
  2. print ['still going', 'stopping'][timeToStop]
Kind of nifty, huh?
Jul 11 '07 #3
Smygis
126 100+
question from a newbie:
can somebody help me in converting this c++ code to python...
Expand|Select|Wrap|Line Numbers
  1. bool flag;
  2. do{        
  3. flag=0;      
  4. for(int i=0;i<n;i++){
  5.         double s1=xold[i];
  6.         double s2=xnew[i];  
  7.         e[i]= s1-s2;
  8.         if (e[i]<0) {e[i]=0-e[i];}
  9.         if(e[i]>prec){flag=1;}
  10.  
  11. for(int i=0;i<n;i++){
  12.         xold[i]=xnew[i];
  13.         }
  14.  
  15. }
  16. }while (flag);
  17.  
basically the bool thing is a prob... also if i hav created a function(say solve(parameters)) within a function(say display(parameters)).... then the python code goes sumwat like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. def display(parameter1, parameter2):
  3.  
  4. {code of display}
  5.  
  6. solve(parameter3, parameter4):
  7.  
  8. {code of solve and it gives me output its output in the form of a number}
  9.  
  10. #now i want to return control to display
  11.  
  12. {remaining code of display continues...}
  13.  
First of, Only becouse C++ lets you make your code 100% unreadable does not mean you shuld do it.

Expand|Select|Wrap|Line Numbers
  1. bool flag;
  2. do{        
  3.     flag = 0;
  4.     for(int i = 0; i < n; i++)
  5.     {
  6.         double s1 = xold[i];
  7.         double s2 = xnew[i];
  8.  
  9.         e[i] = s1 - s2;
  10.  
  11.         if (e[i] < 0)
  12.             e[i] = 0 - e[i];
  13.  
  14.         if(e[i] > prec)
  15.             flag = 1;
  16.  
  17.         for(int i = 0; i < n; i++)
  18.             xold[i] = xnew[i];
  19.     }
  20. }while(flag);
And as for your second Q,
Expand|Select|Wrap|Line Numbers
  1. def solver(arg1, arg2):
  2.     return arg1 ** arg2 # Ofcource you will replace this with whatever you want it to do
  3.  
  4. def display(no1, no2):
  5.     # Code
  6.     solvednum = solver(no2, no1)
  7.     # more code
  8.  
Simply create multiple functions.
Jul 11 '07 #4

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

Similar topics

1
by: Emile van Sebille | last post by:
QOTW: "If we get 2.3.3c1 out in early December, we could release 2.3.3 final before the end of the year, and start 2004 with a 100% bug-free codebase <wink>." -- Tim Peters "cjOr proWe vbCould...
32
by: Joe Rattz | last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to intialize them to false. bool bits = new bool; foreach(bool bit in bits) { bit = false; } The compiler complains...
5
by: Randeh | last post by:
Short story: in a beginning C++ class in college, was in a car accident that caused central spinal stenosis, some new Schmorl's nodes, disc problems and an incredible amount of pain. So far I've...
17
by: fgh.vbn.rty | last post by:
I know that std::vector<boolis specialized to store individual bools as single bits. I have a question about the comparison operation on a pair of vector<bool>s. Is the comparison done bit by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.