【发布时间】:2021-06-11 04:03:35
【问题描述】:
我是新手...正在研究global 关键字。
此代码创建一个随机字符串。
我认为 第 8 行,string.append(word[x]) 在此代码上出错
因为string是一个全局变量,所以必须要global关键字。 (全局字符串)
但是这段代码运行正确... 为什么可以正常运行...?
import random
word = 'a b c d e f g h i j k l m n o p q r s t u v w x y z'.split(" ")
string = []
n = int(input())
def get(leng) :
for i in range (0, leng) :
x = random.randint(0, len(word)-1)
string.append(word[x])
sen = ''.join(string)
return sen
print(get(n))
【问题讨论】:
标签: python python-3.x global-variables global