user5166599
user5166599

Reputation:

Cannot find error in Python code

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.

Upvotes: 1

Views: 153

Answers (2)

Leb
Leb

Reputation: 15953

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

Upvotes: 0

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().

Upvotes: 2

Related Questions