1

I keep getting an error when trying to run my code:

Traceback (most recent call last):
  File "E:\healthtest.py", line 3, in <module>
    if raw_input() == "Hit":
NameError: name 'raw_input' is not defined   

I do not know what I did wrong, but here is the code I have been using.

health = "10"

    if raw_input() == "Hit":
    health = health - 5

I hope you can help me, and thanks in advance.

Matthew
  • 99
  • 1
  • 2
  • 10
  • [**`raw_input`**](https://docs.python.org/2/library/functions.html#raw_input) is Python 2.7. – Peter Wood Nov 06 '15 at 14:53
  • 4
    Possible duplicate of [How do I use raw\_input in Python 3.1](http://stackoverflow.com/questions/954834/how-do-i-use-raw-input-in-python-3-1) – Peter Wood Nov 06 '15 at 14:55

2 Answers2

2

It is raw_input() and not raw_input

Unless you are using Python 3.x, where it has been renamed to input()

Also, regarding health, you are first assigning it a string value and then trying to take away -5 as if it's an int().

Andrés Pérez-Albela H.
  • 4,003
  • 1
  • 18
  • 29
0

Make sure you're using input() and indenting properly

health = 10 # health is an integer not a string

if input()=='hit':
    health -= 5 # pay attention to the indentation


hit # my input

health
Out[34]: 5 # output of health after input

You can take this further my assigning input() to a var to do more validation.

inp = input()

if inp.lower()=='hit':
    # continue code

The .lower() allows input such as 'hit', 'Hit', 'HIT'...

'Hit' == 'hit'
Out[35]: False

'Hit'.lower() == 'hit'
Out[36]: True
Leb
  • 15,483
  • 10
  • 56
  • 75