[Tutor] (no subject)

Steven D'Aprano steve at pearwood.info
Fri Dec 19 02:03:35 CET 2014

On Thu, Dec 18, 2014 at 09:10:40PM +0000, 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:

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

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.


More information about the Tutor mailing list