0

I'm learning python, I'm still in the newbie exercises...So have an exercise where I need to say the ideal weight to the height of the people,considering its genre... So my code it is:

#!/ sr/bin/python
heigth = float(input("Enter the height of the person: "))
sex = input("Enter the person's gender:")

if(sex == "male"):
        pi = (72.7 * heigth) - 58
elif(sex=="female"):
        pi = (62.1 * heigth) - 44.7
else:
        print("Invalid gender")

print("The ideal weight for this person is:",pi)

And the error:

File "n13.py", line 3, in sex = input("Enter the person's gender:") File "", line 1, in NameError: name 'female' is not defined

I don't understanding why the string in the comparison have to be defined if it is just a string??? o.O

Thanks in advance!

jimifiki
  • 5,377
  • 2
  • 34
  • 60
  • 2
    I'm not receiving the same error on Python 3.x, maybe you we running it on Python 2.x? The error could happen on Python 2.x because `input()` is different between the two versions. If you are using Python 2.x, replace `input()` with `raw_input()` – MooingRawr Jan 24 '17 at 14:49
  • Just replace input() with raw_input() in Python2 –  Jan 24 '17 at 14:51
  • 1
    Also, `pi` in the last `print()` statement is undefined `if "Invalid gender"` is `True` – Edwin van Mierlo Jan 24 '17 at 14:59
  • Possible duplicate of [input() error - NameError: name '...' is not defined](http://stackoverflow.com/questions/21122540/input-error-nameerror-name-is-not-defined) – Łukasz Rogalski Jan 24 '17 at 15:03
  • If you copied and pasted your code then take a look at the `#!` line. It should probably say `#!/usr/bin/python`, but the `u` is replaced by a space. – cdarke Jan 24 '17 at 15:09

2 Answers2

1

You are running your code on Python 2, not Python 3. In Python 2 input evaluates the string it reads as Python code (see this question).

If you will be using Python 2, replace input with raw_input.

If you want to use Python 3, it is a good idea to change your shebang to:

#!/usr/bin/python3
Community
  • 1
  • 1
kirelagin
  • 13,248
  • 2
  • 42
  • 57
0

You're not working on python 3 so instant of input you have to use raw_input , so this works perfectly:

heigth = float(input("Enter the height of the person: "))
sex = raw_input("Enter the person's gender:")

if(sex == "male"):
        pi = (72.7 * heigth) - 58
elif(sex=="female"):
        pi = (62.1 * heigth) - 44.7
else:
        print("Invalid gender")

print("The ideal weight for this person is:",pi)

If you have python 3 on your machine i suggest you to use : #!/usr/bin/python3 and then the code is the same like yours

Teshtek
  • 1,212
  • 1
  • 12
  • 20