2017-08-29 47 views
4

嘿所有即时通讯的Python入门介绍,到目前为止享受它,不能停止练习。我有我认为这个练习所要求的。我有一个问题,就是我开始列出诸如总和,差异,产品等变量。我使用Sublime Text,除了总和之外他们都是白色的,为什么?即使当我运行代码数字似乎是准确的,这是否会与事情混淆?数学函数python代码检查

''' 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) 

谢谢你的时间。

+2

'sum'是Python中的内置功能,如果你需要使用它未来 – PRMoureu

+0

你会收到一个'TypeError'某处的路线,你应该改变这个名字,如果你当您的自定义变量覆盖它时,决定使用内置的'sum'函数。 –

+0

啊好吧,这很有道理。代码本身如何?距离,最大和最小看起来好吗? – Matticus

回答

2

sum出现在您的文本编辑器中的事实是it is a function name以及事实。因此,如果您希望稍后在代码中使用此函数,则会引发异常。

有两点要注意你的代码:

没有必要import math,如果你不使用它。

'''标志着评论的开始和结束。要注释一行,请使用#。在开始你的评论应该是:

'''this is a multiline comment 
and here it continues''' 

# this is a single line comment 
+0

谢谢!感谢您的反馈。至于我的距离,最大和最小代码,这一切都做得很好?我带着我在课堂上记得的东西去了,这就是我想出来的。 – Matticus

+0

@Matticus是的,即使'sum'也能工作,唯一发生的事情就是你不能使用'sum()'函数,这就是为什么它被认为是不好的做法。 –

+0

谢谢你的帮助。我现在完全明白。 – Matticus