【问题标题】:Error with the function variable in my calculator program我的计算器程序中的函数变量出错
【发布时间】:2022-01-25 18:14:15
【问题描述】:

我是 python 新手,做了一个计算器程序作为练习。我有 java 方面的经验,所以我可以轻松起步,但即使我是一名 java 程序员,我仍然不熟悉变量声明和函数。所以,如果你能帮我解决这个错误,将不胜感激。

num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
oper = str(input("Enter the operator: "))
def my_func(answer):
    print("The answer is " + answer)

if oper == "+":
    calc = num1 + num2
    my_func(calc)
if oper == "-":
    calc = num1 + num2
    my_func(calc)
if oper == "/":
    calc = num1 + num2
    my_func(calc)
if oper == "*":
    calc = num1 + num2
    my_func(calc)
else:
    print("You have not entered a valid operator")

运行此代码时出现以下错误。

Traceback(最近一次调用最后一次):文件“(文件位置)”,第 13 行, 在 my_func(calc) 文件“(文件位置)”,第 6 行,在 my_func 中 print("答案是" + answer) TypeError: can only concatenate str (not "int") to str

【问题讨论】:

  • 作为与您遇到的问题无关的样式注释,您的ifs 系列应为if/elif/else

标签: python calculator


【解决方案1】:

您必须将 int answer 转换为字符串。 str() 这样做:

def my_func(answer):
    print("The answer is " + str(answer))

【讨论】:

    【解决方案2】:

    有几种方法:

    int 答案转换为string

    print("The answer is " + str(answer))
    

    使用%-formatting

    % 运算符(取模)也可用于字符串格式化。给定 'string' % 值,字符串中 % 的实例被替换为零或 更多的价值元素。

    print("The answer is %d" % answer)
    

    使用str.format()

    括号和其中的字符(称为格式字段)是 替换为传递给 str.format() 方法的对象。一种 括号中的数字可以用来指代的位置 对象传入 str.format() 方法。

    print("The answer is {}".format(answer))
    

    使用f-Strings

    格式化的字符串文字或 f-string 是一个字符串文字,它是 以“f”或“F”为前缀。这些字符串可能包含替换 字段,它们是由大括号 {} 分隔的表达式。尽管 其他字符串文字总是有一个常量值,格式化字符串 实际上是在运行时计算的表达式。

    print(f"The answer is {answer}")
    

    【讨论】:

    • 感谢您的详细解释,它让我深入了解了 python 的功能。另外,当我执行这段代码的时候,它也打印了else语句,你知道为什么吗?
    • 您应该使用elif 而不是连续的if。你可以阅读完整的description here
    【解决方案3】:

    在这里,您的答案将采用整数格式。因此,连接(+)字符串和整数是无效的。所以,你应该修改你的代码如下:

    def my_func(answer):
        print("The answer is ", answer)
    

    另外,我想建议您应该使用 elif 而不是多个 if(s)。否则,将为除最后一个案例之外的所有案例打印 else 部分。所以你的代码应该修改为:

    if oper == "+":
        calc = num1 + num2
        my_func(calc)
    elif oper == "-":
        calc = num1 - num2
        my_func(calc)
    elif oper == "/":
        calc = num1 / num2
        my_func(calc)
    elif oper == "*":
        calc = num1 * num2
        my_func(calc)
    else:
        print("You have not entered a valid operator")
    

    【讨论】:

      【解决方案4】:

      由于 java.Obejct 有方法 toString() 可以覆盖:

      public String toString() {
          return getClass().getName() + "@" + Integer.toHexString(hashCode());
      }
      

      当尝试使用无字符串对象添加字符串时,如下所示:

      System.out.println("Answer is " + (Object) m);
      

      Java 会调用 m.toString() 将该操作从“String + none String”转换为“String + String”,这样代码就可以顺利执行了。
      但是python没有类似的机制,所以我们需要手动指定none str来转换,一种方法是:

      "The answer is" + str(answer)
      

      【讨论】:

        【解决方案5】:

        你的代码有两个问题

        1. 它会在第 6 行产生一个错误,这是一个 TypeError,不用担心,只要这样做就可以轻松解决 您必须添加它们,而不是连接它们,只需将 str 函数放在 (answer) 之前并确保它们是字符串
            def my_func(answer):
            print("The answer is " + str(answer))
        
        1. 运算符分配错误,可以这样解决:
            if oper == "+":
               calc = num1 + num2
               my_func(calc)
            if oper == "-":
               calc = num1 - num2
               my_func(calc)
            if oper == "/":
               calc = num1 / num2
               my_func(calc)
            if oper == "*":
               calc = num1 * num2
               my_func(calc)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-03-12
          • 1970-01-01
          • 2016-02-13
          • 1970-01-01
          • 2011-11-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多