1

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"

7
  • 1
    what do you want to achieve exactly? I don't even see cash2 defined in your code. Commented Mar 31, 2019 at 23:09
  • 2
    Do you mean function parameters? Commented Mar 31, 2019 at 23:09
  • Why can't you just define a function with cash1 and cash2 as input? Commented Mar 31, 2019 at 23:10
  • Are you trying to ask about arrays (called list in Python)? So you can have “variables” like cash[1], cash[2], cash[3], …? Commented Apr 1, 2019 at 2:18
  • 1
    @Anonymous: So did the accepted answer address your problem or not? Commented Apr 2, 2019 at 23:35

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

@DavisHerring Ah, thanks my man. Embarrassing that I missed that lol. Still new to the Stackoverflow formatting interface.
OK, now it gets UnboundLocalError. More importantly, does that while accomplish what the questioner wants (the acceptance notwithstanding)? Or is the goal to change from “cash1” to “cash2” over different iterations of that loop?
@DavisHerring 1/2 I am assuming the questioner only posted a snippet of his code and that cashChoice was defined earlier on in the hypothetical function, which explains the UnboundLocalError. I answered the question relative to how I interpreted it, which, admittedly, is not the best idea, as some further elaboration of this question may have been needed. While an accepted answer may not be completely indicative of me having answered what the questioner initially actually wanted, I guess we both can't know completely until he explains himself more in the comments. You are right. A list would
@DavisHerring 2/2 be the answer if he is wanting different values within the same loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.