有没有办法在代码中重用具有不同值的函数?

匿名

我想知道是否有一种方法可以在代码中重复一个具有不同值的函数

我问过我的老师,在网上搜索过,问过一些同龄人,没有一个答案

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

我希望它重复,但是“ cash1”的值与“ cash2”中的值不同

尼克·安德里

有一个非常简单的答案,以后知道是否希望精通Python至关重要。我不知道您到底想如何实现它,但是您可以这样做:

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

然后,当您调用该函数时,可以为cash1Param输入任何值,例如:

yourFunction(5)

要么

yourFunction(678678967)

您甚至不需要为所有内容使用cash1。您可以将所有使用它的时间替换为cash1Param以直接使用该参数。

这些称为功能参数。这是用于学习它们的相关链接:https : //www.protechtraining.com/content/python_fundamentals_tutorial-functions

如果您还有其他问题,请不要害怕问。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

有没有办法使HTML可重用块?

有没有办法在Rust代码中使用unistd.h中的函数?

有没有办法区分具有相同值的不同`Rc`?

有没有办法重用Job实例?

有没有办法使用具有相同名称但返回类型不同的函数?

有没有办法在底层范围内重用if(...)条件的值?

有没有办法在C ++中复合函数?

有没有办法在ListView中调用函数?

有没有办法从DOM中获得选项的不同值?

在右值和左值引用中创建函数时,有没有办法避免重复的代码?

有没有办法在HTML中多次使用同一代码(代码可重用性)?

有没有办法重用聚合步骤?

有没有办法调用具有非pythonic参数的cdef类方法?

有没有办法从两个不同的数组中删除相同的值?

有没有办法在Dreamweaver中复制代码?

有没有办法使用具有多个条件的excel VLOOKUP?

Java:有没有办法在枚举中获取具有不同返回类型的方法?

有没有办法从具有不同属性的表中选择记录?

有没有办法在比较两列时使用具有 nvarchar 数据类型的 Case 函数?

有没有办法可以重用 EDN 中定义的键的值?

有没有办法重用具有不同结束参数的循环?

有没有办法在 Rust 中实现具有不同泛型约束的相同结构的特征?

SQL 中的 STUFF(...) 函数有没有办法防止重复值?

有没有办法在 postgres 中创建具有值的 CTE?

我有重复的代码 - 有没有办法扩展/重用 terraform 模块?

有没有办法在 R 中制作具有不同变量对的 2 路表?

有没有办法将值存储在一个具有多个可选值的字典键中?

有没有办法在源代码中查看 Scipy.Special.Gamma() 函数实现?

有没有办法以角度调用html属性值中的函数?