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