[英]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.