-1
n = int(input())

for x in (range(1,n))%2!=0:

    if x % 3 == 0 and x % 5 == 0:
        print("SoloLearn")
    elif x % 3 == 0:
        print("Solo")
    elif x % 5 == 0:
        print("Learn")

It gives this error---TypeError: unsupported operand type(s) for %: 'range' and 'int' why can i not divide 2 by range of numbers?

Selcuk
  • 57,004
  • 12
  • 102
  • 110
Musa Khan
  • 1
  • 2
  • *for x in (range(1,n))%2!=0*? –  Sep 01 '21 at 06:07
  • 2
    If you want the odd numbers, `for x in (range(1, n, 2)` would do it. – tdelaney Sep 01 '21 at 06:09
  • Welcome to Stack Overflow. In your own words, *what do you expect this code to mean*? Why do you think you *should* be able to "divide 2 by a range of numbers" (it appears that you actually mean "divide a range of numbers by 2")? What do you think should be the result when you do that? Why? – Karl Knechtel Sep 01 '21 at 06:09
  • Should it also be possible to do this kind of math with other things? Why? Which kinds of things? According to what logic? – Karl Knechtel Sep 01 '21 at 06:10
  • @user1740577 - because (1) I wrote it as you posted and (2) its not really an answer to the question " why can i not divide 2 by range of numbers?" but a guess about the intent of the for loop. – tdelaney Sep 01 '21 at 06:20

3 Answers3

1

you want odd numbers try this:

for x in (range(1,n,2)):

instead of:

for x in (range(1,n))%2!=0:
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
  • 2
    Giving just code is not a good way to tell OP want went wrong. Please elaborate –  Sep 01 '21 at 06:09
  • Why is ```for x in (range(1,n))%2!=0:``` wrong? By the way, I didn't down vote. –  Sep 01 '21 at 06:11
  • @Sujay i write that OP you want odd number can try this answer – I'mahdi Sep 01 '21 at 06:12
  • 1
    But you need to explain what is wrong (if possible), just giving the right answer is not enough. SO is not a code-writing service. You need to show where they are wrong (and provide a better alternative), so that they can learn. – PCM Sep 01 '21 at 06:25
0

Python objects decide for themselves what operations mean by implementing magic methods - see Emulating Numeric Types. range(1,n) % 2 translates to calling range(1,n).__mod__. But that method isn't implemented for range objects because the authors didn't think it made sense to modulo an integer generator. Remember, this is an operation on the generator object, not the integers iterated by the generator.

If you really wanted to divide a range of numbers by 2, you could do it as the numbers are generated

>>> for x in (y % 2 for y in range(10)):
...     print(x)
tdelaney
  • 73,364
  • 6
  • 83
  • 116
-1

Are you trying to create a list of odd numbers? YOu could try:

for x in [num for num in range(1,n) if num x%2]:
    #do_stuff

note that you can lose the !=0 part of the check because non-zero integers equate to True

As noted by Sujay, there's a quicker way to do it that hides the underlying mechanism. The example I gave you can be modified to use more complex checks.

ALSO! Note that you don't really want to do this. You're solving fizzbuzz, you need to iterate over all numbers. Number 30 is even and you want to print 'sololearn' ('fizzbuzz') when you reach it.

Zla Patka
  • 109
  • 4
  • When you can just do ```range(1,n,2)```, why to create a new list? –  Sep 01 '21 at 06:10
  • Just to illustrate the underlying mechanism. Adding the step to range is, of course, a smoother way to do it – Zla Patka Sep 01 '21 at 06:12