A beginner's question - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

A beginner's question

SquirrelSquirrel Posts: 2Member
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?

Comments

  • infidelinfidel Posts: 2,900Member
    : 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:

    [code]
    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!"
    [/code]

    Make sure you get the indentation correct! In Python it matters.
  • SquirrelSquirrel Posts: 2Member
    Thanks! You've helped a lot!
Sign In or Register to comment.