[Tutor] Simple python help needed

Alan Gauld alan.gauld at btinternet.com
Thu Aug 29 19:36:39 EDT 2019


On 29/08/2019 20:19, Cheyne Skapyak wrote:
> Hello, so i'm trying to modify some code so that basically i can choose
> what difficulty of guess the number i want to play. I'm trying to use def
> but it keeps coming back saying they aren't defined which i think they are,


That could mean a couple of tongs. Please always send the exact error
messages
in full so we don't need to guess.


> import random
> import time
>
> print ('Welcome!')
> print()
> print ('************MAIN MENU************')
> print()
> time.sleep(.5)
> print ('Would you like to play the Easy, Medium, or Hard game mode?')
> print()
>
> choice = input('''
>                   A: Easy Game Mode
>                   B: Medium Game Mode
>                   C: Hard Game mode
>
>                   Please enter your choice: ''')
> if choice =='A' or choice =='a':
>         easy()

You xall easy() but it hasn't been defined yet.

However even if you move the definition to the top there is still a problem


def easy():

       guessesTaken = 0


Here is the definition which does absolutely nothing useful.

That's because guessesTaken is a local variable only visible
inside easy(). You need to return the value to the outside world
to be able to use it. However, since you always set the same
value you might as well not use a function but just assign the
value directly


if choice =='A' or choice =='a':
        guessesTaken = 0

for guessesTaken in range(7):

inside the loop guessesTaken will be an integer (0..6)

>     print('Take a guess.') # Four spaces in front of "print"
>     guess = input()
>     guess = int(guess)
>
>     if guess < number:
>        print('Your guess is too low.') # Eight spaces in front of "print"
>
>     if guess > number:
>        print('Your guess is too high.')
>
>     if guess == number:
>        break

But you never use guessesTaken inside the loop.


> if guess == number:
>    guessesTaken = str(guessesTaken + 1)
>    print('Good job, ' + myName + '! You guessed my number in ' +
>         guessesTaken + ' guesses!')

You don;t need all the string addition. print knows how to convert
things to strings automativally so just use:


> if guess == number:
>    print('Good job,', myName, '! You guessed my number in ', guessesTaken,' guesses!')


Or for more control use string formatting:

> if guess == number:
>    print('Good job, %s! You guessed my number in %d guesses!' %(myname, guessesTaken))

def medium():

>     guessesTaken = 0
>
>     print('Hello! What is your name?')
>     myName = input()
>     number = random.randint(1, 20)
>
>     print('Well, ' + myName + ', I am thinking of a number between 1 and
> 20.')

This time you have put some of the game handling code inside the function

You didn't do that with easy()...

But again note that guessesTaken and myname are only visible inside the
function. You need to return them for the rest of your code to use them.

I'll stop here since the rest would just have the same comments.


In summary:

- move your functions to the top of the program.

- return the values you want to use.

- Don't bother with the str() conversions, let print do it for you.

- Think about how you can avoid all the repetition of code.

You are on the right track you just need to refine the code slightly.


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