小编der*_*erp的帖子

5
推荐指数
2
解决办法
1万
查看次数

可以将列表转换为整数

我正在尝试编写一个程序来转换邮件密码.我正在尝试创建一个基本代码来处理.这是问题所在.

data = input('statement')
for line in data:
    code = ('l' == '1',
            'a' == '2'
            'r' == '3',
            'y' == '4')
    line = line.replace(data, code, [data])
print(line)    
Run Code Online (Sandbox Code Playgroud)

当我输入我的名字时,上述程序的这一点是这样的:

larry
Run Code Online (Sandbox Code Playgroud)

输出应该是

12334
Run Code Online (Sandbox Code Playgroud)

但我继续接受这个消息

TypeError: 'list' object cannot be interpreted as an integer
Run Code Online (Sandbox Code Playgroud)

所以我认为这意味着我的代码变量必须是一个在replace()中使用的整数有没有办法将该字符串转换为整数或是否有另一种方法来解决这个问题?

python typeerror python-3.x

4
推荐指数
1
解决办法
143
查看次数

如何使用python中的行号从文本文件中删除一行

这是一个示例文本文件

the bird flew
the dog barked
the cat meowed
Run Code Online (Sandbox Code Playgroud)

这是我的代码,以找到我想删除的短语的行号

phrase = 'the dog barked'

with open(filename) as myFile:
    for num, line in enumerate(myFile, 1):
        if phrase in line:
            print 'found at line:', num
Run Code Online (Sandbox Code Playgroud)

我可以添加什么来删除我尝试过的行号(num)

lines = myFile.readlines()
del line[num]
Run Code Online (Sandbox Code Playgroud)

但这不起作用我应该如何处理这个?

python python-3.x

4
推荐指数
1
解决办法
3万
查看次数

错误的颜色序列在蟒蛇龟中意味着什么?

我正在使用python turtle进行一个项目,我需要乌龟来绘制字符.但是,当我尝试使用rgb值foe颜色时,我不断收到错误消息.输入是

turtle.color((151,2,1))
Run Code Online (Sandbox Code Playgroud)

然后是一系列动作,但是当我运行该程序时,我收到此消息

File "C:/Users/Larry/Desktop/tests.py", line 5, in center
turtle.color((151,2,1))
File "<string>", line 1, in color
File "C:\Python33\lib\turtle.py", line 2208, in color
pcolor = self._colorstr(pcolor)
File "C:\Python33\lib\turtle.py", line 2688, in _colorstr
return self.screen._colorstr(args)
File "C:\Python33\lib\turtle.py", line 1158, in _colorstr
raise TurtleGraphicsError("bad color sequence: %s" % str(color))
turtle.TurtleGraphicsError: bad color sequence: (151, 2, 1)
Run Code Online (Sandbox Code Playgroud)

这是什么意思,我该如何解决?

python turtle-graphics python-3.x

3
推荐指数
2
解决办法
1万
查看次数

如何用笔来阻止乌龟的绘图?

我在python中使用turtle模块.问题是,每当我有乌龟移动时,即使笔上升,我也会画画.例如,如果我运行此程序:

import turtle

turtle.penup
turtle.goto(0,50)
Run Code Online (Sandbox Code Playgroud)

当龟移动到(0,50)时,它仍然会画一条线,为什么会这样,以及如何防止它?

python turtle-graphics python-3.x

1
推荐指数
1
解决办法
2万
查看次数

Python只写文件的第一行?

我之前发布了一个关于在文件中组织信息的程序的问题:Is there a way to Organize file content by list element in python?

我现在正在尝试使用以下代码将信息写入新文件

def main():
    #open the file
    info = open('Studentinfo.txt', 'r')
    #make file content into a list
    allItems = []
    #loop to strip and split
    for i in info:
        data = i.rstrip('\n').split(',')
        allItems.append(data)

    allItems.sort(key=lambda x: x[3]) # sort by activity

    for data in allItems:
        first = data[1]
        last = data[0]
        house = data[2]
        activity = data[3]
        a = str(first)
        b = str(last)
        c = str(house)
        d = …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

0
推荐指数
1
解决办法
3156
查看次数

标签 统计

python ×6

python-3.x ×6

turtle-graphics ×2

typeerror ×1