Reputation: 135
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"
Upvotes: 1
Views: 219
Reputation: 26
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.
Upvotes: 1