Gabe Levin
Gabe Levin

Reputation: 45

Regarding Python Guessing Game

The goal of this game is to guess a number in three tries, and if you get it right, congratulate you, and if you get it wrong tell you "No." I think I have the code correctly, but I'm new to programming and am unsure. I's test it out to see if it works, but that could take a VERY long time. Any help is appreciated!

# Guess My Number
#
# The computer picks a random number between 1 and 100
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money

import random  

print("\tWelcome to 'Guess My Number'!")
print("\nI'm thinking of a number between 1 and 100.")
print("Try to guess it in as few attempts as possible.\n")

# set the initial values
the_number = random.randint(1, 100)
guess = int(input("Take a guess: "))
tries = 1

# guessing loop
while guess != the_number:
    if guess > the_number:
        print("Lower...")
    elif tries == 3:
        break
    else:
        print("Higher...")

    guess = int(input("Take a guess: "))
    tries += 1

if guess == the_number:
    print("You guessed it!  The number was", the_number)
    print("And it only took you", tries, "tries!\n")
    input("\n\nPress the enter key to exit.")

if tries == 3:
    print("no")
    input("\n\nPress the enter key to exit.")

Upvotes: 1

Views: 1607

Answers (3)

import random

n = random.randrange(1, 11)
count=0


print('I\'ve though a number between 1 and 10!')

while True:
    try:
        g = input('Guess Any Number!')
        g = int(g)
        if not 10>g>0:
            if count==6:
                print ("You Loose")

                print('It\'s in between 0 and 10!')
    except ValueError:
        print('Enter an integer')
        continue

    if g == n:
        print('Congratulations You Gussed That Number!')
        break


    if g < n:
        print('Larger Than The Guessed One')

    if g > n:
        print('Smaller Than The Guessed One')
        count +=1

Upvotes: 0

justengel
justengel

Reputation: 6320

Wrap your code in a function.

def guessing_game(number=None):
    if number is None:
        number = int(input("Take a guess: "))
    ... continue the rest of your code.
    # Probably want a return True or a return false
# end guessing_game

Test your code by creating a unit test

import unittest

class GuessTest(unittest.TestCase):
    def setUp(self):
        """Setup variables. This runs before every test method."""
        self.number = random.randint(1, 100)
    # end setUp

    def test_guess(self):
        """Run the test for the guessing game."""
        result = guessing_game(self.number)

        myvalue = result # You want this to be the expected result

        # check result - for your game you may just want to run a loop 
        self.assertEqual(result, myvalue, "Hey this doesn't work")
    # end test_guess

    def test_other_method(self):
        pass

    def tearDown(self):
        """This runs after every test is finished."""
        # If you have to clean up something
        pass
    # end tearDown
# end class GuessTest

if __name__ == "__main__":
    # Code that runs when you run this file, so importing this file doesn't run your code
    # guessing_game() # Run your function

    unittest.main() # Run the unittests

Other notes: If you know how many times you want to loop then use a for loop.

for i in range(3):
    print(i) # Will print 0, 1, 2
    if guess > the_number:
        print("Lower ...")
    elif guess < the_number:
        print("Higher ...")
    else:
        break # You are correct

Upvotes: 1

Frumples
Frumples

Reputation: 433

To test your code, try printing the number stored in the_number = random.randint(1, 100) before entering the while loop. For example:

# Guess My Number
#
# The computer picks a random number between 1 and 100
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money

import random  

print("\tWelcome to 'Guess My Number'!")
print("\nI'm thinking of a number between 1 and 100.")
print("Try to guess it in as few attempts as possible.\n")

# set the initial values
the_number = random.randint(1, 100)
print("The number is: " + str(the_number))
guess = int(input("Take a guess: "))
tries = 1

# guessing loop
while guess != the_number:
    if guess > the_number:
        print("Lower...")
    elif tries == 3:
        break
    else:
        print("Higher...")

    guess = int(input("Take a guess: "))
    tries += 1

if guess == the_number:
    print("You guessed it!  The number was", the_number)
    print("And it only took you", tries, "tries!\n")
    input("\n\nPress the enter key to exit.")

if tries == 3:
    print("no")
    input("\n\nPress the enter key to exit.")

Upvotes: 0

Related Questions