问题是您没有捕获调用int的结果。def ratioFunction(num1, num2):
num1 = input('Enter the first number: ')
int(num1) # this does nothing because you don't capture it
num2 = input('Enter the second number: ')
int(num2) # also this
ratio12 = int(num1/num2)
print('The ratio of', num1, 'and', num2,'is', ratio12 + '.')
ratioFunction(num1, num2)
更改为:def ratioFunction(num1, num2):
num1 = input('Enter the first number: ')
num1 = int(num1) # Now we are good
num2 = input('Enter the second number: ')
num2 = int(num2) # Good, good
ratio12 = int(num1/num2)
print('The ratio of', num1, 'and', num2,'is', ratio12 + '.')
ratioFunction(num1, num2)
另外,当您在最后一行中调用ratioFunction(num1, num2)时,这将是一个NameError,除非您在某处定义了num1和num2。但老实说,这是完全不必要的,因为你正在采取投入。此函数不需要参数。此外,打印时还会出现另一个错误,因为您正在对ratio12 + '.'使用+运算符,但ratio12是一个int,而'.'是一个字符串。快速修复,将ratio12转换为str:In [6]: def ratioFunction():
...: num1 = input('Enter the first number: ')
...: num1 = int(num1) # Now we are good
...: num2 = input('Enter the second number: ')
...: num2 = int(num2) # Good, good
...: ratio12 = int(num1/num2)
...: print('The ratio of', num1, 'and', num2,'is', str(ratio12) + '.')
...:
In [7]: ratioFunction()
Enter the first number: 6
Enter the second number: 2
The ratio of 6 and 2 is 3.
尽管,很可能您的函数是假设接受参数,并且您在函数外部获取输入并将其传递给它。