我这里有一个示例函数:
def options(option1,option2):
if option1 == 'y':
print("Yay")
else:
print("No")
if option2 == 'y':
print("Cool")
else:
print("Stop")
然后,调用函数后,必须使用列出的所有必需参数。你知道吗
userInput = input("Type Y or N: ")
userInput2 = input("Type Y or N: ")
options(userInput,userInput2)
现在我的问题是:
我正在做一个基于文本的冒险游戏,用户可以选择选项1-4。我想有一个定义的方法,我将能够调用,无论有多少选项提供。在一个场景中,我可以给用户3个选项。另一方面,我可能只有1个。我怎样才能不必这样做:
#if there's 4 options in the scene call this method:
def options4(option1,option2,option3,option4):
blabla
#if there's 3 options in the scene call this method:
def options3(option1,option2,option3):
blabla
#if there's 2 options in the scene call this method:
def options2(option1,option2):
blabla
#if there's 1 option in the scene call this method:
def options1(option1):
blabla
我可以嵌套函数吗?你知道吗
创建一个这样的类。类可以使函数调用更简洁。我建议你这样做:
否则,您可以尝试使用字典,或者按照其他人的建议,创建一个列表
使用可选参数定义函数,例如:
现在您可以使用任意数量的参数来调用它,例如:
相关问题 更多 >
编程相关推荐