[英]How do I completely restart my python script from within?
我正在创建一个作为项目的小计算器,当它完成时键入yes,我希望它重新启动。 问题是,我似乎不知道如何。 当涉及到python时,我不是一个天才。
import sys
OPTIONS = ["Divide", "divide", "Multiply", "multiply", "Add", "add", "Subtract", "subtract"]
def userinput():
while True:
try:
number = int(input("Number: "))
break
except ValueError:
print("NOPE...")
return number
def operation():
while True:
operation = input("Multiply/Divide/Add: ")
if operation in OPTIONS:
break
else:
print("Not an option.")
return operation
def playagain():
while True:
again = input("Again? Yes/No: ")
if again == "Yes" or again == "yes":
break
elif again == "No" or again == "no":
sys.exit(0)
else:
print("Nope..")
def multiply(x,y):
z = x * y
print(z)
def divide(x,y):
z = x / y
print(z)
def add(x,y):
z = x + y
print(z)
def subtract(x,y):
z = x - y
print(z)
while True:
operation = operation()
x = userinput()
y = userinput()
if operation == "add" or operation == "Add":
add(x,y)
elif operation == "divide" or operation == "Divide":
divide(x,y)
elif operation == "multiply" or operation == "Multiply":
multiply(x,y)
elif operation == "subtract" or operation == "Subtract":
subtract(x,y)
playagain()
我目前在第28行有一个中断,因为我找不到如何重新启动它。 如果有人可以帮助我,谢谢!
您无需重新启动脚本,只需在编码之前对设计有所了解。 使用您提供的脚本,此问题有两种更改:
def playagain():
while True:
again = input("Again? Yes/No: ")
if again == "Yes" or again == "yes":
return True
elif again == "No" or again == "no":
return False
else:
print("Nope..")
然后,在调用playagain()
将其更改为:
if not playagain(): break
我想我知道您为什么要重新启动脚本,您有一个错误。
Python函数与其他任何对象一样。 当你说:
operation = operation()
将对operation
函数的引用重新分配给该函数返回的字符串。 因此,您第二次在重新启动时调用它时,它失败并显示:
TypeError: 'str' object is not callable
重命名您的operation
函数,例如foperation
:
def fopertion():
然后:
operation = foperation()
因此,完整的代码变为:
import sys
OPTIONS = ["Divide", "divide", "Multiply", "multiply", "Add", "add", "Subtract", "subtract"]
def userinput():
while True:
try:
number = int(input("Number: "))
break
except ValueError:
print("NOPE...")
return number
def foperation():
while True:
operation = input("Multiply/Divide/Add: ")
if operation in OPTIONS:
break
else:
print("Not an option.")
return operation
def playagain():
while True:
again = input("Again? Yes/No: ")
if again == "Yes" or again == "yes":
return True
elif again == "No" or again == "no":
return False
else:
print("Nope..")
def multiply(x,y):
z = x * y
print(z)
def divide(x,y):
z = x / y
print(z)
def add(x,y):
z = x + y
print(z)
def subtract(x,y):
z = x - y
print(z)
while True:
operation = foperation()
x = userinput()
y = userinput()
if operation == "add" or operation == "Add":
add(x,y)
elif operation == "divide" or operation == "Divide":
divide(x,y)
elif operation == "multiply" or operation == "Multiply":
multiply(x,y)
elif operation == "subtract" or operation == "Subtract":
subtract(x,y)
if not playagain(): break
我可以对此代码进行许多其他改进,但让我们首先使其工作。
我没有重新启动股票,所以您可以永久使用它,只有用户本人才能退出。 我只更改了playagain()和while循环的末尾,请阅读注释以获取解释:
import sys
OPTIONS = ["Divide", "divide", "Multiply", "multiply", "Add", "add", "Subtract", "subtract"]
# python2 compatibility, you dont need to care about this ;-)
try:
input = raw_input
except:
pass
def userinput():
while True:
try:
number = int(input("Number: "))
break
except ValueError:
print("NOPE...")
return number
def operation():
while True:
operation = input("Multiply/Divide/Add: ")
if operation in OPTIONS:
break
else:
print("Not an option.")
return operation
def playagain():
"""
return True if userinput "Yes" and False if userinput "no"
does this until user input is yes or no
"""
again = input("Again? Yes/No: ")
if again.lower() == "yes":
return True
elif again.lower() == "no":
return False
else:
# reruns the code --> until user input is 'yes' or 'no'
return playagain()
def multiply(x,y):
z = x * y
print(z)
def divide(x,y):
z = x / y
print(z)
def add(x,y):
z = x + y
print(z)
def subtract(x,y):
z = x - y
print(z)
# a main in python: this will be executed when run as a script
# but not when you import something from this
if __name__ == '__main__':
play = True
while play:
operation = operation()
x = userinput()
y = userinput()
if operation == "add" or operation == "Add":
add(x,y)
elif operation == "divide" or operation == "Divide":
divide(x,y)
elif operation == "multiply" or operation == "Multiply":
multiply(x,y)
elif operation == "subtract" or operation == "Subtract":
subtract(x,y)
# player/user can exit the loop if he enters "no" and therefore end the loop
play = playagain()
使用os.execv()...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.