2
\$\begingroup\$

I'm a newbie trying to teach myself Python. Could somebody tell me if there is a better way to write this? I am also curious as to whether or not there is anything I should not do. This code does work, but some indentation may have been lost when I pasted it.

import random

def game():
    yes = ['y', 'Y', 'Yes', 'yes']
    number = random.randint(1, 20)
    numAttempts = 0
    print('')
    name = input('What is your name? ')

    print('')
    print('Nice to meet you', name, '! I am going to guess a random number! You will have 3 tries to guess it. '
                                'Good luck!')


    while numAttempts < 3:
        print('')
        guess = int(input('Guess the number I have chosen between 1 and 20. '))
        numAttempts += 1
        if guess == number:
            print('')
            print('You guessed correctly! The number was', number, "!", 'Great Job', name, '!')
            break
        elif guess > number:
            print('')
            print('The number is lower than', guess, "!")
        elif guess < number:
            print('')
            print('The number is Higher than', guess, "!")
    else:
        print('')
        print('Sorry, the number I chose was', number, 'but you have used all 3 tries', name, '!')

    print('')
    gameover = input("Would you like to play again? Yes or No? ")
    while gameover in yes:
        game()
    else:
        print('')
        print('You should have chose yes. You computer will now explode!')

game()
\$\endgroup\$
2
\$\begingroup\$

A few observations:

  • In place of the while loop with you managing the numAttempts index, why not use a for loop with the in keyword? i.e. for numAttempts in range(3):
  • Note that with the above, you can still use your else block (for supports the else clause! :) )
  • Multiple games keep asking for your name. Unless your code has Alzheimer's, consider avoiding that :)
  • Related to the above : Structurally, you've chosen to recurse to play another game. Why not keep your game atomic and keep the whole offer another game choice/logic external to your function?
  • Your second elif does not make sense. Why not just else (guess can only be < number now!)
\$\endgroup\$
2
\$\begingroup\$

Aren't you stuck in infinite loop? Because I think this part is designed really badly:

gameover = input("Would you like to play again? Yes or No? ")
while gameover in yes:
    game()

if you answer yes at least one time, gameover will always be yes, so you never leave the game.

The easiest way to do such programs is (treat this rather as pseudocode):

def game():
    # initialization here, prints, ask for name etc
    gameover = True     
    while gameover:
        # Here gamelogic
        inp = input("Play again?")
        if not inp in yes:
            gameover = False
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.