Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 这段简单的代码在第二次循环时给出一个错误_Python_String_Random - Fatal编程技术网

Python 这段简单的代码在第二次循环时给出一个错误

Python 这段简单的代码在第二次循环时给出一个错误,python,string,random,Python,String,Random,为什么这段代码在for循环中第二次运行时会抛出错误?我不知道它第一次是如何完美工作的,因此抛出了以下错误: J6554r15什么的 回溯(最近一次调用上次):文件 “C:\Users\test\Desktop\sooo.py”,第8行,在 string=str(random.choice(string.letters)+str(random.randint(10010000))+ran dom.choice(string.letters)+str(random.randint(0100)))At

为什么这段代码在for循环中第二次运行时会抛出错误?我不知道它第一次是如何完美工作的,因此抛出了以下错误:

J6554r15什么的

回溯(最近一次调用上次):文件 “C:\Users\test\Desktop\sooo.py”,第8行,在 string=str(random.choice(string.letters)+str(random.randint(10010000))+ran dom.choice(string.letters)+str(random.randint(0100)))AttributeError: “str”对象没有属性“letters”


这里缺少什么?

您正在覆盖
导入字符串库的循环内设置变量
字符串
。因此,在第二轮中,您不再使用字符串库来使用
string.letters
,而是实际的字符串。尝试使用其他变量名。

您正在覆盖
导入字符串库的循环内设置变量
字符串。因此,在第二轮中,您不再使用字符串库来使用
string.letters
,而是实际的字符串。尝试使用其他变量名。

您正在使用名称
string
作为结果,它将替换导入的
string
模块。重命名变量。阴影变量名称
string
library with
string
variable.BTW,您不需要外部的
str
调用,您正在转换的内容已经是一个字符串。您正在使用名称
string
作为结果,它将替换导入的
string
模块。重命名变量。阴影变量名称
string
library with
string
variable.BTW,您不需要外部的
str
调用,您正在转换的内容已经是一个字符串。
import random
import string



for x in range(0,15):
    print "something"
    string= str(random.choice(string.letters)+str(random.randint(100,10000))+random.choice(string.letters)+str(random.randint(0,100)))
    print  string