Welcome to Dream.In.Code
Become an Expert!

Join 150,188 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,080 people online right now. Registration is fast and FREE... Join Now!




Something isn't right with this code

 
Reply to this topicStart new topic

Something isn't right with this code, guessing game problem

akarren
14 Sep, 2008 - 11:19 AM
Post #1

New D.I.C Head
*

Joined: 14 Sep, 2008
Posts: 1

I'm trying to make the guessing game stop at 5 tries which it is doing but for some reason it isn't printing you fail when the user doesn't guess the number in 5 tries
CODE

# Adam Karren
# 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, but the user only has 5 guesses

print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100."
print "Try to guess the number quick, you only got 5 chances."

import random

# set the intial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess: "))
tries = 1

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

    guess = int(raw_input("Take a guess: "))
    tries +=1
    if tries == 5:
        break
        print "You fail"
print "You guessed it! The number was", the_number

raw_input("\n\nPress the enter key to exit")

User is offlineProfile CardPM
+Quote Post

Stutzbach
RE: Something Isn't Right With This Code
14 Sep, 2008 - 12:00 PM
Post #2

New D.I.C Head
*

Joined: 6 Jul, 2008
Posts: 11



Thanked: 3 times
My Contributions
QUOTE(akarren @ 14 Sep, 2008 - 01:19 PM) *

CODE

    if tries == 5:
        break
        print "You fail"



The "break" causes it to jump out of the loop, so the print statement is never executed. Put the print statement before the break and it should work as desired.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:09AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month