Is there are way to reuse a function with different values in a code?

Anonymous

I am wondering if there is a way to repeat a function with different values in the code

I've asked my teacher, searched online, asked some peers and none have the answer

while cashChoice > 0:
    cash1 = 200 + int(offer1)*50
    cashorBox = input("Would you like the money or the box? You have been offered $" + str(cash1) + ". If you want money, press [m], if you want the box, press [b]")
    if cashorBox == "m":
        print("Congratulations, you get " + str(cash1) + "! The prize you could've won from the box was " + str(userBox) + ". See you!")
        sys.exit
    elif cashorBox == "b":
        print("Ok... you will be offered another cash amount.")
        cashChoice -= cashChoice
    if cashChoice == 0:
        print("Great! You may have the box. It contains " + str(userBox) + "! Farewell :)")
        sys.exit
    else:
        continue

I want it to repeat but with different values of "cash1" as in "cash2"

Nick Andry

There is a very simple answer for this, and it will be very important to know later on if you wish to become proficient in Python. I don't know exactly how you are wanting to implement this, but you can do it like so:

def yourFunction (cash1Param):
    while cashChoice > 0:
        cash1 = cash1Param
        cashorBox = input("Would you like the money or the box? You have been offered $" + str(cash1) + ". If you want money, press [m], if you want the box, press [b]")
        if cashorBox == "m":
            print("Congratulations, you get " + str(cash1) + "! The prize you could've won from the box was " + str(userBox) + ". See you!")
            sys.exit
        elif cashorBox == "b":
            print("Ok... you will be offered another cash amount.")
            cashChoice -= cashChoice
        if cashChoice == 0:
            print("Great! You may have the box. It contains " + str(userBox) + "! Farewell :)")
            sys.exit
        else:
            continue

Then, when you call the function, you can enter any value for cash1Param, such as:

yourFunction(5)

or

yourFunction(678678967)

You don't even need to use cash1 for everything. You could just replace all of the times you have used it with cash1Param to directly use the parameter.

These are called function parameters. Here is a relevant link for learning them: https://www.protechtraining.com/content/python_fundamentals_tutorial-functions

If you have any further questions, don't be afraid to ask.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I reuse a function for many different ID values?

reuse function with different variables

Code reuse with different event methods

Is there a way to reuse builder code for retrofit

How to reuse function in different files?

what is the efficient way to reuse async function from different file location in nodejs 4.x?

How to reuse code in an R function?

Is there a way to reuse a CSS style but with different background images?

Is there a way to reuse for loops with different end arguments?

Reuse SVG elements with different path values

What way could I add score with different values to my code?

How to reuse a code which is slightly different

Reuse Tkinter code, within different windows

php code reuse. Is there a better way to do it?

Is there a way a to reuse mongodb/mongose aggregation code?

How to reuse this ViewController code in an object oriented way?

Reuse function in two different controllers in angularJS

How to reuse a JS function on different HTML elements

Is there a way to reuse a function for multiple structs in golang?

reuse function with multiple string values pandas

Uncaught TypeError: undefined is not a function (Code reuse scenario)

JavaFx different way of code

Is there a way to transfer 'this' to a different function?

What is the proper way to reuse DbContext across different projects in a single solution?

Reuse code in C++ functions that access different attributes of struct

using interface and composition in java - reuse code with different variables

How to reuse the code if only the base class is different in C++?

How to reuse native query code for different tables and views?

Is there a way to do inheritance or code reuse in an AWS CloudFormation template?