473,398 Members | 2,427 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,398 software developers and data experts.

i need some help

I am a beginner in python language and I have the following question:

i am trying to define something, let say X, under certain limits and I am not sure how to do that.

basically i want to do the following: if X is less than 200 and greater than 100, then continue, else stop.

can someone provide some kind of starting point. i know i have to do a for loop, but not exactly sure how to go about it.

thanks in advance
Feb 19 '08 #1
14 1392
bvdet
2,851 Expert Mod 2GB
I am a beginner in python language and I have the following question:

i am trying to define something, let say X, under certain limits and I am not sure how to do that.

basically i want to do the following: if X is less than 200 and greater than 100, then continue, else stop.

can someone provide some kind of starting point. i know i have to do a for loop, but not exactly sure how to go about it.

thanks in advance
You don't need a for loop. Test for a condition with an if statement. If condition is met, do something. Otherwise, do something else.
Expand|Select|Wrap|Line Numbers
  1. >>> x = 50
  2. >>> if 100 < x < 200:
  3. ...     print 'In range'
  4. ... else:
  5. ...     print 'Out of range'
  6. ...     
  7. Out of range
  8. >>> 
Feb 19 '08 #2
You don't need a for loop. Test for a condition with an if statement. If condition is met, do something. Otherwise, do something else.
Expand|Select|Wrap|Line Numbers
  1. >>> x = 50
  2. >>> if 100 < x < 200:
  3. ...     print 'In range'
  4. ... else:
  5. ...     print 'Out of range'
  6. ...     
  7. Out of range
  8. >>> 
sorry, i should have mentioned the whole thing because I am still unsure.
My x here is actually a list of values. And i have 2 lists of values that correspond to an id and I am trying to define parameters using the two lists. i just don't know the proper notation and such to write the code:
i am trying the following with no sucess:

>>> x = fields [1] -- my list
>>> y = filelds [2] -- 2nd list
>>> if 100 <= x <= 200
>>>>> continue
>>>>>>> if 5 <= y <= 8
>>>>>>> print id (print the id that has the defined parameters)

Thanks again for any help
Feb 21 '08 #3
bvdet
2,851 Expert Mod 2GB
sorry, i should have mentioned the whole thing because I am still unsure.
My x here is actually a list of values. And i have 2 lists of values that correspond to an id and I am trying to define parameters using the two lists. i just don't know the proper notation and such to write the code:
i am trying the following with no sucess:

>>> x = fields [1] -- my list
>>> y = filelds [2] -- 2nd list
>>> if 100 <= x <= 200
>>>>> continue
>>>>>>> if 5 <= y <= 8
>>>>>>> print id (print the id that has the defined parameters)

Thanks again for any help
You will need a for loop after all. I take it that fields is a list of lists. Maybe something like the following?
Expand|Select|Wrap|Line Numbers
  1. xList = fields[1]
  2. yList = fields[2]
  3.  
  4. for i, item in enumerate(xList):
  5.     if 100 <= item <= 200:
  6.         if 5 <= yList[i] <= 8:
  7.             print id
Feb 21 '08 #4
You will need a for loop after all. I take it that fields is a list of lists. Maybe something like the following?
Expand|Select|Wrap|Line Numbers
  1. xList = fields[1]
  2. yList = fields[2]
  3.  
  4. for i, item in enumerate(xList):
  5.     if 100 <= item <= 200:
  6.         if 5 <= yList[i] <= 8:
  7.             print id

I tried this but it didn't work. Actually, my ids = fields [0] (which correspond to fields [1] and fields [2] that I am using for defining parameters), so will that change anything? Also, this code runs and selects the id with the defined parameters. What if I had many different parameters? Will they just have thier own blocks in the code?

thanks again.
Feb 22 '08 #5
bvdet
2,851 Expert Mod 2GB
I tried this but it didn't work. Actually, my ids = fields [0] (which correspond to fields [1] and fields [2] that I am using for defining parameters), so will that change anything? Also, this code runs and selects the id with the defined parameters. What if I had many different parameters? Will they just have thier own blocks in the code?

thanks again.
You can create a function whose arguments are the parameters. I don't understand your data. If you would post a representative sample, it may help.
Feb 22 '08 #6
You can create a function whose arguments are the parameters. I don't understand your data. If you would post a representative sample, it may help.
Here is an example that reflects what I am doing:
Id = fields [0]
Height = fields [1]
Length = fields [2]
Width = fields [3]

Then I have the parameters:
TypeX = (height range, length range, width range) ~ general

For example;
Type1 = (1.1 - 2.1, 3.2 - 5.3, .2 - .5)
Type2 = (1.0 - 2.0 , 2.3 - 4.2 , 1.2 - 2.8)

I want to write a code (but not sure how), so that it runs through my file which has the list (id, Height, Length, Width) and spits out how many Type 1 I have and Type 2, etc. I know that I need a for loop, because the code will first have to see if the height range matches, then if the length range matches, and then the width range. If everything matches, the id should be the output. Would you use
Expand|Select|Wrap|Line Numbers
  1. else
statement when lets say the height range doesn't match for Type 1, but then you want to see if it matches Type2.
I hope this explains little better of what I am trying to achieve. Again, I appreciate your help.
Feb 27 '08 #7
dshimer
136 Expert 100+
If it is just an external file of values in some kind of table, you may be getting too complicated with the data structure on the way in, would it be easier to just read in a line then check it's values on the way through the file. Along with bvdet's idea of a function to check the ranges. If I have a file that has id, height, width, length and looks something like.
a 1 2 3
b 3 4 5
c 5 6 7
reading and parsing the data could be as simple as
Expand|Select|Wrap|Line Numbers
  1. f=open('/tmp/test.txt','r')
  2. for line in f.readlines():
  3.     id,height,width,length=line.split()
  4.     print id,float(height)
  5. a 1.0
  6. b 3.0
  7. c 5.0
  8.  
Then in place of the print line a function could check whichever value you want and return the "type".
Feb 27 '08 #8
jlm699
314 100+
Expand|Select|Wrap|Line Numbers
  1. objs = []
  2. objs.append(['a', 1.0, 2.5, 0.3])  # Invalid
  3. objs.append(['b', 1.2, 5.0, 0.4])  # Type 1
  4. objs.append(['c', 1.2, 4.0, 1.4])  # Type 2
  5. objs.append(['d', 1.2, 4.5, 2.1])  # Invalid
  6.  
  7. for field in objs:
  8.     if (1.1 <= field[1] <= 2.1) and \
  9.         (3.2 <= field[2] <= 5.3) and \
  10.         (0.2 <= field[3] <= 0.5):
  11.         print field[0] + ' is type 1'
  12.     elif (1.0 <= field[1] <= 2.0) and \
  13.         (2.3 <= field[2] <= 4.2) and \
  14.         (1.2 <= field[3] <= 2.8):
  15.         print field[0] + ' is type 2'
  16.  
  17. b is type 1
  18. c is type 2
  19.  
Feb 27 '08 #9
bvdet
2,851 Expert Mod 2GB
You could create a series of comparison lists mapped to the type:
Expand|Select|Wrap|Line Numbers
  1. rangeDict = {'Type 1':([1,2],[3,4],[5,6]), 'Type 2': ([3,4],[5,6],[7,8]), 'Type 3': ([4,5],[6,7],[8,9])}
  2. dataList = [[1.4,3.8,5.2], [4.1,6,9], [4,5.5,7.9], [3,4,6]]
  3.  
  4. resultList = []
  5. for j, item in enumerate(dataList):
  6.     s = 'dataList item %d type is unknown' % j
  7.     for key in rangeDict:
  8.         for i, elem in enumerate(rangeDict[key]):
  9.             comp_list = [elem[0] <= item[i] <= elem[1] for i, elem in enumerate(rangeDict[key])]
  10.             if False not in comp_list:
  11.                 s = 'dataList item %d is %s' % (j, key)
  12.                 break
  13.     resultList.append(s)
  14.  
  15. print '\n'.join(resultList)
Output:

>>> dataList item 0 is Type 1
dataList item 1 is Type 3
dataList item 2 is Type 2
dataList item 3 type is unknown
>>>
Feb 27 '08 #10
Expand|Select|Wrap|Line Numbers
  1. objs = []
  2. objs.append(['a', 1.0, 2.5, 0.3])  # Invalid
  3. objs.append(['b', 1.2, 5.0, 0.4])  # Type 1
  4. objs.append(['c', 1.2, 4.0, 1.4])  # Type 2
  5. objs.append(['d', 1.2, 4.5, 2.1])  # Invalid
  6.  
  7. for field in objs:
  8.     if (1.1 <= field[1] <= 2.1) and \
  9.         (3.2 <= field[2] <= 5.3) and \
  10.         (0.2 <= field[3] <= 0.5):
  11.         print field[0] + ' is type 1'
  12.     elif (1.0 <= field[1] <= 2.0) and \
  13.         (2.3 <= field[2] <= 4.2) and \
  14.         (1.2 <= field[3] <= 2.8):
  15.         print field[0] + ' is type 2'
  16.  
  17. b is type 1
  18. c is type 2
  19.  
Since I am a beginner, your code makes the most sense to me. But I am confused about the object.append in the beginning. What is exactly a, b, c, and d and where did it come from. Is a, for example an id, followed by height, length, range? But in my file (that the code runs through), I have thousand of ids with their corresponding info (height, length, width) and the info is in ranges (from x1 - x2) like in my parameters (Type 1, Type 2), not specific numbers. So would it be something like
Expand|Select|Wrap|Line Numbers
  1. Object.append (['a', 1.0-2.5 , 2.5-5 , .3 -.6])
Again, thanks to anyone that helps.
Feb 28 '08 #11
jlm699
314 100+
Expand|Select|Wrap|Line Numbers
  1. objs = []              
  2. # Elements of this list will be in the format:
  3.      #  [ID, Height, Width, Depth]
  4. objs.append(['a', 1.0, 2.5, 0.3])  
  5. objs.append(['b', 1.2, 5.0, 0.4])  
  6. objs.append(['c', 1.2, 4.0, 1.4])  
  7. objs.append(['d', 1.2, 4.5, 2.1])  
  8.  
  9. for field in objs:
  10. # This is the range for height (1.1 ~ 2.1)
  11.     if (1.1 <= field[1] <= 2.1) and \  
  12. # This is the range for width  (3.2 ~ 5.3)
  13.         (3.2 <= field[2] <= 5.3) and \ 
  14. # This is the range for depth  (0.2 ~ 0.5)
  15.         (0.2 <= field[3] <= 0.5):        
  16. # If the input passes the above constraints it must by type 1
  17.         print field[0] + ' is type 1'      
  18.     elif (1.0 <= field[1] <= 2.0) and \ # (1.0 ~ 2.0)
  19.         (2.3 <= field[2] <= 4.2) and \  # (2.3 ~ 4.2)
  20.         (1.2 <= field[3] <= 2.8):         # (1.2 ~ 2.8)
  21. # Passing the above constraints makes it type 2
  22.         print field[0] + ' is type 2'        
  23.  
I hope that makes it a little bit clearer. I was just making the list of objects a random sample of possible inputs. The ranges that you described as type 1, type 2 are the constraints in the if, else if structure.
Feb 28 '08 #12
Expand|Select|Wrap|Line Numbers
  1. objs = []              
  2. # Elements of this list will be in the format:
  3.      #  [ID, Height, Width, Depth]
  4. objs.append(['a', 1.0, 2.5, 0.3])  
  5. objs.append(['b', 1.2, 5.0, 0.4])  
  6. objs.append(['c', 1.2, 4.0, 1.4])  
  7. objs.append(['d', 1.2, 4.5, 2.1])  
  8.  
  9. for field in objs:
  10. # This is the range for height (1.1 ~ 2.1)
  11.     if (1.1 <= field[1] <= 2.1) and \  
  12. # This is the range for width  (3.2 ~ 5.3)
  13.         (3.2 <= field[2] <= 5.3) and \ 
  14. # This is the range for depth  (0.2 ~ 0.5)
  15.         (0.2 <= field[3] <= 0.5):        
  16. # If the input passes the above constraints it must by type 1
  17.         print field[0] + ' is type 1'      
  18.     elif (1.0 <= field[1] <= 2.0) and \ # (1.0 ~ 2.0)
  19.         (2.3 <= field[2] <= 4.2) and \  # (2.3 ~ 4.2)
  20.         (1.2 <= field[3] <= 2.8):         # (1.2 ~ 2.8)
  21. # Passing the above constraints makes it type 2
  22.         print field[0] + ' is type 2'        
  23.  
I hope that makes it a little bit clearer. I was just making the list of objects a random sample of possible inputs. The ranges that you described as type 1, type 2 are the constraints in the if, else if structure.
Thanks, this makes sense. Problem is nothing is outputting. I am using elif statements over many times, like following. I don't know if this is the problem or indentation, or something else.
[HTML]elif (5 <= Height <= 6) and \
(1 <= Length <= 2) and \
(9 <= Width <= 5):
print fields[0] + ' Type1'
elif (8 <= Height <= 9) and \
(2 <= Length <= 5) and \
(10 <= Width <= 12):
print fields[0] + ' Type2'
elif (11 <= Height <= 16) and \
(3 <= Length <= 4) and \
(11 <= Width <= 16):
print fields[0] + ' Type3'[/HTML]
Mar 1 '08 #13
bvdet
2,851 Expert Mod 2GB
Thanks, this makes sense. Problem is nothing is outputting. I am using elif statements over many times, like following. I don't know if this is the problem or indentation, or something else.
Expand|Select|Wrap|Line Numbers
  1. elif (5 <= Height <= 6) and \
  2.      (1 <= Length <= 2) and \
  3.      (9 <= Width <= 5):
  4.     print fields[0] + ' Type1'
  5. elif (8 <= Height <= 9) and \
  6.      (2 <= Length <= 5) and \
  7.      (10 <= Width <= 12):
  8.     print fields[0] + ' Type2'
  9. elif (11 <= Height <= 16) and \
  10.      (3 <= Length <= 4) and \
  11.      (11 <= Width <= 16):
  12.     print fields[0] + ' Type3'
You can never have a 'Width' that is greater than 9 and less than 5. See 'Type1'. Please use code tags. Your indentation was messed up, but you would probably get an error message when you run the code. The code will work when corrected. Following is suggested code using a dictionary.
Expand|Select|Wrap|Line Numbers
  1. Height = 5.5
  2. Length = 1.5
  3. Width = 8
  4.  
  5. rangeDict = {'Type 1':((5,6),(1,2),(5,9)),
  6.              'Type 2': ((8,9),(2,5),(10,12)),
  7.              'Type 3': ((11,16),(3,4),(11,16))
  8.              }
  9. for key in rangeDict:
  10.     if False not in [a[0] <= b <= a[1] for a,b in zip(rangeDict[key], [Height, Length, Width])]:
  11.         print "Object %s" % key
  12.  
  13. >>> Object Type 1
  14. >>> 
You should be able to tell I like dictionaries.
Mar 1 '08 #14
raubana
56
this is very simple. What you do is make a ' while loop', or a loop that contenues to work untill a condition is false. You make this loop change variables untill something is false, liek this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. x = 150
  3. #this is your variable. you can change it if you want to.
  4. while x >100 and x < 200:
  5.      # here you would put code that would change variables untill the 
  6.      #condition "x>100 and x<200" is false
  7.  
now a 'for loop' is used the same way as a while loop, but is made
specipicly for a list. it's like you take out some cards and place
them on a table. You take a pencil then and point at each card one at
a time. then you put the pencil away when your done.

here's an example:
Expand|Select|Wrap|Line Numbers
  1. x = [1,2,3]
  2. #don't use ' x=(1,2,3) ' unless you don't want to change the list, 
  3. #because a list with () instead of [] can't be changed.
  4. for a in x:
  5.      # 'a' can be changed, but you can't get the index (the spot in the list).
  6.      # this will go through each part of the list.
  7.  
you can also have list's inside lists, so you could make a for loop in a for
loop as well!

Expand|Select|Wrap|Line Numbers
  1. x=[[1,2,3],[4,5,6],[7,8,9]]
  2.  
  3. for a in x:
  4.     for b in a:
  5.         #note: i used a different variable name for the objects in the list because
  6.         # not doing so can mess up the loop!
  7.  
  8.  
...and that's how you use a for loop and a while loop!
Mar 2 '08 #15

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

Similar topics

6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
5
by: John Flynn | last post by:
hi all i'm going to be quick i have an assignment due which i have no idea how to do. i work full time so i dont have the time to learn it and its due date has crept up on me .. As follows:...
0
by: xunling | last post by:
i have a question about answering ..... this topic is "need help" what do i have to write at te topic line, !after i have klicked the "answer message" button ive tried many possibilities,...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
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?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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...
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.