如何通过用户输入重新启动python程序(对于新手)
这是我第一次尝试制作一个合适的python程序,我四处寻找,但找不到一个有用的答案,我知道如何应用于我的程序,谢谢:如果我是你,我会将整个程序包装成一个函数。i、 e如何通过用户输入重新启动python程序(对于新手),python,python-3.x,Python,Python 3.x,这是我第一次尝试制作一个合适的python程序,我四处寻找,但找不到一个有用的答案,我知道如何应用于我的程序,谢谢:如果我是你,我会将整个程序包装成一个函数。i、 e print( """ __ __ __ | | | | | | | | | | ___| | ___ ___ _ __ ___ ___ | |/\| |/ _ \ |/ __/ _
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
还要确保您不应该比较字符串。将0更改为0,等等。我已经按照您的建议执行了,但我的程序没有出现,不确定发生了什么。您必须递归执行。i、 e.if cont==1应嵌套在函数名中。还要确保比较的是整数而不是字符串。14>7没有意义,因为它们都是字符串。它应该是14和7,而不是14和7。cont==1和cont==1的情况也是如此1@J.Hogg另外,确保最初调用该函数。在缩进之外的某个地方。我将编辑上面的代码,使其更相似半修复,它现在运行良好,但按1只是结束程序,而不是重新启动。我已修复字符串comparing@elephant看,;递归将毁掉你的堆栈,以及如何正确操作的指导。
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()