Jump to content

Guess the number! - Python

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
Azrian

Azrian

    Newbie

  • Members
  • Pip
  • 9 posts
Here is a little program I made in Python! :)
I started learning Python yesterday so I am really proud that I was able to create this and actually get it to work exactly as I want it to! :D

Obviously, I did some specific research for some commands with Google to help me out with the harder stuff (for me) such as creating a loop with while True until break and stuff like that :)

Here's the code!
while True:
    import random
    number = random.randint(1,50)
    tries = 0    
    guess = 0
    print "Find the number!"
    print "It's between 1 and 50, you have 5 tries!"
    while guess != number and tries <5:
        guess = input("What's your guess?: ")
        if guess > number:
            print "Too high!"
        elif guess < number:
            print "Too low!"
        tries = tries + 1
    if guess == number:
        print "You got it! The number was:", number
    else:
        print "You lost! The number was:", number
    answer = raw_input ("Play again?: ")
    if answer.strip() in "n N no No NO".split():
        break

Edited by Azrian, 16 February 2010 - 02:23 PM.
Added the name of the language used in the title


#2
JewFro297

JewFro297

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 224 posts
Nice, pretty similar to the one I just made in C++ class
Yea Dat's right.
Apple sucks :thumbup:
[SIGPIC][/SIGPIC]

#3
saeras

saeras

    Learning Programmer

  • Members
  • PipPipPip
  • 67 posts
Took the liberty of revising your code a little. I hope i didn't offend you by doing this but here it is.

import random
while True:
    number = random.randint(1,50)
    tries = 0    
    guess = 0
    print "Find the number!"
    print "It's between 1 and 50, you have 5 tries!"
    while guess != number and tries <5:
        guess = raw_input("What's your guess?: ")
        if guess.isdigit() == True:
            guess = int(guess)
            if guess > number:
                print "Too high!"
            elif guess < number:
                print "Too low!"
        else:
            print("thats not a number, it still counts as a try though")
        tries = tries + 1
    if guess == number:
        print "You got it! The number was:", number
    else:
        print "You lost! The number was:", number
    answer = raw_input ("Play again?: ")
    if answer.strip().lower() in "n no".split():
        break

heres what i did:
Placed import outside the while loop, because you only have to import it once.
Made it safe to inputs other than integers.
Added .lower() to the last if clause because its a handy function to know. ^^
Posted Image

#4
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Nice, this is also one of the first programs I made to.:thumbup:
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#5
JewFro297

JewFro297

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 224 posts
lol I can't wait till I can look back on this day and say that... My most advanced programs are still a tic tac toe game and one like this. Only I had a little ASCII animation in there for fun, but that doesnt take much skill lol
Yea Dat's right.
Apple sucks :thumbup:
[SIGPIC][/SIGPIC]

#6
Azrian

Azrian

    Newbie

  • Members
  • Pip
  • 9 posts

saeras said:

Took the liberty of revising your code a little. I hope i didn't offend you by doing this but here it is.


import random

while True:

    number = random.randint(1,50)

    tries = 0    

    guess = 0

    print "Find the number!"

    print "It's between 1 and 50, you have 5 tries!"

    while guess != number and tries <5:

        guess = raw_input("What's your guess?: ")

        if guess.isdigit() == True:

            guess = int(guess)

            if guess > number:

                print "Too high!"

            elif guess < number:

                print "Too low!"

        else:

            print("thats not a number, it still counts as a try though")

        tries = tries + 1

    if guess == number:

        print "You got it! The number was:", number

    else:

        print "You lost! The number was:", number

    answer = raw_input ("Play again?: ")

    if answer.strip().lower() in "n no".split():

        break


heres what i did:
Placed import outside the while loop, because you only have to import it once.
Made it safe to inputs other than integers.
Added .lower() to the last if clause because its a handy function to know. ^^

Thanks! I was actually wondering how to make it so when I entered something that wasn't an integer, it would print a message instead of crashing. :)

I also didn't know about that .lower() function, thanks! :)

#7
JewFro297

JewFro297

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 224 posts
look at that I learned somethin new.
Yea Dat's right.
Apple sucks :thumbup:
[SIGPIC][/SIGPIC]