如何通过用户输入重启python程序(对于新手来说)

时间:2015-09-04 19:49:16

标签: python python-3.x

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程序,我已经四处寻找,但无法找到一个有用的答案,我知道如何申请我的程序,谢谢:)

1 个答案:

答案 0 :(得分:-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()

另外请确保您不应该比较字符串。改变&#34; 0&#34;到0等等。