Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Else if
#1
I don't understand what the (answer), (4), (5), or (6) they want.

def greater_less_equal_5(answer):
    if 2 + 2:
        return 1
    elif 6 - 1:          
        return -1
    else:
        return 0
        
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)
On line 2, fill in the if statement to check if answer is greater than 5.
On line 4, fill in the elif so that the function outputs -1 if answer is less than 5

Those are the instructions but I am confused because I don't know if they want line 2 to print out 4 what conditional I need to use to get the code to return -1. I know the greater, less than equal to permutations but do i need the logical operators in if/elif code?
Reply
#2
The way I read it, if you parameter is 4 (or less), they want it to return -1; if the parameter is 5, they want it to return 0, and if the parameter is 6 (or greater) they want it to return -1.

And yes, you would need to put the logical operators in the if and elif statements.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Sorry. That was what I had changed the code to
def greater_less_equal_5(answer):
    if ________:
        return 1
    elif ________:          
        return -1
    else:
        return 0
        
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)
This is the real code and what they are asking me to do.
On line 2, fill in the if statement to check if answer is greater than 5.
On line 4, fill in the elif so that the function outputs -1 if answer is less than 5.
I will try it with the logical operators and see if that helps me. Thank you
Reply
#4
I would modify your code like this:

def greater_less_equal_5(answer):
   if answer > 5:
       return 1
   elif answer < 5:
       return -1
   else:
       return 0
You can test this out in Idle or other python environment.
Reply
#5
Thank you cygnus. I was making it more confusing than it was.
Reply
#6
No problem,glad to help.
Sometimes homework questions can be quite ambiguous.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020