number of loops

Michael F. Stemper michael.stemper at gmail.com
Sat Mar 17 10:44:33 EDT 2018


On 2018-03-15 08:28, michealmancini at gmail.com wrote:
> I would like to have this offer 6 guesses, but instead it gives one guess and prints the if statement/output 6 times....any ideas where I went wrong?

It sounds as if you'd like to have it making the offer within the loop
rather than before the loop.

Some other thoughts:

> number_of_guesses = 0

Further on down, you convert this to a string. It might be simpler to
just say:

number_of_guesses = '0'

This will eliminate the need to convert it to a string later on.

Right here is where you offer a guess:

> if yes_or_no == 'yes':
>      print ('ok! guess my number')
> else:
>          print ('log off and go home')
> 
> my_number=random.randint(1,100)

Here is where you read the guess:

> your_guess=input()
> your_guess=int(your_guess)

You could combine these two statements into:

your_guess=int(input())


Here's the loop:

> for number in range(6):
>      if your_guess > my_number:
>          print ('nope, too high')
>      if your_guess < my_number:
>          print ('nope, too low')
>      if your_guess == my_number:
>          break

Notice how the loop doesn't include anything to offer or read a guess?

Here's where you convert the integer 0 from the beginning of the program
into a string. As I mentioned above, it'd be simpler to just initialize
to '0' instead of to 0.

> if your_guess == my_number:
>      number_of_guesses=str(number_of_guesses)
>      print ('good job, you guessed correctly')
> else:
>      print('nope, you lose')

Of course, since you never use this variable for anything, you could
also drop it altogether.

-- 
Michael F. Stemper
Nostalgia just ain't what it used to be.



More information about the Python-list mailing list