[Tutor] Infinite Loop

nehal dattani nehal.dattani at gmail.com
Sat Sep 24 17:00:14 CEST 2011


Hi,

Please modify your else block .

else:
        print("Higher...")
        guess = int(input("Take a guess: "))
        tries += 1


This should work for you.

Regards,
Nehal Dattani

On Sat, Sep 24, 2011 at 8:13 PM, Cameron Macleod <cmacleod170 at gmail.com>wrote:

> Hi,
>
> I've been trying to code a simple guess my number game as a starter
> programming project but I've generated an infinite loop accidentally. Since
> I'm new to programming in general, I can't really figure out what's causing
> it. Thanks
>
> =======================
>
> 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")
>
> if tries <= 5:
>     print("I didn't know roadrunners could type!")
> elif tries >= 99:
>     print("P'raps 'Only' wasn't the right word...")
> elif tries == 100:
>     print("0_0 You are the unluckiest person in the world. Or the
> stupidest...")
>
> input("\n\nPress the enter key to exit.")
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110924/2279a65d/attachment.html>


More information about the Tutor mailing list