2015-09-04 68 views
-2
print(
     """ 
__ __  __       
| | | | | |       
| | | | ___| | ___ ___ _ __ ___ ___ 
| |/\| |/ _ \ |/ __/ _ \| '_ ` _ \/_ \ 
\ /\/__/ | (_| (_) | | | | | | __/ 
\/ \/ \___|_|\___\___/|_| |_| |_|\___| 
""" 
.strip()) 

print("\t\t\t\tto the pHinator 9001") 

ph = input("\nPlease enter a pH value:") 

if ph == "7": 
    print("\nA pH value of 7 is NEUTRAL") 

if "0" < ph < "7": 
    print("\nA pH value of less than 7 is ACIDIC") 

if "14" > ph > "7": 
    print("\nA pH value of more than 7 is ALKALINE") 

if ph < "0": 
     print("\nAn error occurred. Value must be between 0 and 14") 

if ph > "14": 
     print("\nAn error occurred. Value must be between 0 and 14") 

cont = input("\nPress 1 to enter a new value, Press 2 to exit.") 

if cont == "1": 
    (RESTART) 
if cont == "2": 
    input("\n\nPress the enter key to exit.") 
    exit 

這是我第一次嘗試製作一個合適的python程序,並且我已經四處搜尋,但找不到有用的東西答案,我知道如何適用於我的程序,謝謝:)如何通過用戶輸入重新啓動python程序

回答

-1

如果我是你我會包裝整個程序到一個函數。即

def func_name(): 
    print("\t\t\t\tto the pHinator 9001") 

    ph = input("\nPlease enter a pH value:") 

    if ph == "7": 
     print("\nA pH value of 7 is NEUTRAL") 

    if "0" < ph < "7": 
     print("\nA pH value of less than 7 is ACIDIC") 

    if "14" > ph > "7": 
     print("\nA pH value of more than 7 is ALKALINE") 

    if ph < "0": 
      print("\nAn error occurred. Value must be between 0 and 14") 

    if ph > "14": 
      print("\nAn error occurred. Value must be between 0 and 14") 

    cont = input("\nPress 1 to enter a new value, Press 2 to exit.") 

    if cont == 1: 
     func_name() 
    if cont == "2": 
     input("\n\nPress the enter key to exit.") 
     exit 

func_name() 

還要確保你不應該比較字符串。將「0」更改爲0等。

+0

我已經完成了您的建議,但我的程序沒有出現,不確定發生了什麼。 –

+0

你必須遞歸地做。即if cont == 1應嵌套在func_name()中。還要確保你比較整數而不是字符串。 「14」>「7」沒有意義,因爲這些都是字符串。它應該是14和7而不是「14」和「7」。 cont ==「1」與cont == 1相同 – elephant

+0

@ J.Hogg此外,請確保您最初調用該函數。縮進之外的某處。我將編輯上面的代碼,使其更類似 – elephant

相關問題