[Tutor] Newbie string & File I/O questions......
Hi everybody, I am new to python and trying to write a lottery program for a schoolproject. The program is supposed to draw the lottery any entered(user input) number of times, then store the drawing results in a file. After drawings the user should see (if he wants to) how many times each of the possible numbers was picked by the computer and which were the six most succesful numbers. These data must be calculated from the data stored in the file! This is what I've been able to come up with so far: # Lottery chances. # Programs ask how many times you want to have the lottery drawn by the # computer. Then stores these draws and then shows the 6 numbers most drawn # by the computer. # Importing for making the program nicer later on... import os, sys import time import random vDraws = input("How many times do you want to draw the lottery? :>") # Draw lottery numbers & writing them to file 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 it. fout = open("draw__output.dat", "a") fout.write(output_string) #writing string to file fout.close() vDraws = vDraws - 1 # Printing all numbers drawn vView = raw_input("Do want to see the results? y/n :>") if vView == "y" or vView == "Y": fout = open("draw__output.dat", "r") Print_list = fout.readline() print Print_list #end of program so far My question is: how do I write the output of each lottery drawing on a separate line second question: how do i print the lottery drawings to the screen without the brackets[] 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. Any help or pointers would greatly be appreciated. thanks, Robert (forgive me my english, I am Dutch) _ Windows Live Mail: Kies je eigen kleur, indeling en contacten! http://imagine-windowslive.com/mail/launch/default.aspx?Locale=nl-nl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Newbie question: join() string method
Hi, I'm trying to use the join() method on a list to be able to store the contents of that list in a file. Basically I want the contents of the list stored without the []. I've tried numerous ways of using the join() method, but they all return errors. I've tried using the tutorial and documentation on this one but still can't work it out. errors: TypeError: sequence item 0: expected string, int found code: LotNumbers = random.sample(range(1,45), 6) #random numbers from range into list) ','.join() #?what's the correct syntax? fout = open("draw__output.dat", "a") fout.write(LotNumbers) #writing string to file fout.close() vDraws = vDraws - 1 Can someone show me the correct way to use the join() method in this case? Many thanks, Robert _ De makers van Hotmail hebben groot nieuws! Meer weten? Klik hier! http://imagine-windowslive.com/mail/launch/default.aspx?Locale=nl-nl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newbie question: join() string method
Ha! Thanx for all the input everybody. I finally got it to work. The contents of the list is now stored in the file without the []. The solution is in the code below. Next step in my learning process is reading the file contents and storing it in a dictionary. One question: When I run the program from IDLE it writes the data to file but when I run it from the command prompt(win32) it doesn't. why is this? code: vDraws = input("How many times do you want to draw the lottery? :>") # Draw lottery numbers & writing them to file while vDraws > 0: LotNumbers = random.sample(range(1,45), 6) #random numbers from range into list) strgOutput=",".join(str(i) for i in LotNumbers) #??converting list to string to store it. fout = open("draw__output.dat", "a") fout.write(strgOutput + "\n") #writing string to file fout.close() vDraws = vDraws - 1 >From: "Andreas Kostyrka" <[EMAIL PROTECTED]> >Reply-To: "Andreas Kostyrka" <[EMAIL PROTECTED]> >To: "Moedeloos Overste" <[EMAIL PROTECTED]> >CC: tutor@python.org >Subject: Re: [Tutor] Newbie question: join() string method >Date: Mon, 27 Nov 2006 13:32:59 +0100 > >",".join(str(x) for x in intList) > >Andreas > >_ Ursprüngliche Mitteilung _ >Betreff: [Tutor] Newbie question: join() string method >Autor: "Moedeloos Overste" <[EMAIL PROTECTED]> >Datum: 27. November 2006 13:20:41 > >Hi, > >I'm trying to use the join() method on a list to be able to store the >contents of that list in a file. Basically I want the contents of the list >stored without the []. I've tried numerous ways of using the join() method, >but they all return errors. I've tried using the tutorial and documentation >on this one but still can't work it out. > >errors: TypeError: sequence item 0: expected string, int found > > > >code: >LotNumbers = random.sample(range(1,45), 6) #random numbers from range into >list) > ','.join() #?what's >the >correct syntax? > fout = open("draw__output.dat", "a") > fout.write(LotNumbers) #writing string to >file > fout.close() > vDraws = vDraws - 1 > >Can someone show me the correct way to use the join() method in this case? > >Many thanks, > >Robert > >_ >De makers van Hotmail hebben groot nieuws! Meer weten? Klik hier! >http://imagine-windowslive.com/mail/launch/default.aspx?Locale=nl-nl > >___ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor _ Windows Live Mail: Slim, Persoonlijk, Betrouwbaar en het blijft natuurlijk gratis! http://imagine-windowslive.com/mail/launch/default.aspx?Locale=nl-nl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] ashamed
I shamefully bowe my head. How stupid of me not to think of that. I assumed that as the script is in a certain directory the output would also be in that directory. A very humbling learning experience Thank you for the pointer on file open/close outside of the loop. That should speed things up Right now I'm kept busy by dictionaries. I've created a dictionary containing all possible keys(lottery numbers) with their values set to zero. Now I want read the file and for each time a key is in the file i want it's value to go up +1. Like a counter. So far I've not been succesfull:-) see code. d={1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0, 10:0, 11:0, 12:0, 13:0, 14:0, 15:0, 16:0, 17:0, 18:0, 19:0, 20:0, 21:0, 22:0, 23:0, 24:0, 25:0, 26:0, 27:0, 28:0, 29:0, 30:0, 31:0, 32:0, 33:0, 34:0, 35:0, 36:0, 37:0, 38:0, 39:0, 40:0, 41:0, 42:0, 43:0, 44:0, 45:0} done=0 fd = open("draw__output.txt",'r') while not done: line = fd.readline() if line == '': done = 1 else: for i in line: d[i] = int(d[i])+ 1 >From: "Alan Gauld" <[EMAIL PROTECTED]> >To: tutor@python.org >Subject: Re: [Tutor] Newbie question: join() string method >Date: Mon, 27 Nov 2006 18:46:04 - > >"Moedeloos Overste" <[EMAIL PROTECTED]> wrote > > > One question: When I run the program from IDLE it writes the data to > > file > > but when I run it from the command prompt(win32) it doesn't. why is > > this? > >How do you know? Have you searched for the file? >Or are you looking in the same file that IDLE produced? >I would expect the code to create a new file in the current >directory - wherever you started the program. Did you look >there? > >--- >while vDraws > 0: > LotNumbers = random.sample(range(1,45), 6) #random numbers from >range >into list) > strgOutput=",".join(str(i) for i in LotNumbers) >#??converting list >to string to store it. > fout = open("draw__output.dat", "a") > fout.write(strgOutput + "\n") #writing string to file > fout.close() >-- > >It's probably better to put the open/close outside the loop > >fout = open(...,'w') >while vDraws > 0 > ... > fout.write(strgOutput + '\n') > vDraws -= 1 >fout.close() > >That way you can use 'w' to write the file(unless you really want the >previous runs to be in there too.) Also this will save the program >opening and closing the file for each line which is quite a slow >process. > >HTH, > > >-- >Alan Gauld >Author of the Learn to Program web site >http://www.freenetpages.co.uk/hp/alan.gauld > > >___ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor _ Windows Live Mail: Slim, Persoonlijk, Betrouwbaar en het blijft natuurlijk gratis! http://imagine-windowslive.com/mail/launch/default.aspx?Locale=nl-nl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Newbie question: random.sample illusion?
Hi everybody, I'm in the process of learning Python and working my way through O'Reilly's "Learning Python". As an excercise I wrote (some copy/paste as well) a small lottery program just to learn how to work with lists and dictionarys etc. The user has to enter 6 numbers from a range(1-45). The numbers are stored in a list(num_list). Then the program asks the user how many times(vDraws) he wants to play the lottery with his numbers. After entering the program draws the lottery the desired number of times and compares the winning numbers with the users numbers. Now, by accident I stumbled upon the following; if the user enters the numbers 1 to 6 as his lottery numbers he always wins! So somewhere I must have made a mistake. Could somebody have a look at the code please and tell me where I took a wrong turn? winnings=0 right2=1 right3=4 right4=15 right5=450 right6=100 user_nums=[] # empty List for numbers print "Kies je nummers tussen 0 en 46" print whereat=1 listindex=0 # when 7th number is inputted loop breaks while whereat <= 6: num=raw_input("Voer "+str(whereat)+". Lotto nummer in: ") # check that user inputted value is not smaller than 1 or bigger than 45 # and that it is not already in user_nums if num != "" and len(num) != 0: if int(num) >= 1 and int(num) <= 45 and num not in user_nums: user_nums.append(num) whereat+=1 listindex+=1 # if above statement if false just pass and ask for the number again! else: pass print '1 trekking kost U 1 euro inzet' print vDraws = input("Hoe vaak wil je in de lotto spelen met deze nummers? :>") # Create dictionary for statiscal purposes later on :-) d={1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0, 10:0, 11:0, 12:0, 13:0, 14:0, 15:0, 16:0, 17:0, 18:0, 19:0, 20:0, 21:0, 22:0, 23:0, 24:0, 25:0, 26:0, 27:0, 28:0, 29:0, 30:0, 31:0, 32:0, 33:0, 34:0, 35:0, 36:0, 37:0, 38:0, 39:0, 40:0, 41:0, 42:0, 43:0, 44:0, 45:0} match=0 count=0 inzet=vDraws * 1 while vDraws > 0: x=random.sample(range(1,46), 6)# draws lottery from given range for i in x: y=int(i) d[y] = int(d[y])+ 1 #stores values in lottonumbers dictionary for p in user_nums: count+=1 y=str(y) if p in y: match+=1 # increase matching variable by one if its right number # caculating winnings if match ==2: winnings+=right2 match=0 elif match ==3: winnings+=right3 match=0 elif match == 4: winnings+=right4 match=0 elif match == 5: winnings+=right5 match=0 elif match == 6: winnings+=right6 match=0 vDraws = vDraws - 1 # sorting dictionary by its values items=d.items() backitems=[ [v[1],v[0]] for v in items] backitems.sort() sortedlist=[ backitems[i][1] for i in range(0,len(backitems))] print print print saldo=winnings-inzet print print print '-' * 80 if saldo > 0: print 'U heeft', saldo, 'euro winst gemaakt!' else: print 'U heeft', saldo, ' euro verlies geleden!' print 'U heeft in totaal gewonnen', winnings, 'euro.' print 'Uw inzet was', inzet, 'euro' print 'De 6 meest gevallen getallen waren:', for x in sortedlist[0:6]: print x, #prints the 6 most drawn numbers _ Veilig & gerust mailen met de verbeterde antivirusscan van Live Mail! http://imagine-windowslive.com/mail/launch/default.aspx?Locale=nl-nl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newbie question: random.sample illusion?
Dear Luke, Wow, thank you for all the input.You really made an effort .It's always nice when someone is willing to share his knowledge with you. You really opened my eyes on some things. I guess it's gonna be a long night for me. But I'm really enjoying myself though :-) Regards, Robert >From: Luke Paireepinart <[EMAIL PROTECTED]> >To: Moedeloos Overste <[EMAIL PROTECTED]> >CC: tutor@python.org >Subject: Re: [Tutor] Newbie question: random.sample illusion? >Date: Thu, 07 Dec 2006 12:17:23 -0600 > >Moedeloos Overste wrote: >>Hi everybody, >> >>I'm in the process of learning Python and working my way through >>O'Reilly's "Learning Python". As an excercise I wrote (some copy/paste as >>well) a small lottery program just to learn how to work with lists and >>dictionarys etc. >> >>The user has to enter 6 numbers from a range(1-45). The numbers are stored >>in a list(num_list). Then the program asks the user how many times(vDraws) >>he wants to play the lottery with his numbers. After entering the program >>draws the lottery the desired number of times and compares the winning >>numbers with the users numbers. >> >>Now, by accident I stumbled upon the following; if the user enters the >>numbers 1 to 6 as his lottery numbers he always wins! So somewhere I must >>have made a mistake. Could somebody have a look at the code please and >>tell me where I took a wrong turn? >> >> >> >>winnings=0 >>right2=1 >>right3=4 >>right4=15 >>right5=450 >>right6=100 >> >it might be clearer to do >prizes = [0,0,1,4,15,450,100] >>user_nums=[] # empty List for numbers >>print "Kies je nummers tussen 0 en 46" >>print >>whereat=1 >>listindex=0 >> >What is the point of having two variables here? >You never use listindex. >You should start whereat at 0. >># when 7th number is inputted loop breaks >>while whereat <= 6: >> >And loop until whereat is less than 6. >That's the general consensus on looping in Computer Science. Starting at >0. >> num=raw_input("Voer "+str(whereat)+". Lotto nummer in: ") >> >And just change this line to str(whereat+1) instead. >> # check that user inputted value is not smaller than 1 or bigger than >>45 >> # and that it is not already in user_nums >> if num != "" and len(num) != 0: >> >If they enter 'a' your program will crash. >A better solution would be to use a try except block. >eg. try: > tmp = int(num) > if tmp >= 1 and tmp <= 45 and tmp not in user_nums: > user_nums.append(tmp) > whereat += 1 >except ValueError: >pass >> if int(num) >= 1 and int(num) <= 45 and num not in user_nums: >> user_nums.append(num) >> >Also note here that you're using the integer representation of num to >compare it to 1 and 45, >but then you're storing the string representation of it in user_nums. >This may be part of why you're having a problem. >> whereat+=1 >> listindex+=1 >> # if above statement if false just pass and ask for the >>number again! >> else: >> pass >>print '1 trekking kost U 1 euro inzet' >>print >>vDraws = input("Hoe vaak wil je in de lotto spelen met deze nummers? :>") >> >># Create dictionary for statiscal purposes later on :-) >>d={1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0, 10:0, 11:0, 12:0, 13:0, >>14:0, 15:0, >> 16:0, 17:0, 18:0, 19:0, 20:0, 21:0, 22:0, 23:0, 24:0, 25:0, 26:0, >>27:0, 28:0, >> 29:0, 30:0, 31:0, 32:0, 33:0, 34:0, 35:0, 36:0, 37:0, 38:0, 39:0, >>40:0, 41:0, 42:0, >> 43:0, 44:0, 45:0} >> >This could be: >d = {} >for x in range(1,46): >d[x] = 0 >Or several varations of this, using lambda & map, and such. >>match=0 >>count=0 >> >You don't use count in your program anywhere. >>inzet=vDraws * 1 >> >There's absolutely no reason to multiply something by 1. >>while vDraws > 0: >> x=random.sample(range(1,46), 6)# draws lottery from given range >> for i in x: >> y=int(i) >> >This line is unnecessary, 'i' is already an integer. >> d[y] = int(d[y])+ 1 #stores values in lottonumbers dictionary >> >Can be shortened to d[y] += 1 >> for p in user_nums: >>count+=1 >> >no reason to do this because the for loop wil