大佬教程收集整理的这篇文章主要介绍了TypeError:并非所有参数都在字符串格式化期间转换(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-Strings。见PEP 498。
This one 给出了一个很好的介绍。
此外,input()
返回一个字符串,因此无需转换为字符串。
还有,缩进。这是 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}")
以上是大佬教程为你收集整理的TypeError:并非所有参数都在字符串格式化期间转换(Python Typeerror)全部内容,希望文章能够帮你解决TypeError:并非所有参数都在字符串格式化期间转换(Python Typeerror)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。