0
我正在编写一个Python程序,它响应请求并将响应发回给用户。以下是两个函数的示例。我如何在不使用全局变量的情况下执行此操作,并仍然返回非重复的随机响应?从函数内的列表中返回一个非重复的随机结果
# stores prior response
website_result = 'first_response.wav'
def launch_website():
# if service is offline return with default msg otherwise launch service
if is_connected() == 'FALSE':
arg = 'this_service_is_offline.wav'
return arg
else:
site = 'http://www.somesite.com'
launch_it(site)
return launch_website_response()
def launch_website_response():
# using the global variable inside function
global website_result
# possible responses
RESPONSES = ['first_response.wav', 'second_response.wav', 'third_response.wav']
# ensures a non-repeating response
tmp = random.choice(RESPONSES)
while website_result == tmp:
tmp = random.choice(RESPONSES)
website_result = tmp
return website_result
请检查缩进并设置代码的格式 –
也许只是''launch_website_response()'将'website_result'作为参数 – JGut