[Tutor] Begginer in need of help

Glen Wheeler gew75 at hotmail.com
Mon Aug 9 08:52:54 CEST 2004


  Hi Julian,

  Welcome to Python!  It's a wonderful language.  I have some comments on
your existing program:

> s = 0
> a = 1
>

  It's always a good idea to name your variables something meaningful.  It
doesn't make any difference to the python interpreter, and it makes your
life easier when trying to debug code you've written.

> name = "dan"

  What is this variable for?

> while a < 4:
>     my_name = raw_input("what is my name? ")
>     if my_name == "julian":
>         print "correct"
>         a = 10
>     elif s < 3:
>         s = s + a
>         print "Nope, you have, ",3-s," tries left"

  This loop will exit if a is not less than 4.
  The basic problem here is that a is only ever assigned a value if my_name
== "julian".  There is no otherwise, you only have an elif part (where a is
not assigned any value).
  I would suggest that you add an else clause, or simply place

a = a+1

  after the elif (outside the scope of that block) like so:

> while a < 4:
>     my_name = raw_input("what is my name? ")
>     if my_name == "julian":
>         print "correct"
>         a = 10
>     elif s < 3:
>         s = s + a
>         print "Nope, you have, ",3-s," tries left"
*>    a = a + 1

  The problem here is that this makes your message give erroneous output.
I'll leave you that to fix yourself, but as a suggestion try removing the s
variable entirely and just use a.  Even better would be to rename a to
perhaps number_of_tries?

  HTH,
  Glen

----- Original Message ----- 
From: "Julian Martinez del Campo" <jule_s14 at hotmail.com>
To: <tutor at python.org>
Sent: Monday, August 09, 2004 2:17 PM
Subject: [Tutor] Begginer in need of help


> I am brand new to python and am learning python through tutorials on the
> net. i am stuck on one exercise, where i have to write a program that has
a
> user guess your name, but they only get 3 chances to do so until the
program
> quits. I cannot get my program to quit after the 3 tries. can u help.
> (here is my program)
>
>
> s = 0
> a = 1
>
> name = "dan"
> while a < 4:
>     my_name = raw_input("what is my name? ")
>     if my_name == "julian":
>         print "correct"
>         a = 10
>     elif s < 3:
>         s = s + a
>         print "Nope, you have, ",3-s," tries left"
>
> _________________________________________________________________
> FREE pop-up blocking with the new MSN Toolbar – get it now!
> http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list