1 回答

TA貢獻(xiàn)1982條經(jīng)驗(yàn) 獲得超2個(gè)贊
所以全局變量是一件壞事,你應(yīng)該盡可能避免使用它們,但你可以這樣做:
global_list = []
def list_string(): # leave camelCase for javascript -- we use snake_case in Python land.
while True:
string_input = input("Please input a single line of strings\n")
if len(string_input) >= 20:
break
else:
print("Error. Your input needs to have at least 20 characters")
# string_input is now guaranteed to be a string of 20 characters or longer
global global_list # indicate that you're changing the global now
global_list = string_input.split(' ')
也就是說(shuō),這是一個(gè)壞主意,您可能根本不應(yīng)該這樣做。調(diào)試管理全局狀態(tài)的代碼是地獄般的。將此邏輯封裝到一個(gè)對(duì)象中,并在那里對(duì)其進(jìn)行操作。
添加回答
舉報(bào)