0

I am having a spot of bother with the following script, it uses a function within it:

def my_function(arg1, arg2, arg3):
    print("I love listening to %s" %(arg1))
    print("It is normally played at %d BPM" %(arg2))
    print("I also like listening to %s" %(arg3))
    print("Dat be da bomb playa!")

print("What music do you like listening to?")
ans1 = input(">>")

print("What speed is that usually played at?")
ans2 = input(">>")

print("whats your second favourite genre of music?")
ans3 = input(">>")

my_function(ans1, ans2, ans3)

I get to the first user input: What music do you like listening to? When I type in 'House' and hit enter, I am expecting the second question to be displayed, but I get the following error message:

Traceback (most recent call last): 
File "ex19_MyFunction.py", line 26, in <module> ans1 = input(">>") 
File "<string>", line 1, in <module>
NameError: name 'house' is not defined**

Any help would be appreciated!

timgeb
  • 76,762
  • 20
  • 123
  • 145

2 Answers2

1

If you use Python2 then use raw_input() because input() try to parse text as python code and you get error.

furas
  • 134,197
  • 12
  • 106
  • 148
  • Thanks furas. that did the trick. – SnakeInTheGrass Dec 21 '15 at 17:29
  • However I have a second issue...it is related to the functions 2nd argument "print("It is normally played at %d BPM" %(arg2))'....when the user is asked: "print("What speed is that usually played at?")", the input is in 'int' format so I know why the error is coming up (I think), because %d is a string formatter....what would be the correct code to write so that the argument accepts an 'int' input from the user? – SnakeInTheGrass Dec 21 '15 at 17:33
  • sorry dude - ignore me. I figured it out....I replaced %d with %s and it worked! – SnakeInTheGrass Dec 21 '15 at 17:37
1

Your script would run just fine in Python3, so I am assuming you are using Python2. Change input to raw_input.

In Python2, raw_input will convert the input to a string, while input('foo') is equivalent to eval(raw_input('foo')).

timgeb
  • 76,762
  • 20
  • 123
  • 145