Python

Moderators: None (Apply to moderate this forum)
Number of threads: 474
Number of posts: 1166

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
A beginner's question Posted by Squirrel on 4 Dec 2002 at 2:07 PM
Hi, I'm quite a newbie in programming, so this would seem a little lame quesion but I need your help!
So I started learning Python, but I encountered a problem I can't cope with. My task is to "Write a program that has a user guess your name, but they only get 3 chances to do so until the program quits."(hey dont start laughing! :)
I've written something but I can't make him to work properly:

my_name = "Squirrel"
a = 0
guess = raw_input ("What's my name?")

while a != 3:
if guess == my_name:
print "That's it!"
else:
print "Try again."
a = a + 1

I've tried several ways to do that task but I've always ended up in looping. Could somebody please help me telling what am I doing wrong?
Report
Re: A beginner's question Posted by infidel on 4 Dec 2002 at 3:44 PM
: Hi, I'm quite a newbie in programming, so this would seem a little lame quesion but I need your help!
: So I started learning Python, but I encountered a problem I can't cope with. My task is to "Write a program that has a user guess your name, but they only get 3 chances to do so until the program quits."(hey dont start laughing! :)
: I've written something but I can't make him to work properly:
:
: my_name = "Squirrel"
: a = 0
: guess = raw_input ("What's my name?")
:
: while a != 3:
: if guess == my_name:
: print "That's it!"
: else:
: print "Try again."
: a = a + 1
:
: I've tried several ways to do that task but I've always ended up in looping. Could somebody please help me telling what am I doing wrong?

Here's one way to do it:

my_name = "Squirrel" 
a = 0 

while a != 3: 
    guess = raw_input("Guess my name: ") 
    if guess == my_name: 
        print "That's it!"
        break
    else: 
        print "That's not it!" 
        a = a + 1 
else:
    print "Sorry!"


Make sure you get the indentation correct! In Python it matters.
Report
Re: A beginner's question Posted by Squirrel on 4 Dec 2002 at 3:56 PM
Thanks! You've helped a lot!



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.