[Tutor] Inputting variables into functions containing if, elif, else.

ThreeBlindQuarks threesomequarks at proton.me
Mon Jun 26 10:54:35 EDT 2023


Jack,

Maybe you can explain things better.

I assume the phrase "test out" means you want to try various test cases to see if a function operates as expected under various inputs.

So where is your function? You are not showing a function but snippets of code that could be embedded in a function. If you want to test the code the way you have it, then when asking us, don't call it a FUNCTION, please. 

If you just want to test your logic, then you can keep changing the current value of "number" and run the remaining lines and see what pops out.

If you had an actual function such as do_it(numb) that you can call it repeatedy, you could call do_it(0) then do_it(666) and do_it("This is not a number") and so on.

And there are ways to automate the process by automatically calling the function in multiple ways and comparing it to the expected result and perhaps just reporting how many tests passed or failed but those are outside what you seem to be working on.

Can I ask you about the code you showed and whether it is organized the way you want?

It looks like you are expecting a number and want to partition it into 4 categories.

The first is less than or equal to 5 which I note includes possibly negative numbers. Are they definitely integers? Do you care about 0 or negative numbers?

The second jumps to test for exactly 33. Nothing wrong with that but not the order of testing I might have chosen.

The third jumps back to test is the number is at least six but strictly less than 32. I suggest you ask yourself what if it is exactly 32. And look at your print statement and note is does NOT say what the code does. it says GREATER than 6 while the code says >= 6.

The final condition catches anything not caught by the others. It looks to me like it would find not only any number greater than 33 but also any 32 and for that matter any floating point numbers between 32 and 33 such as 32.5.

There is no one way to write most code. What some people might do is strive for efficiency by making the most commonly expected test cases go first so the remaining IF statements rarely get evaluated.

Others may use some logical progression like this, assuming you have an integer and there are no gaps allowed:

number <= 5
number <= 32
number == 33
else ...




Sent with Proton Mail secure email.

------- Original Message -------
On Monday, June 26th, 2023 at 7:11 AM, Jack Simpson <simpsonrmz41 at gmail.com> wrote:


> Hi,
> 
> I was hoping to gain some knowledge on how I can test out a function.
> Should I have used the return function somewhere or have I totally missed a
> step?
> Or have I not defined a variable properly?
> 
> number = 25
> 
> 
> if number <= 5:
> print("The number is 5 or smaller.")
> elif number == 33:
> print("The number is 33.")
> elif number < 32 and number >= 6:
> 
> print("The number is less than 32 and greater than 6.")
> else:
> print("The number is " + str(number))
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list