我是python的初学者.我无法理解问题所在?
def list_benefits():
s1 = "More organized code"
s2 = "More readable code"
s3 = "Easier code reuse"
s4 = "Allowing programmers to share and connect code together"
return s1,s2,s3,s4
def build_sentence():
obj=list_benefits()
print obj.s1 + " is a benefit of functions!"
print obj.s2 + " is a benefit of functions!"
print obj.s3 + " is a benefit of functions!"
print build_sentence()
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
Traceback (most recent call last):
Line 15, in <module>
print build_sentence()
Line 11, in build_sentence
print obj.s1 …Run Code Online (Sandbox Code Playgroud) 我有一个小的测试代码块试图处理一个带有球的简单照片:
#!/usr/local/bin/python
import cv2
import numpy as np
img = cv2.imread("b.jpg")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray,cv2.CV_HOUGH_GRADIENT)
Run Code Online (Sandbox Code Playgroud)
当我尝试运行时,我得到:
AttributeError:'module'对象没有属性'HOUGH_GRADIENT'
我已经安装并重新安装了两天,试图弄清楚什么是错的.任何帮助或指针将不胜感激!
我is_zipfile在提取它之前用它来检查它是否是一个zipfile.但该方法在StringIO对象的excel文件中返回True.我使用的是Python 2.7.有谁知道如何解决这一问题?使用可靠is_zipfiile吗?谢谢.
所以我有一个for循环.
>>> row = [5, 2, 5, 4, 2, 2, 5, 5, 5, 2]
>>> for i in row:
if i == 5:
print(row.index(i))
if i == 2:
print(row.index(i))
Run Code Online (Sandbox Code Playgroud)
OUTPUT
0
1
0
1
1
0
0
0
1
Run Code Online (Sandbox Code Playgroud)
我想得到:0 1 2 4 5 6 7 8 9.换句话说,我想得到i我正在for循环中看到的索引,如果它是= 5或2 ...任何简单的方法来做到这一点?
我想知道如何从列表中获取随机颜色以在draw_rectangle()
colors = ["red", "orange", "yellow", "green", "blue", "violet"]
canvas.create_rectangle(self.x, self.y, self.x + 60, self.y + 60, fill = random.choice(colors))
Run Code Online (Sandbox Code Playgroud)
这导致我的代码崩溃,我还能尝试什么?