Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何让python程序重新运行自身_Python - Fatal编程技术网

如何让python程序重新运行自身

如何让python程序重新运行自身,python,Python,我希望下面的代码自动重新运行自己有什么想法可以做到这一点吗?顺便说一句,我是新的堆栈溢出和python本身,所以如果我做了任何错误的事情,请让我知道,谢谢 import sys import os import random answer_correct_message = random.choice(['Well done', 'Correct answer','Nice one','Thats correct!']) answer_wrong_message = random.choice

我希望下面的代码自动重新运行自己有什么想法可以做到这一点吗?顺便说一句,我是新的堆栈溢出和python本身,所以如果我做了任何错误的事情,请让我知道,谢谢

import sys
import os
import random 
answer_correct_message = random.choice(['Well done', 'Correct answer','Nice one','Thats correct!'])
answer_wrong_message = random.choice(['Unlucky','Thats wrong','Nope'])
random_num_1 = random.randint(1,10)
random_num_2 = random.randint(1,10)


def question_asker_and_answerer():
    q2 = input("What is " + str(random_num_1) + " + " + str(random_num_2) + "?")
    if q2 == random_num_1 + random_num_2:
        the_questions = True
        if the_questions == True:
            return (answer_correct_message)
        else:
            return (answer_wrong_message)
    else:
        the_questions = False
        if the_questions == True:
            return (answer_correct_message)
        else:
            print(answer_wrong_message)


print question_asker_and_answerer()

在这种情况下,您不需要一个程序来重新运行自己。当您希望脚本作为守护进程运行时,就会出现这种需求。这只是创建一个循环的问题

while True:
   print question_asker_and_answerer()

这里有两个问题:

  • 如何迭代
  • 如何确保各个随机选择的变量在每个过程中都是不同的
只需在现有函数上循环,或者让它递归(就像在其他几个答案中一样)就可以解决第一个问题(实际上,递归实际上没有,因为Python没有尾部调用消除,所以它最终会耗尽堆栈)

要解决这两个问题,您需要使随机选择的变量成为函数的局部变量,然后循环。我还对其进行了修改,以便在出现错误答案(函数的最后一行)时,它返回要打印的字符串,而不是打印它


你考虑过循环吗?看我的答案:如果你想让程序有用,你还需要修改函数!这是一个很好的答案。不太清楚为什么它会被否决+1.我相信你做得很好!
import sys
import os
import random 

def question_asker_and_answerer():
    answer_correct_message = random.choice(['Well done', 'Correct answer',
                                            'Nice one','Thats correct!'])
    answer_wrong_message = random.choice(['Unlucky','Thats wrong','Nope'])
    random_num_1 = random.randint(1,10)
    random_num_2 = random.randint(1,10)
    q2 = input("What is " + str(random_num_1) + " + " + str(random_num_2) + "?")
    if q2 == random_num_1 + random_num_2:
        the_questions = True
        if the_questions == True:
            return (answer_correct_message)
        else:
            return (answer_wrong_message)
    else:
        the_questions = False
        if the_questions == True:
            return (answer_correct_message)
        else:
            return (answer_wrong_message)


while True:
    print question_asker_and_answerer()