genNum function in Python was: Python question

  • From: "Richard Dinger" <rrdinger@xxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Tue, 24 Nov 2009 08:16:24 -0800

Edward,

You have some mistakes in your code. This is a simple assignment and can be solved several ways, but I am not certain what your approach is here. Can you explain in simple terms what approach you are trying to take?

If I know how you are trying to solve this problem, maybe I can help you figure it out. I suggest that you first look closely at how the range function works. Start by running:

help(range)

in the interactive shell in order to see what options you have with range.

Richard
----- Original Message ----- From: "edward" <personal.edward@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Monday, November 23, 2009 7:21 PM
Subject: RE: Python question


Hello
Thanks for your help. I have another question.
This code is supposed to take an integer, n and print it like the below.
Int is 5
1
12
123
1234
12345
Here is my very wrong code.

def genNum(n):
mat=[]
i=1
for i in range(n):
mat.append(range(i))
return mat

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Richard Dinger
Sent: Monday, November 23, 2009 5:25 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: Python question

Edward,

A few notes about your problem:

1. The signature for your isAfter method should look something like this:
def isAfter(self, other):
then inside the body of the method you can refer to the attributes of each
object like this:
 if self.hour > other.hour:
and so forth to make whatever comparisons you like.

2. Outside the class then, you call your isAfter function something like
this for two objects named t1 and t2:

 t1.isAfter(t2)

3. Your problem of comparing may be simplified if you pick a better
abstraction for your time. Consider keeping the hour, minute and second in
a container object such as a tuple, which can be compared directly.

4. You can also format your string representation directly from a tuple
using the string formatting operator.
 "the time is %02d:%02d:%02d" % hr_min_sec_tuple

5. In your initialization (constructor) method, you do not check for valid
input.  As a result negative or too large values may be entered.  Consider
using an assertion to control input value errors:
 assert hour >= 0
 assert hours <= 24
 etc.
the assertion will halt execution if an error occurs.

6. Finally, you can overload the "<" and ">" operators in your class and
then compare MakeTime objects directly such as:
 if t1 > t2:

Richard
----- Original Message -----
From: edward <mailto:personal.edward@xxxxxxxxx>
To: programmingblind@xxxxxxxxxxxxx
Sent: Saturday, November 21, 2009 4:48 PM
Subject: Python question


hello all
I am trying to check if one time comes immediately after another.  I
wrote a method called isAfter. the problem is how do I use it. meaning to use a method in python the syntax is x.isAfter but I have two variables not
one.  so I need to x's.

edward
code starts
class WhatTime:
def __init__(self, hour,minute,second):
  self.second=second
  self.minute=minute
  self.hour=hour

def isAfter(self,t1,t2):
  check=t2-t1
  return 1 in check
def __str__(self):
  return "the time is: "+str(self.hour)+":"+str(self.minute)+"
"+str(self.second)

t=WhatTime(10,25,43)
print t
t1=WhatTime(10,12,13)
t2=WhatTime(10,12,14)
isAfter



__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind



__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: