[Tutor] need help with a script..

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] need help with a script..

Khalid Al-Ghamdi
Hi All,

I'm using python 3.2 on a windows xp.

I wrote the below script and ran it with the hope of returning a list of proctors (list_proc), but when it runs it  doesn't call the function convert_proctors() as intended. On the other hand, when i import the module from the IDLE prompt and call the convert_proctors() function, the function returns the desired list.

Why is this so?

Thanks

  1. import csv
  2.  
  3. proctor_file=r'c:\Python32\Khalid Stuff\Testing_Scheduler\proctors.csv'
  4.  
  5.  
  6. def convert_proctors():
  7.     proctor_csv_reader = csv.reader(open(proctor_file))
  8.     proctor_list=list(proctor_csv_reader)
  9.     list_proc=[]
  10.     for row in range(len(proctor_list)):
  11.         list_proc.append(proctor_list[row][0])
  12.     return (list_proc)
  13.  
  14.  
  15. convert_proctors()

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] need help with a script..

Jerry Hill
On Wed, Apr 11, 2012 at 9:50 AM, Khalid Al-Ghamdi <[hidden email]> wrote:

> Hi All,
>
> I'm using python 3.2 on a windows xp.
>
> I wrote the below script and ran it with the hope of returning a list of
> proctors (list_proc), but when it runs it  doesn't call the function
> convert_proctors() as intended. On the other hand, when i import the module
> from the IDLE prompt and call the convert_proctors() function, the function
> returns the desired list.
>
> Why is this so?

When you run that code as a script, it does call your
convert_proctors() function.  But since all you do is create a list,
and you never save it to disk, or print it to the screen, or even
assign it to a variable, the result is just thrown away.  Try changing
the last line to:

print(convert_proctors())

and you'll see the results you were expecting.

--
Jerry
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] need help with a script..

Christian Witts
In reply to this post by Khalid Al-Ghamdi
On 2012/04/11 03:50 PM, Khalid Al-Ghamdi wrote:
Hi All,

I'm using python 3.2 on a windows xp.

I wrote the below script and ran it with the hope of returning a list of proctors (list_proc), but when it runs it  doesn't call the function convert_proctors() as intended. On the other hand, when i import the module from the IDLE prompt and call the convert_proctors() function, the function returns the desired list.

Why is this so?

Thanks

  1. import csv
  2.  
  3. proctor_file=r'c:\Python32\Khalid Stuff\Testing_Scheduler\proctors.csv'
  4.  
  5.  
  6. def convert_proctors():
  7.     proctor_csv_reader = csv.reader(open(proctor_file))
  8.     proctor_list=list(proctor_csv_reader)
  9.     list_proc=[]
  10.     for row in range(len(proctor_list)):
  11.         list_proc.append(proctor_list[row][0])
  12.     return (list_proc)
  13.  
  14.  
  15. convert_proctors()


_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
convert_proctors() will get called when you run the application from say the command line, but because there's no explicit printing of the resulting list it will never get displayed to your console. Whereas when you run it from IDLE it will implicitly print the return value of a function if you do not "save" the data to a variable.
--
Email Signature

Christian Witts
Python Developer


_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] need help with a script..

Khalid Al-Ghamdi
In reply to this post by Jerry Hill
got it.... thanks a lot

On Wed, Apr 11, 2012 at 4:59 PM, Jerry Hill <[hidden email]> wrote:
On Wed, Apr 11, 2012 at 9:50 AM, Khalid Al-Ghamdi <[hidden email]> wrote:
> Hi All,
>
> I'm using python 3.2 on a windows xp.
>
> I wrote the below script and ran it with the hope of returning a list of
> proctors (list_proc), but when it runs it  doesn't call the function
> convert_proctors() as intended. On the other hand, when i import the module
> from the IDLE prompt and call the convert_proctors() function, the function
> returns the desired list.
>
> Why is this so?

When you run that code as a script, it does call your
convert_proctors() function.  But since all you do is create a list,
and you never save it to disk, or print it to the screen, or even
assign it to a variable, the result is just thrown away.  Try changing
the last line to:

print(convert_proctors())

and you'll see the results you were expecting.

--
Jerry
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] need help with a script..

Mark Lawrence
In reply to this post by Khalid Al-Ghamdi
On 11/04/2012 14:50, Khalid Al-Ghamdi wrote:

> Hi All,
>
> I'm using python 3.2 on a windows xp.
>
> I wrote the below script and ran it with the hope of returning a list of
> proctors (list_proc), but when it runs it  doesn't call the function
> convert_proctors() as intended. On the other hand, when i import the module
> from the IDLE prompt and call the convert_proctors() function, the function
> returns the desired list.
>
> Why is this so?
>
> Thanks
>
>
>     1. import csv
>     2.
>     3. proctor_file=r'c:\Python32\Khalid Stuff\Testing_Scheduler\p
>     roctors.csv'
>     4.
>     5.
>     6. def convert_proctors():
>     7.     proctor_csv_reader = csv.reader(open(proctor_file))
>     8.     proctor_list=list(proctor_csv_reader)
>     9.     list_proc=[]
>     10.     for row in range(len(proctor_list)):
>     11.         list_proc.append(proctor_list[row][0])
>     12.     return (list_proc)
>     13.
>     14.
>     15. convert_proctors()
>
>
>
>
> _______________________________________________
> Tutor maillist  -  [hidden email]
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

Your main query has already been answered, but I'd like to point out
that your function could be written something like this.

def convert_proctors():
     list_proc = []
     for row in csv.reader(open(proctor_file)):
         list_proc.append(row[0])
     return list_proc

--
Cheers.

Mark Lawrence.

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor