3 Replies - 283 Views - Last Post: 01 July 2019 - 01:25 PM Rate Topic: -----

#1 kbeliza   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-June 19

Calling a function with 2 arguments

Posted 30 June 2019 - 06:32 PM

[print("Welcome to the Ribbon Store!")


name = input("Please enter your name: ")



def total_cost(numboffeet):

    numboffeet = int(input("How much ribbon do you want to buy? "))

    print("Okay, you want to install", numboffeet, "feet of ribbon")

    multipp = 0

    if numboffeet < 10:
        multipp = 0.50
    elif 10 < numboffeet < 25:
        multipp = 0.40
    elif 25 < numboffeet < 50:
        multipp = 0.30
    else:
        multipp = 0.20

    totcost = multipp * numboffeet

    print("The total cost of buying this amount of ribbon will be", totcost, "dollars")

total_cost(numboffeet)]

:code:


I am completely lost on the syntax of functions. I'm trying to call a function that has 2 parameters, the amount of feet and the price and then I pass them to the function. Am I on the right track?

This post has been edited by modi123_1: 30 June 2019 - 08:50 PM
Reason for edit:: In the future please use the [code] tag button in the editor


Is This A Good Question/Topic? 0
  • +

Replies To: Calling a function with 2 arguments

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,229
  • Joined: 12-December 12

Re: Calling a function with 2 arguments

Posted 01 July 2019 - 12:47 AM

Where is your attempt to provide two arguments?

What material are you studying?
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11651
  • View blog
  • Posts: 19,801
  • Joined: 19-March 11

Re: Calling a function with 2 arguments

Posted 01 July 2019 - 07:31 AM

Can you be more precise about your question? The syntax of functions is pretty straightforward: you define them with a def, whatever's indented under the def is the body of the function, you return something or a None is returned for you... pretty simple. Calling them, you use the name of the function, open parenthesis, args and kwargs, close paren, and at execution time that call is replaced by the return value of the function, whatever that might turn out to be.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7482
  • View blog
  • Posts: 15,508
  • Joined: 16-October 07

Re: Calling a function with 2 arguments

Posted 01 July 2019 - 01:25 PM

First, this fundamentally makes no sense:
def total_cost(numboffeet):
    numboffeet = int(input("How much ribbon do you want to buy? "))


Why are you passing a value name that you are both overwriting and not using? Which, in turn, is probably your issues with:
total_cost(numboffeet)


Because you haven't defined that variable name yet.

So, two birds with one stone:
def total_cost(numboffeet):
    # use the value passed
    print("Okay, you want to install", numboffeet, "feet of ribbon")

# ask the user for a value here
feet = int(input("How much ribbon do you want to buy? "))
# pass that value to a function
total_cost(feet)



Changing little more than the order of your code, and adding the new concept of return, you can do this:
def total_cost(numboffeet):
    multipp = 0
    if numboffeet < 10:
        multipp = 0.50
    elif 10 < numboffeet < 25:
        multipp = 0.40
    elif 25 < numboffeet < 50:
        multipp = 0.30
    else:
        multipp = 0.20
    # totcost = multipp * numboffeet
    return multipp * numboffeet

def main():
    print("Welcome to the Ribbon Store!")
    name = input("Please enter your name: ")
    numboffeet = int(input("How much ribbon do you want to buy? "))
    print("Okay, you want to install", numboffeet, "feet of ribbon")
    print("The total cost of buying this amount of ribbon will be", total_cost(numboffeet), "dollars")

main()



Or, perhaps:
def feet_multiplyer(feet):
    if feet < 10:
        return 0.50
    if feet < 25:
        return 0.40
    if feet < 50:
        return 0.30
    return 0.20

def total_cost(feet):
    return feet * feet_multiplyer(feet)



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1