如何让python程序重新运行自身
我希望下面的代码自动重新运行自己有什么想法可以做到这一点吗?顺便说一句,我是新的堆栈溢出和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
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()
这里有两个问题:
- 如何迭代李>
- 如何确保各个随机选择的变量在每个过程中都是不同的
你考虑过循环吗?看我的答案:如果你想让程序有用,你还需要修改函数!这是一个很好的答案。不太清楚为什么它会被否决+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()