Python Forum
Getting error trying to use functions to multiply numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting error trying to use functions to multiply numbers
#1
For some reason I get an error trying to multiply two numbers after returning each value through its respected function.

def get_user_input_1():
    user_input = input('Type a number: ')
    return user_input


def get_user_input_2():
    user_input = input('Type another number to multiply by your first number: ')
    return user_input


def calculate_result(x, y):
    if x or y != 'q':
        x = int(x)
        y = int(y)
    result = x * y
    print(result)


get_user_input_1()
get_user_input_2()
calculate_result(get_user_input_1, get_user_input_2)
Here is my error message:
Error:
Traceback (most recent call last): File "C:\Users\Vadim\Desktop\Anti Clog Folder\Coding Stuff\Python\Project1\test.py", line 21, in <module> calculate_result(get_user_input_1, get_user_input_2) File "C:\Users\Vadim\Desktop\Anti Clog Folder\Coding Stuff\Python\Project1\test.py", line 13, in calculate_result x = int(x) TypeError: int() argument must be a string, a bytes-like object or a number, not 'function' Process finished with exit code 1
I also get this as a highlighted warning in PyCharm referring to the first argument in "calculate_result".
Error:
Expected type '{__mul__}', got '() -> str' instead
Reply
#2
this is incorrect
if x or y != 'q'
should be like this
if x != 'q' or y != 'q'
or
if 'q' not in (x, y) # also correct
Don’t treat the code as a human language
Reply
#3
your code is incorrect
def get_user_input_1():
    user_input = input('Type a number: ')
    return user_input # here got return
 
 
def get_user_input_2():
    user_input = input('Type another number to multiply by your first number: ')
    return user_input # here also got return
 
 
def calculate_result(x, y):
    if x or y != 'q':
        x = int(x)
        y = int(y)
    result = x * y
    print(result)
 
 
get_user_input_1() # Where is the return value?
get_user_input_2() # here also
calculate_result(get_user_input_1, get_user_input_2) # get_user_input_1 and get_user_input_2 is function not variable
should be
def get_user_input_1():
    user_input = input('Type a number: ')
    return user_input
 
 
def get_user_input_2():
    user_input = input('Type another number to multiply by your first number: ')
    return user_input
 
 
def calculate_result(x, y):
    if 'q' not in (x or y):
        x = int(x)
        y = int(y)
    result = x * y
    print(result)
 
 
x = get_user_input_1()
y = get_user_input_2()
calculate_result(x, y)
Reply
#4
I see; thank you for the help.

Question 1:
Is it possible to do this with only functions, without using any global variables whatsoever?

Question2:
If "get_user_input_1()" calls and executes the function, does "get_user_input_1" without the parentheses contain the return value, making it something like a variable? If not, why does python allow running functions without parentheses in the first place?

Thanks
Reply
#5
1. Well, yes, of course, just call the functions on line 21 so that their return values are passed to calculate_result

2. When you use the function name without parentheses, that refers to the function itself. It does not call the function. Being able to pass functions around to other functions is very useful. For example, when you want to do something when a button is pressed on a UI, you express the "something" as a function, but you don't call it - you give that function to the library code that handles the button press. It's again, all about parameterising the thing to be done. Other examples are when you want to operate on collections, e.g.

- doing the same thing to every item in a collection:

def square(x):
    return x ** 2

values = [1, 2, 3]
squared_values = map(square, values)
names = ["alice", "bob", "mary"]
capitalised_names = map(lambda name: name.title(), names)
- keeping items in a collection that satisfy a condition

def even(x):
    return x % 2 == 0

values = [3, 2, 4, 7, 20, 16]
evens_only = filter(even, values)
names = ["Alice", "Mary", "Bob", "Andrew"]
names_starting_with_a_only = filter(lambda name: name.startswith("A"), names)
Reply
#6
Question 1
You could pass the function that return user input directly into the calculate function
calculate_result(get_user_input_1(), get_user_input_2())
note as your get_user_input functions are basically the same apart from the string that the user sees(also the numbering 1 & 2 is usually a telltale sign)
these should be changed into one function that takes the string as input.
def user_input(prompt):
    return input(prompt)
you can create constants for the user input prompts
PROMPT_1 = "Type a number: "
PROMPT_2 = "Type another number to multiply by your first number: "
and then the calculate_result call would be
calculate_result(user_input(PROMPT_1), user_input(PROMPT_2))
Then you see that your function is not actually adding anything to the built-in input
so you could remove the functions and just use
calculate_result(input(PROMPT_1), input(PROMPT_2))
Note you would also likely want to add to the user_input function to say validate the input and then the function would be worth having.

Question 2
When you don't use parentheses a function is not run, this is useful because then a function can be passed around to other functions and then called at another point.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error : "can't multiply sequence by non-int of type 'float' " Ala 3 3,085 Apr-13-2021, 10:33 AM
Last Post: deanhystad
  I want to multiply two variables, but I get an error LeqendFire 3 2,138 Aug-09-2020, 03:52 PM
Last Post: LeqendFire
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,739 May-09-2019, 12:19 PM
Last Post: Pleiades

Forum Jump:

User Panel Messages

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