[英]Is there a command for exiting and re-entering a script?
Sorry for the other post, it had an error.对不起,另一个帖子,它有一个错误。
My simplified code:我的简化代码:
#test
import json
x = {}
x['x'] = {'y': 1, 'z': 0}
with open('x.json', 'w') as f:
json.dump(x, f)
and和
#test2
import json
f = open('x.json')
x = json.load(f)
while x['x']['y'] > 0:
x['x']['z'] = x['x']['z'] + 1
x['x']['y'] = x['x']['y'] - 1
if x['x']['y'] == 0:
print(x['x']['z'])
n = input("Number: ")
while n.isdigit() == False:
print("Not a number")
n = input("Number: ")
if n.isdigit() == True:
x['x']['y'] = int(n)
with open('x.json', 'w') as f:
json.dump(x, f)
I have two codes so I don't overwrite the numbers of test2 with test我有两个代码,所以我不会用 test 覆盖 test2 的数字
This is already kinda what I want, but my output for n = 4 and n = 5 is:这已经有点像我想要的了,但是我的 output 对于 n = 4和 n = 5是:
1
Number: 4
5
Number: 5
10
Number:
And so on...等等...
But instead, I want the code to Exit completely and then start it again without me doing it manually.但相反,我希望代码完全退出,然后重新启动它,而无需我手动执行。 Kinda like:有点像:
1
Number: 4
Code restarts代码重新启动
5
Number: 5
Code restarts代码重新启动
5
Number: 10
and so on.等等。 Thank you:-)谢谢:-)
If I understood you correctly, You will have to create two separate scripts如果我理解正确,您将不得不创建两个单独的脚本
test1.py测试1.py
#test
import json
import os
x = {}
x['x'] = {'y': 1, 'z': 0}
with open('x.json', 'w') as f:
json.dump(x, f)
os.system('python test2.py') # this line will run the second script
test2.py测试2.py
import json
f = open('x.json')
x = json.load(f)
while x['x']['y'] > 0:
x['x']['z'] = x['x']['z'] + 1
x['x']['y'] = x['x']['y'] - 1
if x['x']['y'] == 0:
print(x['x']['z'])
n = input("Number: ")
while n.isdigit() == False:
print("Not a number")
n = input("Number: ")
if n.isdigit() == True:
x['x']['y'] = int(n)
with open('x.json', 'w') as f:
json.dump(x, f)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.