[Tutor] Newbie string & File I/O questions......

Alan Gauld alan.gauld at btinternet.com
Mon Nov 27 00:52:39 CET 2006


"Moedeloos Overste" <kloosterjunkie at hotmail.com> wrote

> This is what I've been able to come up with so far:
>

> vDraws = input("How many times do you want to draw the lottery? :>")

input is considered a damngerous function because a
user could input malicious python code. Its better to use
raw_input and convert to an integer or float etc.

> while vDraws > 0:
>    List_LotNumbers = random.sample(range(0,46), 6) #random numbers 
> from
> range into list
>    output_string = str(List_LotNumbers) #converting list to string 
> to store

You probably want to use the join string method here rather
than str(). str just produces a string representation of your list,
which includes the []. Join will produce a string from the members
of a list

Try

>>> help(''.join)

> vView = raw_input("Do want to see the results? y/n :>")
>
> if vView == "y" or vView == "Y":

You might prefer to use 'in' here:

if vView[0] in 'yY'

will work for y, Y, yes, Yes (and yesterday too in case
thats a problem!)

> My question is: how do I write the output of each lottery drawing on 
> a
> separate line

Previous poster answered that

> second question: how do i print the lottery drawings to the screen 
> without
> the brackets[]

Use join instead of str

> third:can somebody point me in the right direction as to how do I 
> make the
> program read from the file and count how many times a number has 
> been drawn.

Try using a dictionary with each number drawn as a key and
the corresponding value being the count.

Alan G. 




More information about the Tutor mailing list