[Tutor] Question

Alan Gauld alan.gauld at yahoo.co.uk
Sat Oct 3 19:49:23 EDT 2020


On 03/10/2020 22:14, Aaron Klein via Tutor wrote:
> ... I'm not sure what variables to use in the outer loop. 
> Do I create new ones? 

Don't be afraid of creating variables. It's usually a good
thing and adds to the clarity of the program, if you name
them sensibly and they actually have a purpose.

> while():    #prime nested loop    guess = int(input('Guess a number between 1 and 10\n'))    num = random.randint(1, 10)    while(guess != num):        guess = int(input('Guess a number between 1 and 10\n'))        count+=1    print('It took', count, 'guesses to guess correctly.')playAgain = str(input('Would you like to play again?\n'))

Unfortunately the list requires plain text format for code
otherwise we lose all the indentation and structure,
as you can see here.

Taking a stab at reformatting it I get:

while():    #prime nested loop
   guess = int(input('Guess a number between 1 and 10\n'))
   num = random.randint(1, 10)
   while(guess != num):
      guess = int(input('Guess a number between 1 and 10\n'))
      count+=1
      print('It took', count, 'guesses to guess correctly.')
   playAgain = str(input('Would you like to play again?\n'))

If I got it right then you want to loop while playAgain == 'y'?
So that might be a good thing to put in the outer while?
You might need to do some data processing on the input
from the user to make sure it returns 'y' though....
And you need to assign a value to it ('y'?) before the loop test.

BTW In Python we don't need parentheses around the test,
its not C.

Also if you set guess = 0 in the first assignment you don't
need to repeat the user input line. You only need it in the
inner loop.

-- 
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




More information about the Tutor mailing list