Python Forum
1st Project Guess between 1 and 20
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
1st Project Guess between 1 and 20
#1
Hi, first time on python (only dabbled with VBA previously) and I've done the cliche guessing game (sorry!) Taken me a few days.

I've tried to go further and make it a more robust using functions. I'm finding it really hard to get my head around functions and how to use them with arguments. My code is a little as a result of trial-and-error and I don't quite understand why some of it works. If anyone's willing, could I have some feedback on whether the code is 'neat' or unnecessary or if I've committed any python faux pas? Thank you very much. Shy

# Guessing Game

def string_or_integer(guess):
    while True:
        if guess in ('quit', 'Quit'):
            print('Bye bye!')
            sys.exit()
        elif not guess in ('quit', 'Quit'):
            try:
                return int(guess)
            except ValueError:
                try:
                    float(guess)
                    print("Please use whole numbers")
                    guess = input('>>> ')
                except ValueError:
                    print ("This is not a number. Please enter a number between 1 and 20")
                    guess = input('>>> ')

def game():
    guess = input('>>> ')
    guessInt = string_or_integer(guess)
    if (guessInt == rNum):
        print('Congratulations! You won!')
        sys.exit()
    elif (guessInt > 20):
        print('Way too high! Guess between ONE and TWENTY')
        game()
    elif (guessInt < rNum):
        print('Try a higher number')
        game()
    elif (guessInt > rNum):
        print('Try a lower number')
        game()

import random
import sys
rNum = random.randint(1,20)
print('Welcome to the Guessing Game!\nGuess a number between 1 and 20\nTo give up, type "quit"')
game()
Reply
#2
There is nothing at all wrong with your code that I can see. In terms of being concise, you could technically get by with just "else" on line 8 instead of your "elif" clause, but there is nothing wrong with being explicit here and some may prefer it. (Personally, I try to stick with "else" when the condition is binary like this one. Because your "if" condition is unsatisfied, you know your "elif" condition will be satisfied without explicitly checking for it.) You could also theoretically get by without differentiating between floats and strings (lines 12-18) since you really only care whether you have a valid integer or not, but what you did is cleverly implemented and certainly not incorrect.

Great job! Things you might consider adding:
-A check to see if the user entered a number below 1 (the same way you check for a number above 20)
-A guess count to show the user how many guesses they needed to win
-A prompt to let the user play again after a round is over
Reply
#3
(May-31-2020, 09:35 AM)MiNigle Wrote:
# Guessing Game

def string_or_integer(guess):
    while True:
        if guess in ('quit', 'Quit'):
            print('Bye bye!')
            sys.exit()
        elif not guess in ('quit', 'Quit'):
            try:
                return int(guess)
            except ValueError:
                try:
                    float(guess)
                    print("Please use whole numbers")
                    guess = input('>>> ')
                except ValueError:
                    print ("This is not a number. Please enter a number between 1 and 20")
                    guess = input('>>> ')
Very nice code, I see in your type 'Quit' to give up you could of made it more effective and simpler. Let's say they type qUiT the program does not know what to do. I would like for you to take a look of my guessing game when I first started out where I did the same concept. https://repl.it/@KEVINGRANADOS1/GuessNumber take a look on line 43 and try to experiment with it, it is much simpler and efficient.
Reply
#4
(May-31-2020, 04:11 PM)NectDz Wrote: I see in your type 'Quit' to give up you could of made it more effective and simpler. Let's say they type qUiT the program does not know what to do.

This is a good point. One easy way to deal with this possibility would be to change line 5 to use the string method lower() so that no matter how they type it, you only evaluate what it looks like with all lower-case letters.
if guess.lower() == 'quit':
Reply
#5
Thanks for your replies!

(May-31-2020, 03:05 PM)GOTO10 Wrote: Great job! Things you might consider adding:
-A check to see if the user entered a number below 1 (the same way you check for a number above 20)
-A guess count to show the user how many guesses they needed to win
-A prompt to let the user play again after a round is over

All good ideas! I hadn't considered the possibility of guesses below 1, and I chickened out of doing guess counts but I may give it a go now. Also thanks for pointing out the elif.


(May-31-2020, 04:11 PM)NectDz Wrote: I see in your type 'Quit' to give up you could of made it more effective and simpler. Let's say they type qUiT the program does not know what to do. I would like for you to take a look of my guessing game when I first started out where I did the same concept. https://repl.it/@KEVINGRANADOS1/GuessNumber take a look on line 43 and try to experiment with it, it is much simpler and efficient.
(May-31-2020, 04:24 PM)GOTO10 Wrote: This is a good point. One easy way to deal with this possibility would be to change line 5 to use the string method lower() so that no matter how they type it, you only evaluate what it looks like with all lower-case letters.
if guess.lower() == 'quit':

Took a look at your code NectDz, I see what you mean. I also liked the 'time.sleep(.1)' lines, didn't know it could do that.

These are very clever thank you both. Much more elegant. The more you know Smile
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020