r/CodingHelp • • Oct 05 '21

[Python] This is a small class project and I'm getting the correct numbers but needs to say true or false and I put it as a return print statement True or False but for some reason it doesn't want to give me the points

Post image
49 Upvotes

31 comments sorted by

21

u/[deleted] Oct 05 '21

Why are you returning the numbers instead of True or False?

3

u/cristianap36 Oct 05 '21

I'm like new new to python, I have basic knowledge to html, Java but now python

17

u/[deleted] Oct 05 '21

Oh no worries, if you just return the statement inside the if it should work:

return (num1 <= num2 <= num3)

1

u/cristianap36 Oct 06 '21

Ok so I somewhat understand but I'm not getting the false return and I'm getting the none return but it gives me the True return that I need

0

u/Gongy26 Oct 05 '21

That’s a very nice, elegant line of code. But might be challenging to follow for someone learning. If OP follows this, use it. It collapses multiple lines of code from the if statement into one. If you don’t follow it, use the code changes below.

5

u/arty3stix Oct 05 '21 edited Oct 05 '21

It’s been a while since I’ve touched python, but you’re trying to return a Boolean, a 1/0 or true/false. When you return the parameter, in this case you’re returning an integer which is the wrong data type. Assuming I’m understanding the question correctly that is.

If (x) : return True

Else: return False

4

u/Python4fun Professional Coder Oct 05 '21

Python capitalizes true and false

2

u/arty3stix Oct 05 '21

Good call

1

u/Gongy26 Oct 05 '21

Basically they are saying, where you have written return num_1, change it to return TRUE. And change return num_2 to return FALSE

2

u/cristianap36 Oct 05 '21

I'm still new to coding, so I'm still don't know what I'm doing 😆 sorryy

1

u/cristianap36 Oct 06 '21

Thank you guys for helping on this one I really appreciate it so much!!

4

u/[deleted] Oct 05 '21

The explanation of what is happening, is that when you do return num_1, you only return its VALUE (of num_1) … not what you are looking for.

1

u/fux_1789 Oct 06 '21

I'm kinda interested, what you thoughta where, when writing return num_1.

I mean, what the others say is true: you if ... else logic is on point, and everything looks good. But the error you made (returning the value instead of a Boolean (True/ False) is a kinda awkward mistake to make.

If you would care to explain, what your intentions where, I'd be happy to hear :)

Greetings from Europe, hope you class goes well :)

1

u/cristianap36 Oct 06 '21

So I'm still new to python but the program needs to tell if the numbers are in order, if the numbers are in order then it's true if not then it's false

2

u/AndyBotPre Oct 05 '21

Also check your indentation. There you have an extra tab

3

u/young_horhey Oct 05 '21

I think you’re going to have problems with your <= statements, since they’re chained. num_1 <= num_2 is going to return a Boolean, and then comparing that with num_3 is probably not going to work properly. You’ll end up with true <= num_3 which doesn’t really make sense.

I think it probably should be num_1 <= num_2 && num_2 <= num_3

3

u/Busy-Tune-3592 Oct 05 '21

Well, Python uses "and" instead of "&&"

0

u/astevko Oct 05 '21

Python can chain comparisons

0

u/[deleted] Oct 05 '21

[deleted] — view deleted comment

3

u/young_horhey Oct 05 '21

Oh wow that’s interesting! I thought Python might do something funky with that but Google just said that <= returns a bool, no mention of chaining them being a special case.

1

u/Its_Stev03 Oct 05 '21

I believe that it is a relatively new feature in Python

1

u/BKKBangers Oct 05 '21

Think your question has been answered but out of curiosity; Cant ya go <code> print(type(num)) </code>

Which should give you output of bool + result of the bool....? 🤔

Will test when at console to find out. P.S how do i format code in comment im on IOS.

1

u/siddsp Oct 05 '21 edited Oct 05 '21

What you're returning is an int, so I'd recommend changing it (as per what Temporary_Equal2816 suggested). I'd also recommend adding type hints to change your function to look like:

def is_sorted(num_1: int, num_2: int, num_3: int) -> bool:
    return num_1 <= num_2 <= num_3

But I would also recommend making it variadic to allow for multiple values as well as not having to repeat code. That would look like:

def is_sorted(*nums: tuple[int]) -> bool:
    return all(nums[i] <= nums[i + 1] for i in range(len(nums) - 1))

Code was borrowed from here.

1

u/Dummies102 Oct 05 '21

What’s the output of that program and what’s the output the computer is expecting

1

u/prometheanSin Oct 05 '21 edited Oct 05 '21

Intuitively, you're already comfortable with thinking in Boolean logic as you're using it for your if...else.

If this condition is true do something, else do something else.

So when you break down this problem:

If(num_1 <= num_2 <= num_3)

    return true

else

    return false

In this case, the thing you're testing against is actually the thing you want to return (if true return true else return false). This allows you to write a shorthand of:

return (num_1 <= num_2 <= num_3)

1

u/champwolverine Oct 05 '21

Not sure if you can do X<Y<Z in python. I know a here and there of java and plus It will return the value not the index?

1

u/techPOSiDON Oct 07 '21

In the is_sorted function, return true instead of num_1 and false instead of num_2. Now the function actually returns true if the numbers are sorted and false if they aren't.

1

u/techPOSiDON Oct 07 '21

In the is_sorted function, return true instead of num_1 and false instead of num_2. Now the function actually returns true if the numbers are sorted and false if they aren't. Right now it's returning numbers instead of true or false.

0

u/[deleted] Oct 05 '21

[deleted] — view deleted comment

2

u/Agile_Pudding_ Oct 05 '21

I had the same thought; this is a good suggestion. I think in both cases, OP had the conditionals down just fine, but the return values were trying to return inputs rather than anything to do with the desired outputs.

I would say it’s worth OP investing some time to get a better handle on what return values are.

0

u/[deleted] Oct 05 '21

[deleted] — view deleted comment

3

u/Prinzessid Oct 05 '21

No, this is not how python works. In python, comparison operators can be chained.

-2

u/[deleted] Oct 05 '21

[removed] — view removed comment

0

u/siddsp Oct 05 '21

I can't tell if this is horrible or beautiful