急需你們的幫助,所以我必須輸入一個可以進行計算的數字,但它會詢問我是否要輸入另一個數字。如果我按 y 或 Y ir 再次啟動,如果我按任何其他按鈕它會結束,但似乎我的代碼沒有結束。請幫忙
import math
import sys
def start():
t = input()
x = int(t)
try:
a = (3 * math.pow(x, 4) - 12 * math.pow(x, 2) - 1)/((x 2)*(x - 2)) # aprekina a(x)
b = math.log(3 * x 5 * x) # aprekina b(x)
if a * b < 50: # parbauda nosacijumu
f1 = 1 / math.pow(a, 2) - math.pow(a, 2) * b - 11 # ja nosacijums y tad aprekina f1
print(round(f1, 2)) # izvada f1
else:
f2 = 16 * a * b - 2 * math.sqrt(2) # ja nosacijums n tad aprekina f2
print(round(f2, 2)) # izvada f2
except ValueError:
print("error")
except ZeroDivisionError:
print("error")
c = input("repeat?")
if c == "y" or "Y":
start()
else:
sys.exit
start()
uj5u.com熱心網友回復:
if c == "y" or "Y":將始終回傳true,因為這不是一個空字串(任何字串"")將True因此"Y"永遠是True
你需要寫:
if (c == "y" or c == "Y"):
這將正確檢查 c 是否等于小寫或大寫 y。
你也可以寫:
if (c.lower() == "y"):
uj5u.com熱心網友回復:
if c == "y" or "Y"
此條件始終為 True,您的代碼永遠不會結束!
因為“Y”永遠是真的!!!
嘗試這個:
if c == "y" or c == "Y"
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/348413.html
