TypeError: not all arguments converted during string formatting (Python Typeerror)

TypeError: not all arguments converted during string formatting (Python Typeerror)

我有一个基本的 Python 脚本:

first_number = float(input("What is your first number?"))
#Ask the user what the first number is before calculation.
second_number = float(input("What is your second number?"))
#Ask the user the second number.
cal = str(input("Do you want to add, subtract, multiply, or divide? You can also 
square, or calculate with each option."))
if cal == "add":
 print("Done! The result is" % first_number + second_number)
 #Checks if the user wants to add, then adds if so. Then prints the final number.
if cal == "subtract":
 print("Done! The result is" % first_number + second_number)
 #Divides and prints.
if cal == "multiply":
 print("Done the result is" % first_number * second_number)
 #Multiplies
if cal == "square":
 print("Done! The result is" % first_number * second_number)
if cal == "divide":
 print("Done! The result is" % first_number / second_number)
 remainder = str(input("Assuming you know the original number, would you like to see 
 the integer remainder? (y/n)"))
 if remainder == "y":
  print("Here you go. The result is" % first_number % second_number)

在第 21 行,它有一个奇怪的字符串格式错误。我正在尝试将引号中的 str 与余数数学运算结合起来(使用模运算符),但它会出现这个奇怪的错误:TypeError: not all arguments converted during string formatting

我认为您需要更新字符串格式。假设您使用的是 Python 3.6 或更高版本,请使用 F 弦。参见 PEP 498

This one 介绍得很好。

此外,input() returns 一个字符串,因此无需转换为字符串。
另外,缩进。这是Python的一个核心原则。现在 学会正确地做。很难忘记一种编码方式...

first_number = float(input("What is your first number?"))
# Ask the user what the first number is before calculation.
second_number = float(input("What is your second number?"))
# Ask the user the second number.
cal = input("Do you want to add, subtract, multiply, or divide? You can also square, or calculate with each option.")
if cal == "add":
    print(f"Done! The result is {first_number + second_number}")
    # Checks if the user wants to add, then adds if so. Then prints the final number.
if cal == "subtract": # you were adding instead of substracting here
    print(f"Done! The result is {first_number - second_number}") 
    # Divides and prints.
if cal == "multiply":
    print(f"Done the result is {first_number * second_number}")
    # Multiplies
if cal == "square":
    print(f"Done! The result is {first_number * second_number}")
if cal == "divide":
    print(f"Done! The result is {first_number / second_number}")
    remainder = input("Assuming you know the original number, would you like to see the integer remainder? (y/n)")
    if remainder == "y":
        print(f"Here you go. The result is {first_number % second_number}")