Dnia 16-02-2010 o 21:55:47 Sander Sweers <sander.swe...@gmail.com>
napisaƂ(a):

On 16 February 2010 21:32, Shurui Liu (Aaron Liu) <shuru...@gmail.com> wrote:
Here is a program I wrote, I don't know why I cannot exit when I tried 10
times? Hope somebody can help me. Thank you!

while (guess != the_number):
    if (guess > the_number):
        print ("Lower...")
        print ("You were wrong, try again", tries, "times left.\n")
    else:
        print ("Higher...")
        print ("You were wrong, try again", tries, "times left.\n")

    guess = int(raw_input("Take a guess: "))
    tries -= 1

This will loop untill guess == the_number with no other way out.
Meaning unless the number is guessed it will end up in an infinite
loop. You will have to figure out how to *break* out ot the while loop
when tries == 0.

Hello,

Shurui Liu try adding, as Sander Sweers has written, the break keyword to
your script.

In the meantime I've written an alternative version of the program that
does the same thing. I'm a complete beginner (Python is my first
programming language) and a hobbyist so I thought I could take up the
challenge and try to solve the problem in my spare time.

The script seems to work, I've tested it. List members, if there are any
mistakes or things that can be done better, please let me know.

Here's the body of the script:

#!/usr/bin/python
# -*- coding: utf-16 -*-

right_guess = 7
number_of_guesses = 10

print "Guess the number. You have ten guesses"

for an_attempt in range(1, 11):
      answer = int(raw_input("Guess the number: "))
      if answer == right_guess:
          print "That's the correct number! Congrats pal, I mean my dear
teacher ;)"
          break
      else:
          number_of_guesses = number_of_guesses - 1
          if number_of_guesses == 0:
              print "You have no more guesses.\n\t\tGAME OVER"
              break
print "That's not the right number. Choose again, you have %i more
guesses" % number_of_guesses


Regards,
Piotr

Attachment: guess_my_number.py
Description: Binary data

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to