【问题标题】:How to input string to function?如何输入字符串来运行?
【发布时间】:2019-12-08 23:38:54
【问题描述】:

以下是我收到的作业说明:

4) 在 Mylib 中添加如下函数

scalc(p1)
p1 will be a string like this "N1, N2, operator"
examples
scalc("20,30,*")
the result will be 600

scalc("50,20,+")
the result will be 70

scalc("50,20,-")
the result will be 30

scalc("60,20,/")
the result will be 30

使用字符串函数从输入字符串中解析第一个数字、第二个数字和运算符。 使用前面的函数(加、减、除和乘)进行计算。


这是我的尝试,但不起作用。我有 add() sub() mult() div() 函数,我只是不在这里展示它们。

我知道这很简单,可能与我调用函数 scalc(p1) 的位置有关。这样做的正确方法是什么?

def scalc(p1):
    astring = p1.split(",")
    num1 = float(astring[0])
    num2 = float(astring[1])
    if astring[3] == "+":
        add()
    elif astring[3] == "-":
        sub()
    elif astring[3] == "*":
        mult()
    elif astring[3] == "/":
        div()
    return num1, num2

p1 = input("Enter two numbers and an operator, each separated by a comma: ")
scalc(p1)

编辑:这是答案。我没有将参数传递给我的函数。通过将 num1 和 num2 添加到我的算术函数的每个实例中,它们能够接收新的变量值。

#Define the main program function
def main():
    #Define input function
    def float_input(msg):
        while True:
            try:
                return float(input(msg))
            except ValueError:
                print("You must enter a number!")
            else:
                break
    #Declare variables        
    rangeLower = float_input("Enter your Lower range: ")
    rangeHigher = float_input("Enter your Higher range: ")
    num1 = float_input("Enter your First number: ")
    num2 = float_input("Enter your Second number: ")

    #Define formula functions
    def add(num1, num2):
        sum = num1 + num2
        print("The Result of",num1,"+",num2,"=", sum)

    def sub(num1, num2): 
        diff = num1 - num2
        print("The Result of",num1,"-",num2,"=", diff)

    def mult(num1, num2):
        product = num1 * num2
        print("The Result of",num1,"*",num2,"=", product)

    def div(num1, num2):
        if num2 == 0:
            print("The Result of",num1,"/",num2,"= You cannot divide by Zero")
        else:
            quotient = num1 / num2
            print("The Result of",num1,"/",num2,"=", quotient)

    #If-else
    if num1 < rangeLower or num1 > rangeHigher or num2 < rangeLower or num2 > rangeHigher:
        print("The input values are outside the input ranges.")
        print("Please check the number and try again.")
        print("Thanks for using our calculator")
    else:
        #Call functions
        add(num1, num2)
        sub(num1, num2)
        mult(num1, num2)
        div(num1, num2)
        print("Thanks for using this calculator!")

    def scalc(p1):
        astring = p1.split(",")
        num1 = float(astring[0])
        num2 = float(astring[1])
        if astring[2] == "+":
            add(num1, num2)
        elif astring[2] == "-":
            sub(num1, num2)
        elif astring[2] == "*":
            mult(num1, num2)
        elif astring[2] == "/":
            div(num1, num2)
        return num1, num2

    p1 = input("Enter two numbers and an operator, each separated by a comma: ")
    scalc(p1)

【问题讨论】:

  • 您没有将任何值传递给您的函数(您应该在问题中至少包含一个值!),也没有捕获结果。 add() 应该是 result = add(num1, num2)

标签: python python-module


【解决方案1】:

这样做。有几个错误。首先,在 Python 中,您从 0 开始计数,因此您想使用 astring[2] 而不是 astring[3]。您还需要返回一个值:

def scalc(p1):
    astring = p1.split(",")
    print(astring)
    num1 = float(astring[0])
    num2 = float(astring[1])
    if astring[2] == "+":
        add(num1,num2)
    elif astring[2] == "-":
       sub(num1,num2)
    elif astring[2] == "*":
        mult(num1,num2)
    elif astring[2] == "/":
        div(num1,num2)
    return value

p1 = input("Enter two numbers and an operator, each separated by a comma: ")
scalc(p1)

例子:

input: "20,30,+"

Out[2]: 50.0

【讨论】:

  • 哦!好的,我看到我有 3 而不是 2。我修复了这个问题,但现在我仍然需要用新值替换原始代码中的 num1 和 num2。
  • 老实说,我什至不知道我们是否需要保留除公式函数之外的原始代码。我可以删除 num1 和 num2 最初给定值的其余代码,并使用我们给定的新函数。
  • 好的,所以你添加了加法、减法、除法、乘法的公式……但我已经在以前的作业中构建的函数中使用了这些公式。我们被告知要使用我们现有的功能。因此,我需要返回 num1 和 num2 而不是返回值,以便我的其他函数可以使用它们。我更新了原帖中的代码。
  • 所以,现在我将我的 3 更改为 2,代码运行;但是,调用我的公式函数之一会从先前的输入返回原始的 num1 和 num2。
  • 您的函数中缺少参数,您应该考虑添加它们,以便您的主函数正常运行。因为这是作业,我不应该进一步帮助你,但应该足以让你找到正确的方法
猜你喜欢
  • 1970-01-01
  • 2011-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-29
  • 1970-01-01
  • 2021-06-03
  • 1970-01-01
相关资源
最近更新 更多