-1

Python function that I just wrote is keep on looping up to a hundred. On the interpreter (or rather RUN), I am entering "5", but it executes the wrong "if" statement.

def range_v2(c):
    int(c)
    if c == 5:
        for x in range(1, 50, +5):
            print(x)
    else:
        for x in range(0, 100, +5):
            print(x)

y = input()
int(y)
range_v2(y)

2 Answers2

3

You are redefining your "c" variable in the function. Your function accepts an argument called "c" and then in the first line of the function you set the value of "c" to 0, so no matter what argument you pass to the function, it will always be evaluated for 0. Remove the line "c=0" and it should work how you expect.

BHawk
  • 2,382
  • 1
  • 16
  • 24
1

int(c) doesn't in-place convert c to an int if that's what you think it does. You'd have to do

c = int(c)
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218