Regarding If statement loop


#1

I am trying to run the following code but it's showing error.

#If loop condition
x = raw_input('please enter number in between 1 & 10')
def the_flying_circus(x):
    if x>3 and x<10:
        print "Value is in between 3 and 10"
    elif x<=3 and x>=1:
        print "value is in between 1 and 3"
    else:
        print"Please enter the value in between 1 and 10"
        return x

The code isn't working. Can anyone please help me out with it.

Thanks,
Tarun


#2

is this 15. big if?

your function can't take any parameters:

def the_flying_circus():

simply move the raw_input inside the function


#3

Hi Stetim94,
Yes it is. I was just trying to write a code of my own after "Big if" exercise but it was not working. I tried doing in the way you said but it didn't work . Can you explain me regarding it in detail .

Thanks,
Tarun


#4

please show an updated version of your code


#5

def the_flying_circus():
    x = raw_input('please enter number in between 1 & 10')
        if x>3 and x<10:
            print "Value is in between 3 and 10"
        elif x<=3 and x>=1:
            print "value is in between 1 and 3"
        else:
            print"Please enter the value in between 1 and 10"
        return x

Output:
File "python", line 3
if x>3 and x<10:
^
IndentationError: unexpected indent


#6

why do this two lines:

    x = raw_input('please enter number in between 1 & 10')
        if x>3 and x<10:

not have the same indent? they should have the same indent


#7

Sorry I didn't get what you are trying to say.


#8

they have different indent(ion)/spacing, while they should have the same amount of indent


#9

def the_flying_circus():
    x = raw_input('please enter number in between 1 & 10')
    if x>3 and x<10:
        print "Value is in between 3 and 10"
    elif x<=3 and x>=1:
        print "value is in between 1 and 3"
    else:
        print"Please enter the value in between 1 and 10"
        return x

Still not working.


#10

your function needs to return True


#11

... also, when Codecademy calls your function to test it, there will not be an opportunity to enter a value. Therefore, the function will likely fail if depends upon the result of an internal raw_input call.

You can get it to pass, however, by placing the line that calls raw_input outside the function. You will have to modify the line, however, to convert the user's input to an int, as follows ...

x = int(raw_input('please enter number in between 1 & 10'))

Make sure that whatever conditional block will execute abides by @stetim94 's advice to have the function return True.

Even though relying upon user input can work here, if performed strategically, there is something a bit sketchy about depending upon x as a global variable that stores the user input in order to pass Codecademy's test. The issue is that Codecademy's test will call the the_flying_circus function, but will not execute the external line that gets the user input. We might expect that this will leave x undefined during the test. However, due to the manner in which Codecademy handles memory during an exercise, it will have x defined based on the value that you have entered after submitting your code.


#12

Thanks applypye, I got it.

def the_flying_circus(): # defining function
x = int(raw_input('please enter number in between 1 & 10')) # giving input
if x>5 and x<10: # conditional loop start
print "Value is in between 5 and 10"
print "The value entered is %s " % x
elif x<=5 and x>=1:
print "value is in between 1 and 5"
print "The value entered is %s " % x
else:
print "Please enter the value in between 1 and 10"
print "Please enter the value in between 1 and 10"
return x

y= the_flying_circus()
print y


#13

Hi, @terminator6102 ,

Did it pass Codecademy's test?

The Python track is my favorite material on Codecademy, but The Big If is ... well ... not my favorite exercise within the Python track.

Codecademy is asking the user to write a function

  • with no parameters
  • within which it is not easy to include statements that successfully collect user input

In response, the quickest thing to do is to submit a solution that includes code of this ilk ...

if (7 > 6):
    return True

Conditions that compare constants are not the sort of stuff that makes for a very interesting or useful function.

However, a few users, such as yourself, demonstrate determination to get more creative, and include such features as user input :sunglasses: (cool) or pseudorandom numbers.


#14