1 回答

TA贡献1982条经验 获得超2个赞
所以全局变量是一件坏事,你应该尽可能避免使用它们,但你可以这样做:
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(' ')
也就是说,这是一个坏主意,您可能根本不应该这样做。调试管理全局状态的代码是地狱般的。将此逻辑封装到一个对象中,并在那里对其进行操作。
添加回答
举报