0
\$\begingroup\$
def guessr():
    '''Application that generates 5 numbers which a user enters a number as a guess to match any of the number generated
    by the application, if you is able to guess one of the numbers correct, user has won, else user lost'''

    from random import randint

    print 'Welcome to the Number Guesser Application'
    print 'You are required to enter a number between 1 to 20'
    print 'You would be rewarded for a correct answer guessed right'


    end_game = False
    computer_guess = []

    # Generate a list of 5 unique numbers
    for x in range(0, 5):
        computer_guess.append(randint(1, 20))

    # Check if user guess with an integer, if you entered some more than an integer, immediatelly end game
    try:
        while not end_game:
            # User gusess
            user_guess = int(raw_input('Enter your guess: '))
            # Guess can not be less than 0 and more than 20
            if user_guess > 20:
                print 'Your guess can not be greater than 20'
            elif user_guess < 1:
                print 'Your guess can not be less than 1'
            elif user_guess in computer_guess:
                print 'Your got it right. {} was in the list of guesses.'.format(user_guess)
                # Finally end game
                end_game = True
            else:
                print 'Please try again.'
    except ValueError:
        print 'To guess you must enter a number'
        # Testing the new line break in my code
        print 'Invalid characters are not allowed.\nGood Bye!!!' 
        # print 'Good Bye!!!' 

def main():
    guessr()

if __name__ == '__main__':
    main()
\$\endgroup\$
2
  • \$\begingroup\$ Comparing this to the previous scripts i posted. \$\endgroup\$
    – blakroku
    Oct 25, 2015 at 21:45
  • 1
    \$\begingroup\$ Your post is a little empty right now. I think you could improve its quality if you added some more information to it, like what you would like reviewed. \$\endgroup\$
    – SirPython
    Oct 25, 2015 at 21:53

2 Answers 2

3
\$\begingroup\$
  • A main() function is not required in Python - so why do you have a main() function and guessr() function if all main() does is call guessr()? You can just remove the main() function and do:
if __name__ == '__main__':
    guessr()

which calls the guessr() function

  • If you want to make the game harder, you could use random.sample(). This will create the random numbers without duplicates. It also removes your for loop, so you could simply do:
computer_guess = random.sample(xrange(1, 20), 5)
\$\endgroup\$
1
\$\begingroup\$

Don't put imports inside a function, put them at the very top of the file before you run any other code. There's no reason to put the import inside the function. Even if this function is being imported to another script, the import will run this import from random for you so you wont have issues.

Instead of using

for x in range(0, 5):
    computer_guess.append(randint(1, 20))

You could just use a list comprehension:

computer_guess = [randint(1, 20) for _ in range(5)]

You don't need to pass 0 as the starting value, that's the default. Also it's idiomatic to use _ for the name of an unused value in Python.

Also you could just put the ValueError directly around the user input and not break the game. Just point out that their input wasn't valid and then let the loop run again.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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