简体   繁体   English

是否有退出和重新输入脚本的命令?

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

相关问题 Python,重新进入`with`块 - Python, re-entering the `with` block Kivy:在重新进入屏幕时将切换按钮重置为“正常” - Kivy: resetting toggle buttons to “normal” on re-entering screen 更新Tkinter列表框而不退出并重新进入? - Updating Tkinter listbox without quitting and re-entering? 重新进入 FOV 时修复其他客户端字符上的偏移量,(PyGame 和套接字) - fix the offset on other client's char when re-entering FOV, (PyGame and sockets) 通过从报纸收集文字重新输入代码时,所有内容都被忽略,但第一个链接 - Everything ignored but the first link when re-entering the code by gathering text from newspapers Django - 用户在注销后单击浏览器后退按钮重新进入会话 - Django - User re-entering session by clicking browser back button after logging out 如何使用户在重新进入 discord 服务器时不会删除静音角色? - How can I make it so that the user does not remove mute role when re-entering the discord server? Scipy 旋转模块在重新输入转换后的四元数时产生不同的结果 - Scipy Rotation Module produces differing results when re-entering a converted quaternion 输入 CLI 命令后保持脚本运行 - Keep script running after entering a CLI command 在继续运行脚本的同时从python脚本退出命令 - Exiting command from python script whilst continuing to run script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM