0

I'm a newbie. I want to put a condition on numbers. If I check one number, it works fine. But if I want it to return the same value for two differ)

for row in row2:
  if int ==  1: 
    return = "Music"

but I want to expand it further to include other numbers. So,

for row in row2:
  if int ==  1 , 2: 
    return = "Music"

I've also tried using or in the second option but it didn't work. Any help to check different integers to bring back a certain result would be great.

user28849
  • 71
  • 1
  • 5
  • 1
    Check this out [How do I test one variable against multiple values?](http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values) – Bhargav Rao Nov 07 '15 at 11:47
  • Don't try to use standard Python names like `int` and `return` as variables. In the case of `int` you will be masking the builtin definition, and in the case of `return` you will get a syntax error. Try to work with the language, as opposed to fighting it. – Tom Karzes Nov 07 '15 at 11:58

3 Answers3

2

To test multiple values:

if i in (1, 2):

Note that return is a keyword and you can't assign a value to it. int is a predefined identifier; you can assign a value to it, but you really don't want to; it will confuse things terribly.

Tom Zych
  • 13,329
  • 9
  • 36
  • 53
1

Do you mean

for row in row2:
    if int == 1 and 2: 
        return = "Music"

or

for row in row2:
    if int in (1 , 2): 
        return = "Music"

Also, int is a built-in function name. So don't use it as a variable name. And return = "Music" is invalid syntax because return is a keyword in Python. Maybe you mean return "Music".

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
1

Three mistakes in your code

  • int is a type, shouldn't use it as a variable name
  • The syntax for your if statement is wrong
  • You shouldn't use an = when returning

What you want is probably:

if row in (1,2):
    return "Music"

Or

if row == 1 or row == 2:
    return "Music"
Sebastian
  • 2,678
  • 25
  • 24