python-3.3

Matplotlib Basemap animation

泄露秘密 提交于 2019-11-29 08:29:18
I'm using basemap to plot some points on a map, and I want to add any kind of animation to it. It could literally serve no purpose at all, as long as it is an animation it would be nice. This is what I currently have to make the map, from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt import numpy as np import matplotlib.animation as animation map = Basemap(projection='robin', resolution = 'l', area_thresh = 1000.0, lat_0=0, lon_0=-130) map.drawcoastlines() map.drawcountries() map.fillcontinents(color = 'gray') map.drawmapboundary() map.drawmeridians(np.arange(0, 360, 30))

SyntaxError: unexpected EOF while parsing

蓝咒 提交于 2019-11-29 06:37:00
问题 I have no idea why this does not work please help import random x = 0 z = input('?') int(z) def main(): while x < z: n1 = random.randrange(1,3) n2 = random.randrange(1,3) t1 = n1+n2 print('{0}+{1}={2}'.format(n1,n2,t1) When i run this it outputs this error File "/Users/macbook/Documents/workspace/gamlir_filar/samlagning.py", line 12 ^ SyntaxError: unexpected EOF while parsing I am using eclipse and python 3.3 and i have no idea why this happens. It sometimes outputs errors like this. 回答1: You

Random word generator- Python

独自空忆成欢 提交于 2019-11-28 22:23:06
问题 So i'm basically working on a project where the computer takes a word from a list of words and jumbles it up for the user. there's only one problem: I don't want to keep having to write tons of words in the list, so i'm wondering if there's a way to import a ton of random words so even I don't know what it is, and then I could enjoy the game too? This is the coding of the whole program, it only has 6 words that i put in: import random WORDS = ("python", "jumble", "easy", "difficult", "answer"

Python : meaning of end='' in the statement print(“\t”,end='') [duplicate]

做~自己de王妃 提交于 2019-11-28 21:32:36
问题 This question already has an answer here: python print end=' ' 13 answers This is the function for printing all values in a nested list (taken from Head first with Python). def printall(the_list, level): for x in the_list: if isinstance(x, list): printall(x, level=level + 1) else: for tab_stop in range(level): print("\t", end='') print(x) The function is working properly. The function basically prints the values in a list and if there is a nested list then it print it by a tab space. Just for

Pygame attribute, init()

久未见 提交于 2019-11-28 14:09:28
I'm trying to use Pygame with Python 3.3 on my windows 8 laptop. Pygame installed fine and when I import pygame it imports fine as well. Although when I try to execute this small code: import pygame pygame.init() size=[700,500] screen=pygame.display.set_mode(size) I get this error: Traceback (most recent call last): File "C:\Users\name\documents\python\pygame_example.py", line 3, in <module> pygame.init() AttributeError: 'module' object has no attribute 'init' I used pygame-1.9.2a0-hg_56e0eadfc267.win32-py3.3 to install Pygame. Pygame is installed in this location 'C:\PythonX' and Python 3.3

Tkinter create image function error (pyimage1 does not exist)

ぐ巨炮叔叔 提交于 2019-11-28 14:04:52
I'm a student from the outside world with no previous programming experience. I have been learning Python as an extension of my math class. I have been trying to create a program that generates fractals using Tkinter. The code works well on its own, but the inclusion of a user-input GUI causes it to give an error: Exception in Tkinter callback Traceback (most recent call last): File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__ return self.func(*args) File "C:\Python33\FractalGUI.py", line 74, in fractals canvas.create_image((0, 0), image = img, state = "normal", anchor =

sum of nested list in Python

不羁岁月 提交于 2019-11-28 11:20:39
I try to sum a list of nested elements e.g, numbers=[1,3,5,6,[7,8]], sum=30 I wrote the following code def nested_sum(L): sum=0 for i in range(len(L)): if (len(L[i])>1): sum=sum+nested_sum(L[i]) else: sum=sum+L[i] return sum The above code gives following error: object of type 'int' has no len() I also tried len([L[i]]), still not working Anyone can help? BTW, it is Python 3.3 You need to use isinstance to check whether an element is a list or not. Also, you might want to iterate over the actual list, to make things simpler. def nested_sum(L): total = 0 # don't use `sum` as a variable name for

Writing a .CSV file in Python that works for both Python 2.7+ and Python 3.3+ in Windows

我怕爱的太早我们不能终老 提交于 2019-11-28 10:54:39
EDIT: I put it in the title, but just realized I didn't mention it in the body. This seems to be specific to Windows. I'm having a hard time writing output using the csv Python module in a script that works with both Python 2.7 and 3.3. First try which works as expected in Python 2.7: with open('test.csv', 'wb') as csv_file: writer = csv.DictWriter(csv_file, ['header1', 'header2']) writer.writeheader() for item in items: writer.writerow(item) However, when that same thing is run in Python 3.3 you wind up with: TypeError: 'str' does not support the buffer interface So I change 'wb' to 'wt' and

Python float to ratio

非 Y 不嫁゛ 提交于 2019-11-28 10:17:04
I try get ration of variable and get unexpected result. Can somebody explain this? >>> value = 3.2 >>> ratios = value.as_integer_ratio() >>> ratios (3602879701896397, 1125899906842624) >>> ratios[0] / ratios[1] 3.2 I using python 3.3 But I think that (16, 5) is much better solution And why it correct for 2.5 >>> value = 2.5 >>> value.as_integer_ratio() (5, 2) Use the fractions module to simplify fractions: >>> from fractions import Fraction >>> Fraction(3.2) Fraction(3602879701896397, 1125899906842624) >>> Fraction(3.2).limit_denominator() Fraction(16, 5) From the Fraction.limit_denominator()

subprocess and Type Str doesnt support the buffer API

有些话、适合烂在心里 提交于 2019-11-28 09:42:34
I have cmd = subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE) for line in cmd.stdout: columns = line.split(' ') print (columns[3]) have error in line 3 Type Str doesnt support the buffer API. What am i doing wrong i am on Python 3.3 You are reading binary data, not str , so you need to decode the output first. If you set the universal_newlines argument to True , then stdout is automatically decoded using the result of the locale.getpreferredencoding() method (same as for opening text files): cmd = subprocess.Popen( 'dir', shell=True, stdout=subprocess.PIPE, universal_newlines=True)