How do I make decisions using executable functions with data from an input value?

Returning the value seems to end the block entirely and just makes it the displayed result instead of allowing the function to use it in decision making. Here is a simplified example of my code:
my_number = input("Type a number: ")

def big_number(number):
return number
if number > 100:
print(f"{number} is a big number!")
else:
print(f"{number} is not a big number.")
print(big_number(my_number))

Is there a reason to not put the “return” at the end?

def big_number(number):
    if number > 100:
        print(f"{number} is a big number!")
    else:
        print(f"{number} is not a big number.")
    return number

That would be more normal.

Cheers,
Cameron Simpson cs@cskk.id.au

With that I get the following error:
Traceback (most recent call last):
File “C:\Users\admin\OneDrive\Documents\My Python Programs\Experiment\Function Experiment.py”, line 15, in
print(big_number(my_number))
File “C:\Users\admin\OneDrive\Documents\My Python Programs\Experiment\Function Experiment.py”, line 10, in big_number
if number > 100:
TypeError: ‘>’ not supported between instances of ‘str’ and ‘int’

And if I put return at the beginning and the end it just gives me the number I put in.

I got something similar to work with this code:
what_is_big_number = input(“what is the smallest big number?”)
my_number = input("Type a number: ")

def big_number(number, number_big):
if number >= number_big:
print(f"{number} is a big number!")
elif number < number_big:
print(f"{number} is not a big number.")

big_number(my_number, what_is_big_number)

but for some reason this does not work the way I want it to:
big_number_2 = input(“Type a number: “)
number_2 = 100
def big_number_also(number_sec, number_big_sec):
if number_sec >= number_big_sec:
print(f”{number_sec} is a big number!”)
elif number_sec < number_big_sec:
print(f"{number_sec} is not a big number.")

big_number_also(big_number_2, number_2)

it gives me this error:
Traceback (most recent call last):
File “C:\Users\admin\OneDrive\Documents\My Python Programs\Experiment\Function Experiment.py”, line 31, in
big_number_also(big_number_2, number_2)
File “C:\Users\admin\OneDrive\Documents\My Python Programs\Experiment\Function Experiment.py”, line 26, in big_number_also
if number_sec >= number_big_sec:
TypeError: ‘>=’ not supported between instances of ‘str’ and ‘int’

So the problem is the values not being the EXACT same type to start, is there a way around this?

I got something similar to work with this code:
what_is_big_number = input(“what is the smallest big number?”)
my_number = input("Type a number: ")

The input function returns a string. You need to convert that to a
number. If you’re working with ints, do this:

my_number_s = input("Type a number: ")
my_number = int(my_number_s)

[…]

So the problem is the values not being the EXACT same type to start, is
there a way around this?

Yes. Make them the same type, as above. Otherwise you’re going “1” > 100
instead of 1 > 100.

When you type things, you’re just typing text. input() returns what you
typed, so a string. The int() constructor accepts a string and returns
an int.

Cheers,
Cameron Simpson cs@cskk.id.au

With that I get the following error:
Traceback (most recent call last):
File “C:\Users\admin\OneDrive\Documents\My Python Programs\Experiment\Function Experiment.py”, line 15, in
print(big_number(my_number))
File “C:\Users\admin\OneDrive\Documents\My Python Programs\Experiment\Function Experiment.py”, line 10, in big_number
if number > 100:
TypeError: ‘>’ not supported between instances of ‘str’ and ‘int’

See my other post: you need to convert the string you get from input()
into a number.

And if I put return at the beginning and the end it just gives me the
number I put in.

That is because the return exits the function immediately. So it does
not run the comparison above which explodes.

Cheers,
Cameron Simpson cs@cskk.id.au

Hi Eric,

Do you understand what the return statement does? It means to return
from inside the function back to the caller. The means it exits the
current function, and any code after it will never be executed.

# The called function.
def func():
    print("First")
    print("Second")
    return "Hello World!"
    print("This line of code will never run.")
    print("Neither will this.")

# The caller calls the function.
result = func()

Can you predict what that code will do? Try to guess before
reading on or running it:

  • what will be printed?

  • what will the variable “result” get set to?

Answers below.

S

P

O

I

L

E

R

S

P

A

C

E

The first two print lines will be run, so “First” and “Second” get
printed. The variable “result” gets set to “Hello World”.

As for the error you are getting:

TypeError: ‘>’ not supported between instances of ‘str’ and ‘int’

Python does not consider strings and numbers to be the same.

a_string = "One Two Three Four"

also_a_string = "1234"

a_number = 1234  # no quotation marks

Python doesn’t allow you to compare strings and numbers directly. You
need to convert them to a common type:

if also_a_string == str(a_number):
    ...

if int(also_a_string) == a_number:
    ...

Thank you so much for the help, I didn’t know I could force an integer like that. I looked but couldn’t find anything because I was looking for a way to force the input to be an integer. Here is the finalized code:

number_2 = [input("Type a number: "), 100]

number_2_mine = int(number_2[0])

def big_number_also(number_sec, number_big_sec):
if number_sec >= number_big_sec:
print(f"{number_sec} is a big number!")
elif number_sec < number_big_sec:
print(f"{number_sec} is not a big number.")

big_number_also(number_2_mine, number_2[1])

Thank you so much for the help, I didn’t know I could force an integer
like that.

No worries.

It isn’t a “force”. The int() constructor accepts a str as a valid
argument. But if you give it a bad string, it won’t “force” it to be an
int anyway, it will fail instead:

int("not a number")

number_2 = [input("Type a number: "), 100]

Why make a list here instead of just using 2 variables?

number_2_mine = int(number_2[0])

def big_number_also(number_sec, number_big_sec):
if number_sec >= number_big_sec:
print(f"{number_sec} is a big number!“)
elif number_sec < number_big_sec:
print(f”{number_sec} is not a big number.")

Since failing “number_sec >= number_big_sec” implies that “number_sec <
number_big_sec” your “elif” line should just be “else:”.

It will have the same meaning, but by putting a condition in the elif
you’re forcing the reader to look at that condition to determine when it
fires. With an “else:” the reader knows it always fires if the
previous condition (in the “if”) failed.

The natural supposition with “if… elif …” is that there are 3
possible outcomes: the first for the “if”, a second for the “elif” and a
third for when neither applies.

big_number_also(number_2_mine, number_2[1])

If you use 2 variables such as “number_2_s” and “threshold” you don’t
need to index number_2 - the expressions “number_2[1]” and earlier
“number_2[0]” are not obviously a threshold value. Whereas you could
just name it “threshold” or something similar.

In a programme this small the issue is small, but in a larger programme
your “def big_number_also” might well be far from where you call it, so
having a good name for the values conveys more meaning to someone
reading the code (even you, some time later).

Cheers,
Cameron Simpson cs@cskk.id.au

Cameron,
sorry. I just got confused with some stuff experimenting and forgot to change it back.
Thanks again, Erik