[Tutor] Simple python help needed

David L Neil PyTutor at DancesWithMice.info
Thu Aug 29 17:14:06 EDT 2019


On 30/08/19 7:19 AM, 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,
> i cant seem to find a solution. here is the code

It would be kinder if the actual diagnostics returned by Python were 
included!

Python is an interpreted language. So, the code is examined 
line-by-line. If you wish to *use* functions such as easy(), medium(), 
and hard(); then they must have been previously def[ined] (appear 
'higher-up' in the code).

The <<<__name__ == “__main__”>>> idiom (phrase) is commonly-used in 
Python (for more reasons than this question). Please review 
https://www.geeksforgeeks.org/what-does-the-if-__name__-__main__-do/ and 
find a later example which WILL demonstrate how to solve this problem in 
a Pythonic manner!


> Thank you
> 
> 
> 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()
> elif choice =='B' or choice =='b':
>          medium()
> elif choice =='C' or choice =='c':
>          hard()
> print()
> 
> # Gamemode Easy
> def easy():
>      guessesTaken = 0
> 
> print('Hello! What is your name?')
> myName = input()
> 
> number = random.randint(1, 15)
> print('Well, ' + myName + ', I am thinking of a number between 1 and 15.')
> 
> for guessesTaken in range(7):
>      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
> 
> if guess == number:
>     guessesTaken = str(guessesTaken + 1)
>     print('Good job, ' + myName + '! You guessed my number in ' +
>          guessesTaken + ' guesses!')
> if guess != number:
>     number = str(number)
>     print('Nope. The number I was thinking of was ' + number + '.')
> 
> # Gamemode Medium
> 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.')
> 
> for guessesTaken in range(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
> 
> if guess == number:
>      guessesTaken = str(guessesTaken + 1)
>      print('Good job, ' + myName + '! You guessed my number in ' +
>            guessesTaken + ' guesses!')
> 
> if guess != number:
>      number = str(number)
>      print('Nope. The number I was thinking of was ' + number + '.')
> 
> # Gamemode Hard
> def hard():
>      guessesTaken = 0
> 
> print('Hello! What is your name?')
> myName = input()
> 
> number = random.randint(1, 50)
> print('Well, ' + myName + ', I am thinking of a number between 1 and 50.')
> 
> for guessesTaken in range(5):
>      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
> 
> if guess == number:
>      guessesTaken = str(guessesTaken + 1)
>      print('Good job, ' + myName + '! You guessed my number in ' +
>              guessesTaken + ' guesses!')
> if guess != number:
>      number = str(number)
>      print('Nope. The number I was thinking of was ' + number + '.')
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
> 

-- 
Regards =dn


More information about the Tutor mailing list