python怎么重新运⾏,如何让python程序重新运⾏其
这⾥_有_两_个_问题_ :_
如何迭代
如何确保不同的随机选择的变量是不同的每次通过。在
只需在现有函数上循环,或者让它递归(⽐如在其他⼏个答案中),就可以解决第⼀个问题(实际上,递归确实不⾏,因为Python没有尾部调⽤消除,所以它最终会耗尽堆栈)。在
要解决这两个问题,需要使随机选择的变量成为函数的局部变量,然后循环。我还修改了它,以便在出现错误答案(函数的最后⼀⾏)的情况下,它返回要打印的字符串,⽽不是打印它。在import sys
import os
random pythonimport 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()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。