Re: Beginner trying to understand functions.

2008-12-08 Thread cadmuxe
i think we should use raw_input('Please enter your name: ') instead of
input('Please enter your name: ')

2008/12/8 Peter Otten <[EMAIL PROTECTED]>

> simonh wrote:
>
> > In my attempt to learn Python I'm writing a small (useless) program to
> > help me understand the various concepts. I'm going to add to this as I
> > learn to serve as a single place to see how something works,
> > hopefully. Here is the first approach:
>
> > That works fine. Then I've tried to use functions instead. The first
> > two work fine, the third fails:
>
> > def getName():
> > name = input('Please enter your name: ')
> > print('Hello', name)
> >
> > def getAge():
> > while True:
> > try:
> > age = int(input('Please enter your age: '))
> > break
> > except ValueError:
> > print('That was not a valid number. Please try again.')
> >
> > def checkAge():
> > permitted = list(range(18, 31))
> > if age in permitted:
> > print('Come on in!')
> > elif age < min(permitted):
> > print('Sorry, too young.')
> > elif age > max(permitted):
> > print('Sorry, too old.')
> >
> > getName()
> > getAge()
> > checkAge()
> >
> > I get this error message: NameError: global name 'age' is not
> > defined.
> >
> > I'm stuck, can someone help? Thanks.
>
>
> Generally, when you calculate something within a function you tell it the
> caller by returning it:
>
> >>> def get_age():
> ... return 74
> ...
> >>> get_age()
> 74
> >>> age = get_age()
> >>> age
> 74
>
> And if you want a function to act upon a value you pass it explicitly:
>
> >>> def check_age(age):
> ... if 18 <= age <= 30:
> ... print("Come in")
> ... else:
> ... print("Sorry, you can't come in")
> ...
> >>> check_age(10)
> Sorry, you can't come in
> >>> check_age(20)
> Come in
>
> To check the age determined by the get_age() function you do:
>
> >>> age = get_age()
> >>> check_age(age)
> Sorry, you can't come in
>
> Peter
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: Beginner trying to understand functions.

2008-12-08 Thread cadmuxe
On 12月8日, 下午10时53分, "James Mills" <[EMAIL PROTECTED]>
wrote:
> On Tue, Dec 9, 2008 at 12:46 AM, Peter Otten <[EMAIL PROTECTED]> wrote:
> > I think the OP is using Python 3.0. What used to cause trouble
>
> Well of course he/she/it is!
> I'm too blind to have noticed that! :)
>
> --JamesMills
>
> --
> --
> -- "Problems are solved by method"

oh,i think i should go to read the new document of py3k
thanks for all
--
http://mail.python.org/mailman/listinfo/python-list


Re: Password input in console/terminal

2008-12-09 Thread cadmuxe
On 12月9日, 下午2时01分, "Chris Rebert" <[EMAIL PROTECTED]> wrote:
> On Mon, Dec 8, 2008 at 9:53 PM, RP <[EMAIL PROTECTED]> wrote:
> > Hello All,
>
> > This is my first REAL post(question) to Python-List. I know I can take input
> > from a user with raw_input()
> > How do I take password input in console? Any Help would be Greatly
> > Appreciated. Thank You. RP
>
> You use the appropriately-named `getpass` 
> module:http://docs.python.org/library/getpass.html
>
> Cheers,
> Chris
>
> --
> Follow the path of the Iguana...http://rebertia.com

you can use getpass()
import getpass
passwd = getpass.getpass()

---
>>> import getpass
>>> passwd = getpass.getpass()
Password:
--
http://mail.python.org/mailman/listinfo/python-list