0

im teaching myself python and im abit confused

#!/usr/bin/python

def Age():
        age_ = int(input("How old are you? "))


def Name():
        name_ = raw_input("What is your name? ")


def Sex():
        sex_ = raw_input("Are you a man(1) or a woman(2)? ")
        if sex_ == 1:
                man = 1

        elif sex_ == 2:
                woman = 2

        else:
                print "Please enter 1 for man or 2 for woman "

Age()
Name()
Sex()
print "Your name is " + name_ + " you are " + age_ + " years old and you are a " + sex_ + "."

Error

File "./functions2.py", line 25, in print "Your name is " + name_ + " you are " + age_ + " years old and you are a " + sex_ + "." NameError: name 'name_' is not defined

Surely it is defined in the Name() function? Im confused :(


Arr I am now thanks for making it a bit more newb proof, Ive now got a problem in the Sex() function. It was returning a number in the print rather than a word "man" or "woman" so I change the code to try fix it. But im getting the following error now File

"./functions2.py", line 16
    2 = woman
SyntaxError: can't assign to literal

I tried to make the 2 an str(2) but it gave me another error. Thanks for you help so far appreciated

#!/usr/bin/python

def Age():
    age_ = raw_input("How old are you? ")
    return age_
def Name():
    name_ = raw_input("What is your name? ")
    return name_
def Sex():
    sex_ = str(raw_input("Are you a man or a woman? "))
    if sex_ == 1:
            1 = man
            return sex_

    elif sex_ == 2:
            2 = woman
            return sex_

    else:
            print "Please enter man or woman "


age_ = Age()
name_ = Name()
sex_ = Sex()


print "Your name is " + name_ + " you are " + age_ + " years old and you are a " + sex_ + "."
casperOne
  • 73,706
  • 19
  • 184
  • 253
k3eper
  • 41
  • 1
  • 8

3 Answers3

0

It is, but _name's scope only extends to the end of the Name() function. Once the Name() function exits, the _name variable is gone.

Try something like this instead:

def Name():
    name = raw_input("What is your name? ")
    return name

_name = Name()

print "Your name is " + _name

You'll need to do something similar for age and sex.

Collin
  • 11,977
  • 2
  • 46
  • 60
  • Thanks for the response, so name_ is a local variable to that function? (Sorry trying to learn the naming conventions too) So do I do name_ = Name() outside of the function? – k3eper Nov 04 '12 at 15:06
  • Yes, [this answer](http://stackoverflow.com/a/292502/224286) explains it quite well. – Collin Nov 04 '12 at 15:08
  • I tried adding name_ = Name() but i get the following error now print "Your name is " + name_ + " you are " + age_ + " years old and you are a " + sex_ + "." TypeError: cannot concatenate 'str' and 'NoneType' objects – k3eper Nov 04 '12 at 15:14
  • Are you actually `return`ing anything from the `Name()` function?. I've edited my code to make it a little more clear – Collin Nov 04 '12 at 15:15
0

Final working code in case anyone comes up with same issues

#!/usr/bin/python

def Age():
    age_ = raw_input("How old are you? ")
    return age_
def Name():
    name_ = raw_input("What is your name? ")
    return name_
def Sex():
    sex_ = str(raw_input("Are you a man or a woman? "))
    if sex_ == "man":
            sex_ = "man"
            return sex_

    elif sex_ == "woman":
            sex_ = "woman"
            return sex_

    else:
            print "Please enter man or woman "


age_ = Age()
name_ = Name()
sex_ = Sex()


print "Your name is " + name_ + " you are " + age_ + " years old and you are a " + sex_ + "."
k3eper
  • 41
  • 1
  • 8
0

I've changed "man" "1" 1, etc. to match the question. I've also changed str(raw_input("Are you a man...")) into input("Are you a man...") to reflect the datatype.

def Age():
    age_ = raw_input("How old are you? ")
    return age_
def Name():
    name_ = raw_input("What is your name? ")
return name_
def Sex():
    sex_ = input("Are you a man(1) or a woman(2)? ")
    if sex_ == 1:
        sex_ = "man"
        return sex_

    elif sex_ == 2:
        sex_ = "woman"
        return sex_
    else:
        print "Please enter 1 for man or 2 for woman "

age_ = Age()
name_ = Name()
sex_ = Sex()

print "Your name is " + name_ + " you are " + age_ + " years old and you are a " + sex_ + "."
giraffe.guru
  • 480
  • 8
  • 21