2017-08-29 45 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