![](/img/trans.png)
[英]Python - Looking for a more efficient way to renumber the keys in my dictionary
[英]Looking for a more efficient way to write my python program
在我的三角學課上,我們被分配查找方程的判別式和圓錐形部分。
我寫了一個計算判別式的函數,然后根據判別式的值打印圓錐曲線...
我只是想知道是否有更好,更有效的方式編寫此代碼:
def disc():
a_disc = raw_input("Please enter the value of A: ")
b_disc = raw_input("Please enter the value of B: ")
c_disc = raw_input("Please enter the value of C: ")
disc = b_disc**2-4*(a_disc)*(c_disc)
print ("The discriminant is: %s") % (disc)
if disc < 0:
if a_disc != c_disc:
print ("The conic is an Ellipse!")
elif a_disc == c_disc:
print ("The conic is a Circle!")
elif disc > 0:
print ("The conic is a Hyperbola!")
elif disc == 0:
print ("The conic is a Parabola!")
else:
print ("Something went wrong...")
disc()
我不完全理解在函數內部使用參數,但是我想做些類似的事情:
def disc(a,b,c):
我猜這將是更干凈的方法。
我非常感謝任何人提供的任何反饋。 提前致謝!
是的,您可以將disc
移到一個僅計算值的函數中,然后將所有輸入和輸出邏輯作為單獨的代碼。 您的某些if語句也是多余的。 這是一個稍微簡單的版本:
def disc(a, b, c):
return b ** 2 - 4 * a * c
a = raw_input("Please enter the value of A: ")
b = raw_input("Please enter the value of B: ")
c = raw_input("Please enter the value of C: ")
val = disc(int(a), int(b), int(c))
print ("The discriminant is: %s") % (val)
if val == 0:
print ("The conic is a Parabola!")
elif val > 0:
print ("The conic is a Hyperbola!")
elif a != c:
print ("The conic is an Ellipse!")
else:
print ("The conic is a Circle!")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.