如何从内部完全重新启动python脚本?
我正在创建一个小计算器作为一个项目,我希望它在完成后键入yes时重新启动。问题是,我似乎不知道怎么做。说到python,我不是一个天才如何从内部完全重新启动python脚本?,python,Python,我正在创建一个小计算器作为一个项目,我希望它在完成后键入yes时重新启动。问题是,我似乎不知道怎么做。说到python,我不是一个天才 import sys OPTIONS = ["Divide", "divide", "Multiply", "multiply", "Add", "add", "Subtract", "subtract"] def userinput(): while True: try:
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行有一个中断,因为我不知道如何重新启动它。如果有人能帮我,谢谢 使用os.execv()
您不需要重新启动脚本,只需在编写代码之前考虑一下设计。根据您提供的脚本,此问题有两种修改:
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..")
然后,在调用playreach()
的地方,将其更改为:
if not playagain(): break
我想我知道你为什么要重新启动脚本,你有一个bug
Python函数与任何其他对象类似。当你说:
operation = operation()
将对操作的引用
函数重新分配给该函数返回的字符串。因此,在重新启动时第二次调用时,它会失败:
TypeError: 'str' object is not callable
将操作
函数重命名为类似fooperation
:
def fopertion():
然后:
因此,完整的代码变成:
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
我还可以对这段代码做很多其他的改进,但是让我们先让它工作起来。我没有重新启动scrip,而是让你可以永远使用它,只有用户自己可以退出它。最后,我只更改了playreach()和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()
请在问题本身中添加一个,而不是指向其他地方所有代码的链接。同时,通常的答案是:将所有顶级代码封装在函数中,然后在
中调用该函数,而True:
循环。这在某些情况下不起作用(例如,如果你有一堆混乱的全球状态),但是试试看,看看你是否能让它起作用。对不起!我是新来的。我该怎么做?我有这个。def playreach():while True:reach=input(“reach?Yes/No:”)if reach==“Yes”或reach==“Yes”:再次中断elif==“No”或reach==“No”:sys.exit(0)else:print(“Nope..”),就像我说的,我不是什么天才。所以我对你刚才说的几乎一无所知。不过我学得很快:另一方面,如果你了解字典,你可以把函数放在字典里,比如。或者甚至变得很棘手,比如说,它说它与任何“缩进”级别都不匹配。
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()