1

My next task is modifying current code. In a previous exercise, I've written a basic application that covers a numbers guessing game. The code is as follows: -

# 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...")
    else:
        print("Higher...")

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

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.")

My task is to modify this so that there is a limited number of goes before a failure message is given to the user. Thus far, the chapter has covered "if, elif, else, for, loops, avoiding infinte loops." As such, I'd like to limit my response to these concepts only. For loops are covered next chapter.

What have I tried?

So far, I've tried amending the block in another while loop using 5 goes and the tries variable but it doesn't seem to work.

# guessing loop
while tries < 6:
    guess = int(input("Take a guess: "))
    if guess > the_number:
        print("Lower...")
    elif guess < the_number:
        print("Higher...")
    elif guess == the_number:
        print("You guessed it!  The number was", the_number)
        print("And it only took you", tries, "tries!\n")
    break
    tries += 1

input("You didn't do it in time!")
input("\n\nPress the enter key to exit.")

Any pointers or highlighting what I've missed would be appreciated plus any explanation as to what I'd missed. Teaching myself to think programatically is aslo proving tricky.

What doesn't work When I run it, the loop conditions't don't appear to work. My idle feedback is as follows.

This means my question can be summarised as Where is my looping logic broken?

>>> ================================ RESTART ================================
>>> 
    Welcome to 'Guess My Number'!

I'm thinking of a number between 1 and 100.
Try to guess it in as few attempts as possible.

Take a guess: 2
Take a guess: 5
Higher...
You didn't do it in time!


Press the enter key to exit.
6
  • You need to define what "doesn't seem to work" means. Does it give an error? Do you get output that doesn't match your expectations? Please include those expectations and what the output you got instead is. Commented Mar 1, 2013 at 16:08
  • Not a real-question (what is the problem?) / off-topic (code reviews are on codereviews.stackexchange.com) Commented Mar 1, 2013 at 16:09
  • Incidentally, you can save a bit of code by using a for loop instead of a while loop, and get rid of the tries variable. Just change the while tries < 6: line to for i in range(6):. Commented Mar 1, 2013 at 16:11
  • Martijn / rds: Question added. Thanks for pointing that out. Commented Mar 1, 2013 at 16:13
  • @StevenDAndrews: Did you try out my answer? Commented Mar 1, 2013 at 16:17

2 Answers 2

2

The problem is that your break statement is not indented to be included in your elif:

elif guess == the_number:
    print("You guessed it!  The number was", the_number)
    print("And it only took you", tries, "tries!\n")
break

Thus, the loop always stops after the first iteration. Indent the break to be included within the elif and it should work.

Sign up to request clarification or add additional context in comments.

Comments

0

The break is not in the conditional. Add a tab before it.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.