Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (0)

Powered by Vanilla. Made with Bootstrap.
Python number guessing game
  • nu11byte
    Posts: 53
    Well, I got bored and decided to brush up on some Python. It's been quite a while since I actually sat down and coded a program from scratch. This is very simple, yet I have tried to add some level of complexity to it. Feel free to post some changes and suggestions to this program (Yes, I know I didn't need to define some stuff, I just did to refresh my mind).

    Edit: Source code posted on Pastebin as the forums code tags fubar'd my code and indentations.

    http://pastebin.com/NgH5N63X
  • Deque
    Posts: 78
    Your program fails if the user types in something that is not number. The rest seems fine.
  • nu11byte
    Posts: 53
    said:


    Your program fails if the user types in something that is not number. The rest seems fine.



    Yeah, I realise that. Because this is a program that is run locally and doing anything malicious wouldn't benefit anyone (like crashing it), I felt no need to fix that.
  • undead
    Posts: 822
    said:


    Your program fails if the user types in something that is not number. The rest seems fine.



    said:


    If someone guesses it right the first time, your program will say it only took 0 guesses.




    #!/usr/bin/python

    import random
    '''
    Random number guessing game. Just made because I was bored. :p
    Number is randomly generated from between 1 and 100.
    Made by nu11byte.
    A lot of un-needed crap, but I was just having a play. Just refreshing
    my mind. Parts of Python have been forgot and left behind for other languages syntax. Luckily Python's
    syntax is quite similar to other languages.
    '''

    #Use and edit this as you wish.
    def cls():
    print '''

    ______ _
    (_____ \ _ | |
    _____) ) _| |_ | | _ ___ ____
    | ____/ | | | _)| || \ / _ \| _ \
    | | | |_| | |__| | | | |_| | | | |
    |_| \__ |\___)_| |_|\___/|_| |_|
    (____/

    '''
    print \"\n\"
    cls()
    print \"Guess what number I am thinking of. My number is between 1 and 100.\"

    def main():
    randomNumber = random.randint(1,100)
    userGuess = 0
    guessCount = 0
    while (userGuess!=randomNumber):

    userGuess = raw_input(\"Guess: \")
    try:
    userGuess = int(userGuess)
    except:
    print userGuess + \" is not a number.\"
    main()
    guessCount += 1
    if (userGuess < randomNumber):
    print \"Guess a little higher.\"
    elif (userGuess > randomNumber):
    print \"Guess a little lower.\"
    elif (userGuess == randomNumber):
    if guessCount == 1:
    print \"You found it with the first try!\"
    playagain()
    else:
    print \"It took you %d goes to correctly guess the number.\" % guessCount
    playagain()
    def playagain():
    pause = raw_input(\"Play again? (y/n): \");
    if (pause == \"y\"):
    userGuess = 0
    guessCount = 0
    randomNumber = random.randint(1,100)
    cls()
    main()
    main()
    ###EOF


    :)
  • nu11byte
    Posts: 53
    said:


    If someone guesses it right the first time, your program will say it only took 0 guesses.



    No it doesn't... I just tried it.
  • undead
    Posts: 822
    It says 1 not 0:
    Guess what number I am thinking of. My number is between 1 and 100.
    Guess: 5
    It took you 1 goes to correctly guess the number.
    Play again? (y/n):


    But with the change I made if you find it with the first try it would be like this:
    Guess what number I am thinking of. My number is between 1 and 100.
    Guess: 28
    You found it with the first try!
    Play again? (y/n):


    and the other change I made:

    Guess what number I am thinking of. My number is between 1 and 100.

    Guess: hi
    hi is not a number.
    Guess:
  • nu11byte
    Posts: 53
    said:


    It says 1 not 0:

    Guess what number I am thinking of. My number is between 1 and 100.
    Guess: 5
    It took you 1 goes to correctly guess the number.
    Play again? (y/n):


    But with the change I made if you find it with the first try it would be like this:
    Guess what number I am thinking of. My number is between 1 and 100.
    Guess: 28
    You found it with the first try!
    Play again? (y/n):


    and the other change I made:

    Guess what number I am thinking of. My number is between 1 and 100.

    Guess: hi
    hi is not a number.
    Guess:



    Okay, thanks. Your code inspired me to use/learn new ways of producing my original program. Cheers. :)