除以两个整数并打印时出现 TypeError
TypeError when dividing two integers and printing
该程序应该取两个数字,在“findRemainder”函数中将它们相除并打印余数。
num1=input("Enter a number: ")
num2=input("Enter the divisor: ")
result = 0
def findRemainder(x, y):
result = (x%y)
print(str(result))
findRemainder(num1, num2)
但是,当代码是运行时它returns这个错误:
第 5 行,在 findRemainder
中
结果 = (x%y)
类型错误:在字符串格式化期间并非所有参数都已转换
您没有将字符串转换为整数。在找到余数之前,您必须将输入字符串转换为整数。像这样,
num1 = int(input("Enter a number: "))
num2 = int(input("Enter the divisor: "))
result = 0
def findRemainder(x, y):
result = (x % y)
print(str(result))
findRemainder(num1, num2)
该程序应该取两个数字,在“findRemainder”函数中将它们相除并打印余数。
num1=input("Enter a number: ")
num2=input("Enter the divisor: ")
result = 0
def findRemainder(x, y):
result = (x%y)
print(str(result))
findRemainder(num1, num2)
但是,当代码是运行时它returns这个错误:
第 5 行,在 findRemainder
中结果 = (x%y)
类型错误:在字符串格式化期间并非所有参数都已转换
您没有将字符串转换为整数。在找到余数之前,您必须将输入字符串转换为整数。像这样,
num1 = int(input("Enter a number: "))
num2 = int(input("Enter the divisor: "))
result = 0
def findRemainder(x, y):
result = (x % y)
print(str(result))
findRemainder(num1, num2)