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
#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


Messages In This Thread
RE: Getting error trying to use functions to multiply numbers - by Yoriz - Aug-30-2021, 07:11 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Error : "can't multiply sequence by non-int of type 'float' " Ala 3 3,147 Apr-13-2021, 10:33 AM
Last Post: deanhystad
  I want to multiply two variables, but I get an error LeqendFire 3 2,229 Aug-09-2020, 03:52 PM
Last Post: LeqendFire
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,817 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