我对python编程很陌生,我想尝试一个简单的文本冒险游戏,但我立即偶然发现了一个障碍.
class userInterface:
def __init__(self, roomID, roomDesc, dirDesc, itemDesc):
self.roomID = roomID
self.roomDesc = roomDesc
self.dirDesc = dirDesc
self.itemDesc = itemDesc
def displayRoom(self): #Displays the room description
print(self.roomDesc)
def displayDir(self): #Displays available directions
L1 = self.dirDesc.keys()
L2 = ""
for i in L1:
L2 += str(i) + " "
print("You can go: " + L2)
def displayItems(self): #Displays any items of interest
print("Interesting items: " + str(self.itemDesc))
def displayAll(self, num): #Displays all of the above
num.displayRoom()
num.displayDir()
num.displayItems()
def playerMovement(self): …Run Code Online (Sandbox Code Playgroud) 我对python有点新,但熟悉OOP.我正在尝试使用PyGame编写游戏.基本上,我的目标是每隔几秒钟渲染一次树木,并将树木移动到整个屏幕上.
所以这是我的代码:
from collections import deque
import pygame,random,sys
pygame.init()
size = 800,600
screen = pygame.display.set_mode(size)
class tree:
def __init__(self):
self.img = pygame.image.load("tree.png")
self.rect = self.img.get_rect()
def render(self):
screen.blit(self.img,self.rect)
def move(self,x,y):
self.rect = self.rect.move(x,y)
#creating a queue of trees
trees = deque()
#appending the first tree on to the queue
trees.append(tree())
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
#appending tree() to trees queue every 300 ms
if pygame.time.get_ticks() % 300 == 0:
trees.append(tree())
#rendering and moving all …Run Code Online (Sandbox Code Playgroud) 如何CAP_PROP_FRAME_COUNT从python中的opencv 访问?我试过这个:
import cv2
cap = cv2.VideoCapture('myvideo.avi')
frames_count, fps, width, height = cap.get(cv2.CAP_PROP_FRAME_COUNT), cap.get(cv2.CAP_PROP_FPS), cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
Run Code Online (Sandbox Code Playgroud)
还有这个:
import cv2
import cv
cap = cv2.VideoCapture('myvideo.avi')
frames_count, fps, width, height = cap.get(cv.CAP_PROP_FRAME_COUNT), cap.get(cv.CAP_PROP_FPS), cap.get(cv.CAP_PROP_FRAME_WIDTH), cap.get(cv.CAP_PROP_FRAME_HEIGHT)
Run Code Online (Sandbox Code Playgroud)
还有这个:
import cv2
cap = cv2.VideoCapture('myvideo.avi')
frames_count, fps, width, height = cap.get(cv2.cv.CAP_PROP_FRAME_COUNT), cap.get(cv2.cv.CAP_PROP_FPS), cap.get(cv2.cv.CAP_PROP_FRAME_WIDTH), cap.get(cv2.cv.CAP_PROP_FRAME_HEIGHT)
Run Code Online (Sandbox Code Playgroud)
但是我收到了这个错误:
AttributeError: 'module' object has no attribute 'CAP_PROP_FRAME_COUNT'
Run Code Online (Sandbox Code Playgroud)
我正在使用python 2.7.5和OpenCV 2.4.9.
我试图将共享秘密传递给子进程.在Linux环境中,这可行.在Windows环境中,孩子不会收到共享密钥.下面的三个文件是我正在尝试做的一个简单示例:
import multiprocessing
import module1
import module2
if __name__ == "__main__":
module1.init()
process = multiprocessing.Process(target=module2.start)
process.start()
process.join()
Run Code Online (Sandbox Code Playgroud)
import ctypes
import multiprocessing
x = None
def init():
global x
x = multiprocessing.Value(ctypes.c_wchar_p, "asdf")
Run Code Online (Sandbox Code Playgroud)
import module1
def start():
print(module1.x.value)
Run Code Online (Sandbox Code Playgroud)
在Ubuntu 14.04环境中,在Python 3.5上,我收到以下输出:
$ python3 main.py
asdf
Run Code Online (Sandbox Code Playgroud)
在CentOS 7环境中,我收到以下输出:
$ python3 main.py
asdf
Run Code Online (Sandbox Code Playgroud)
在Windows 10上使用Windows子系统Linux(在创建者更新之前和之后,所以Ubuntu 14.04和16.04)我得到以下输出:
$ python3 main.py
asdf
Run Code Online (Sandbox Code Playgroud)
但是,在Windows 7和Windows 10环境中,使用3.5或3.6,我得到的AttributeError不是上面的输出:
Process Process-1:
Traceback (most recent call last):
File "C:\Python\Python35\lib\multiprocessing\process.py", line 249, in _bootstrap
self.run()
File …Run Code Online (Sandbox Code Playgroud) 我收到错误 AttributeError: 'Response' object has no attribute 'get' 对于我编写的以下代码
def convert_json(self,bucket,userid,imgfilename,field,i):
bucketName = bucket
link = "users_"+str(userid)+'/'+imgfilename
c = S3Connection(self.AWS_ACCESS_KEY_ID,self.AWS_ACCESS_KEY_SECRET)
p = c.generate_url(expires_in=long(7200),method='GET',bucket=bucketName,key=link,query_auth=True,force_http=False)
post_url = "http://someurl"
wrapper = {"filename":p}
try:
response = requests.post(post_url, json=wrapper)
print response
if response.status_code == 200:
text = response.get('description', [])
else:
text = []
except Exception:
if response.status_code == 200:
text = response.get('description', [])
else:
text = []
return text
Run Code Online (Sandbox Code Playgroud) Python版本:3.10
\n我试图使用以下 pip 命令安装freegames python 包
\nC:\\Users\\praty>pip install freegames\nDefaulting to user installation because normal site-packages is not writeable\nCollecting freegames\n Downloading freegames-2.3.2-py2.py3-none-any.whl (108 kB)\n |\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88| 108 kB 504 kB/s\nInstalling collected packages: freegames\nSuccessfully installed freegames-2.3.2\nRun Code Online (Sandbox Code Playgroud)\n但是在我的 python 环境中导入相同的内容时,我收到了这个错误
\nC:\\Users\\praty>python\nPython 3.10.0 (tags/v3.10.0:b494f59, Oct 4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)] on win32\nType "help", "copyright", "credits" or "license" for more information.\n>>> import freegames\nTraceback (most recent call last):\n File "<stdin>", line 1, in <module>\n File "C:\\Users\\praty\\AppData\\Roaming\\Python\\Python310\\site-packages\\freegames\\_init_.py", line 61, in …Run Code Online (Sandbox Code Playgroud) 我正在附加特定路径c:\ important\log.txt中的文件
sender = 'poojagupta4112@gmail.com'
receiver = ['shubh4112@gmail.com']
message = """From: From Pooja Gupta <poojagupta4112@gmail.com>
To: To Shubha Goel <shubh4112@gmail.com>
Subject: SMTP e-mail test
This is a test e-mail message.
"""
file_name = 'C:\important\log.txt'
msg=MIMEMultipart()
msg['From'] = sender
msg['To'] = receiver
msg['Subject'] = message
msg['Date'] = email.Utils.formatdate(localtime=True)
# build the attachment
att = MIMEBase('application', 'base64')
att.set_payload(open(file_name, 'rb').read())
email.Encoders.encode_base64(att)
att.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file_name))
msg.attach(att)
print 'successfully built attachment'
try:
session = smtplib.SMTP('smtp.gmail.com',587)
print 'Starting..'
session.ehlo()
print 'ehlo executed..'
session.starttls()
print …Run Code Online (Sandbox Code Playgroud) 我看过这篇关于如何从django运行python脚本的帖子:http: //www.djangotutsme.com/how-to-run-python-script-from-django/
我试过这个例子,但在运行时遇到以下错误python manage.py runscript myscript.我安装了Python 2.7,Django 1.10和django扩展1.6.1.
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/lib/python2.7/site-packages/Django-1.10.dev20151201151517-py2.7.egg/django/core/management/__init__.py", line 349, in execute_from_command_line
utility.execute()
File "/usr/lib/python2.7/site-packages/Django-1.10.dev20151201151517-py2.7.egg/django/core/management/__init__.py", line 341, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/lib/python2.7/site-packages/Django-1.10.dev20151201151517-py2.7.egg/django/core/management/__init__.py", line 193, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/usr/lib/python2.7/site-packages/Django-1.10.dev20151201151517-py2.7.egg/django/core/management/__init__.py", line 40, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/usr/lib/python2.7/site-packages/django_extensions-1.6.1-py2.7.egg/django_extensions/management/commands/runscript.py", line 6, in <module>
from django_extensions.management.email_notifications import \
File "/usr/lib/python2.7/site-packages/django_extensions-1.6.1-py2.7.egg/django_extensions/management/email_notifications.py", line …Run Code Online (Sandbox Code Playgroud) 问题:我一直在使用Google的Python脚本示例将apk上传到Play商店并获取通过我的帐户发布的应用列表(list_apks.py和upload_apk.py).然而最近它开始打破.我试图通过这样做来更新类似的软件包google-api-python-client,但它没有帮助.oath2clientpip install --update packagename
日志:
这个如果在列出apk的时候:
Determining latest version for my.package.name...
error 25-Feb-2016 06:30:52 Traceback (most recent call last):
error 25-Feb-2016 06:30:52 File "list_apks.py", line 80, in <module>
error 25-Feb-2016 06:30:52 main()
error 25-Feb-2016 06:30:52 File "list_apks.py", line 46, in main
error 25-Feb-2016 06:30:52 credentials = client.SignedJwtAssertionCredentials(
error 25-Feb-2016 06:30:52 AttributeError: 'module' object has no attribute 'SignedJwtAssertionCredentials'
build 25-Feb-2016 06:30:52 Found latest APK version:
build 25-Feb-2016 06:30:52 Generated …Run Code Online (Sandbox Code Playgroud) python android attributeerror oauth2client google-play-developer-api
>import tensorflow
>import tensorflow.contrib
>tensorflow.contrib
module 'tensorflow.contrib' from 'D:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\__init__.py'
>import tensorflow.python
>tensorflow.python
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'tensorflow' has no attribute 'python'
Run Code Online (Sandbox Code Playgroud)
如您所见,我在cmd(win 10)中运行此代码."import tensorflow.contrib"和"import tensorflow.python"都可以,但命令"tensorflow.contrib"和"tensorflow.python"是不同的.一个返回一个目录,另一个返回AttributeError.
有没有人知道为什么?
attributeerror ×10
python ×8
python-2.7 ×3
android ×1
django ×1
linux ×1
oauth2client ×1
oop ×1
opencv ×1
pip ×1
pygame ×1
python-3.10 ×1
windows ×1