大家好,我正在上Python入门课程,目前很喜欢并且无法停止练习。我认为我已经完成了练习,但是我有一个问题,就是在哪里开始列出变量,例如sum、difference、product等等。我使用Sublime Text,它们都是白色的,除了sum之外,为什么会这样?即使当我运行代码时数字似乎是准确的,这会对事情造成影响吗?
''' Write a program that prompts the user for two integers and then prints
'''sum, difference, product, average, distance, max and min.
import math
number1 = float(input("Please enter an integer: "))
number2 = float(input("Please enter another integer: "))
print()
print()
sum = number1 + number2
difference = number1 - number2
product = number1 * number2
average = (number1 + number2) / 2
distance = abs(number1 - number2)
maximum = max(number1, number2)
minimum = min(number1, number2)
print("Sum between the two:",sum)
print("Difference between the two:",difference)
print("Product between the two:",product)
print("Average between the two:",average)
print("The distance between the two:",distance)
print("The maximum between the two:",maximum)
print("The minimum between the two:",minimum)
感谢您的时间。
sum
是 Python 中的内置函数,如果需要使用它,则应更改此名称。 - PRMoureusum
函数,而您自定义的变量覆盖了它,那么在某个地方您将会收到一个TypeError
。 - cs95sum
是Python中预定义的函数,建议避免使用相同名称的变量。此外,请删除顶部引号和那些行。 - sudo