i think we should use raw_input('Please enter your name: ') instead of input('Please enter your name: ')<br><br><div class="gmail_quote">2008/12/8 Peter Otten <span dir="ltr"><__<a href="mailto:peter__@web.de">peter__@web.de</a>></span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="Ih2E3d">simonh wrote:<br>
<br>
> In my attempt to learn Python I'm writing a small (useless) program to<br>
> help me understand the various concepts. I'm going to add to this as I<br>
> learn to serve as a single place to see how something works,<br>
> hopefully. Here is the first approach:<br>
<br>
</div><div><div></div><div class="Wj3C7c">> That works fine. Then I've tried to use functions instead. The first<br>
> two work fine, the third fails:<br>
<br>
> def getName():<br>
>     name = input('Please enter your name: ')<br>
>     print('Hello', name)<br>
><br>
> def getAge():<br>
>     while True:<br>
>         try:<br>
>             age = int(input('Please enter your age: '))<br>
>             break<br>
>         except ValueError:<br>
>             print('That was not a valid number. Please try again.')<br>
><br>
> def checkAge():<br>
>     permitted = list(range(18, 31))<br>
>     if age in permitted:<br>
>         print('Come on in!')<br>
>     elif age < min(permitted):<br>
>         print('Sorry, too young.')<br>
>     elif age > max(permitted):<br>
>         print('Sorry, too old.')<br>
><br>
> getName()<br>
> getAge()<br>
> checkAge()<br>
><br>
> I get this error message: NameError: global name 'age' is not<br>
> defined.<br>
><br>
> I'm stuck, can someone help? Thanks.<br>
<br>
<br>
</div></div>Generally, when you calculate something within a function you tell it the<br>
caller by returning it:<br>
<br>
>>> def get_age():<br>
...     return 74<br>
...<br>
>>> get_age()<br>
74<br>
>>> age = get_age()<br>
>>> age<br>
74<br>
<br>
And if you want a function to act upon a value you pass it explicitly:<br>
<br>
>>> def check_age(age):<br>
...     if 18 <= age <= 30:<br>
...             print("Come in")<br>
...     else:<br>
...             print("Sorry, you can't come in")<br>
...<br>
>>> check_age(10)<br>
Sorry, you can't come in<br>
>>> check_age(20)<br>
Come in<br>
<br>
To check the age determined by the get_age() function you do:<br>
<br>
>>> age = get_age()<br>
>>> check_age(age)<br>
Sorry, you can't come in<br>
<font color="#888888"><br>
Peter<br>
</font><div><div></div><div class="Wj3C7c">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br>