[Tutor] (no subject)
hello i need help with a program and i dont understand what is wrong it is a lottery ticket generator. the problem is: when the computers asks the user if he would like to go again and the user says yes, it asks for the number of lines and then if the user clicks 3 it will only give me one line. here is the code appreciate if you could help import random abz = 0 lines = int(input('How many lines would you like?')) loop = lines if lines >7: print('Too many lines saaxib') exit() else: print() while lines != 0: line1 = random.randint (1,7) line2 = random.randint (8,14) line3 = random.randint (15,21) line4 = random.randint (22,28) line5 = random.randint (29,35) line6 = random.randint (36,42) line7 = random.randint (43,49) lines = lines - 1 print(line1, line2, line3, line4, line5, line6,line7) while abz == 0: again = input('Would you like to go again?') if again == 'yes': lines = int(input('How many lines would you like?')) line1 = random.randint (1,7) line2 = random.randint (8,14) line3 = random.randint (15,21) line4 = random.randint (22,28) line5 = random.randint (29,35) line6 = random.randint (36,42) line7 = random.randint (43,49) lines = lines - 1 print(line1, line2, line3, line4, line5, line6,line7) if again == 'no': print('Okay the program is finished saaxib') exit() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] lottery problem (Was Re: (no subject))
On 18/12/14 21:10, Abdullahi Farah Mohamud wrote: hello i need help with a program and i dont understand what is wrong Please always add a meaningful subject line. when the computers asks the user if he would like to go again > and the user says yes, it asks for the number of lines > and then if the user clicks 3 it will only give me one line. import random abz = 0 lines = int(input('How many lines would you like?')) loop = lines if lines >7: You might want to check fpor negative numbers too or you will loop forever. print('Too many lines saaxib') exit() else: print() while lines != 0: line1 = random.randint (1,7) line2 = random.randint (8,14) line3 = random.randint (15,21) line4 = random.randint (22,28) line5 = random.randint (29,35) line6 = random.randint (36,42) line7 = random.randint (43,49) > lines = lines - 1 > print(line1, line2, line3, line4, line5, line6,line7) Down to here basically works. But... You could have used a list instead of all the individual variables line[0] = ... line[1] = ... But then you could get clever and use a loop: while lines != 0: start = 1 period = 7 for lineNum in range(7): line[lineNum] = random(start,period) start += period period += period print (*line) lines -=1 while abz == 0: again = input('Would you like to go again?') if again == 'yes': lines = int(input('How many lines would you like?')) line1 = random.randint (1,7) Notice this is outside the if statement so will execute regardless of the answer. But unlike the equivalent section above it is NOT in a while loop so will only execute once per question. Which is what you were seeing. line2 = random.randint (8,14) line3 = random.randint (15,21) line4 = random.randint (22,28) line5 = random.randint (29,35) line6 = random.randint (36,42) line7 = random.randint (43,49) lines = lines - 1 print(line1, line2, line3, line4, line5, line6,line7) Since you are duplicating code you could put it in a function - have you seen functions yet? def getLine(start=1, period=7): line = [] for lineNum in range(7): line[lineNum] = random(start,period) start += period period += period return line then the while loop becomes: while lines != 0: line = getLine() print(*line) lines -= 1 if again == 'no': print('Okay the program is finished saaxib') exit() HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
On Thu, Dec 18, 2014 at 09:10:40PM +, Abdullahi Farah Mohamud wrote: > hello i need help with a program and i dont understand what is wrong > it is a lottery ticket generator. > the problem is: > when the computers asks the user if he would like to go again and the user > says yes, it asks for the number of lines and then if the user clicks 3 it > will only give me one line. > here is the code > appreciate if you could help > > > import random > abz = 0 > lines = int(input('How many lines would you like?')) > loop = lines > if lines >7: > print('Too many lines saaxib') > exit() > else: > print() Here you loop over each lines. A "while loop" is not the best way to do this, a for loop would be much better. > while lines != 0: > line1 = random.randint (1,7) > line2 = random.randint (8,14) > line3 = random.randint (15,21) > line4 = random.randint (22,28) > line5 = random.randint (29,35) > line6 = random.randint (36,42) > line7 = random.randint (43,49) > lines = lines - 1 > print(line1, line2, line3, line4, line5, line6,line7) When you finish this while loop, you then start a brand new while loop: > while abz == 0: > again = input('Would you like to go again?') > if again == 'yes': > lines = int(input('How many lines would you like?')) All this does is repeatedly ask the user if they want to go again, over and over and over and over again, never stopping. It never stops because abz never gets changed: it starts with the value 0, and it stays with the value 0 forever. Here is how I would solve this problem in English. You can try translating it into Python code: start WHILE the user wants to play: ask how many numbers to pick FOR each of those numbers: print a random number print that number ask the user if they want to play again end Take note of the indentation: there is only one WHILE loop, and asking the user if they want to play again is *inside* that while loop, not outside it. Hope that this helps. Please try your best to change this to Python code, and ask for help if you need it. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] lottery problem (Was Re: (no subject))
On Fri, 19 Dec 2014 00:55:49 + Alan Gauld wrote: > You could have used a list instead of all the > individual variables > > line[0] = ... > line[1] = ... > > But then you could get clever and use a loop: > > while lines != 0: > start = 1 > period = 7 > for lineNum in range(7): > line[lineNum] = random(start,period) > start += period > period += period > print (*line) > lines -=1 > A list comprehension might be fun. https://docs.python.org/3.4/tutorial/datastructures.html#list-comprehensions For example: >>> [random.randint(x,x+6) for x in range(1,50,7)] [4, 9, 15, 27, 33, 36, 49] And to build the 'lines' list (although, this is getting rather ugly): >>> lines = [[random.randint(x,x+6) for x in range(1,50,7)] for i in range(7)] >>> lines [[2, 13, 18, 27, 35, 37, 47], [1, 11, 21, 24, 34, 37, 49], [7, 12, 16, 24, 29, 36, 44], [4, 9, 16, 22, 32, 37, 46], [2, 13, 20, 22, 29, 40, 46], [7, 14, 19, 26, 35, 42, 43], [4, 12, 16, 22, 34, 40, 46]] It might also be a good idea to execute random.seed() before calling randint() - https://docs.python.org/3.4/library/random.html#random.seed ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] lottery problem (Was Re: (no subject))
On Thu, 18 Dec 2014 20:27:03 -0500 Adam Jensen wrote: > And to build the 'lines' list (although, this is getting rather ugly): > > >>> lines = [[random.randint(x,x+6) for x in range(1,50,7)] for i in range(7)] Oops, in the context of the original program this might make more sense if written as: data = [[random.randint(x,x+6) for x in range(1,50,7)] for i in range(lines)] Side note: if one were to only import specific functions from a module, would the load time and memory consumption be smaller? Example, is: from random import randint, seed smaller and faster than: import random Side side note: since 'i' isn't being used, is there a way to loop (within the list comprehension) without the 'i'? For example, to generate three random numbers: [randint(1,10) for i in range(3)] # This works. [randint(1,10) for range(3)] # This does not work. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor