Python 3.x is the latest version of Python. Use this tag if you are specifically using Python 3. Such questions should be tagged with [python] as well.

learn more… | top users | synonyms

1
vote
0answers
18 views

Tkinter file searching program

This is my first relatively big program. First it shows an askdirectory() screen to get the path. It gets all subfolders of that path and appends them to a list. It checks every folders' items if they ...
1
vote
1answer
33 views

Python converter: number-to-English

So I wrote this function to convert a given number to its interpretation in the English language as part of the Project Euler exercises. It works fine, but I sense that it's rather sloppy and ...
1
vote
0answers
25 views

Solving cubic equations using Newton's method

I am a complete beginner to programming, and have just started python a week ago. Using the newton-raphson method, and the quadratic formula, I devised a piece of code which solves cubic equations. ...
0
votes
0answers
11 views

Why is this error being thrown? (Python 3, Pygame) [migrated]

In my game, pertinent code found here, and also at the end of this post, this error is being thrown when evaluating: Traceback (most recent call last): File ...
2
votes
1answer
71 views

Performance of modular square root

Here's Project Euler problem 451: Consider the number 15. There are eight positive numbers less than 15 which are coprime to 15: 1, 2, 4, 7, 8, 11, 13, 14. The modular inverses of these ...
2
votes
0answers
59 views

Reading columns and rows in a .csv file

I have some data in a .csv file, which looks roughly like this: [fragment1, peptide1, gene1, replicate1, replicate2, replicate3] [fragment1, peptide2, gene1, replicate1, replicate2, replicate3] ...
3
votes
2answers
135 views

Python Port Scanner

This is only my third Python script. Be brutal with me. Any tips, tricks, best practices, or better usages would be great! import socket from concurrent.futures import ThreadPoolExecutor THREADS = ...
0
votes
1answer
81 views

Fastest way for working with itertools.combinations

I need to speed up the function below: import numpy as np import itertools def combcol(myarr): ndims = myarr.shape[0] solutions = [] for idx1, idx2, idx3, idx4, idx5, idx6 in ...
2
votes
1answer
63 views

Partial data reading implementation

I'm trying to learn Python and am working a simple Tiled Map Format (.tmx) reader as practice. So as to not post too much code at once, I'm only publishing the <data> element which stores the ...
1
vote
1answer
37 views

struct.unpack on a bytearray that works under Python 2.6.6, 2.7 and 3.3?

Given the test code: import struct buf = bytearray(b'\xef\xf8\t\xf2') number = struct.unpack("<I", buf)[0] How do I cleanly make it work both under Python 2.6 and Python 3.3? The problem is that ...
1
vote
1answer
40 views

Reading Multidimensional arrays

I am building a cypher program which uses a matrix of numbers and letters. A message is encoded according to the row an collum each letter of the message is found in the matrix. I am trying to find a ...
3
votes
1answer
130 views

chess moves - pythonic

I started a chess in python for fun. i learned python on my free time but i've been doing it for a while. The code works but i just want to know if there's anything non-pythonic in my code. The ...
4
votes
2answers
138 views

Python Script to Search PirateBay

I've written a very basic Python 3 script to search ThePirateBay. Since the tracker doesn't have an API, I had to parse the HTML using BeautifulSoup. I'd like to get some reviews, I'm pretty sure the ...
0
votes
0answers
40 views

Drawing stars around the center of the screen

I have an homework as follows: Write a program where you enter from the keyboard a number n. Then n stars are drawn onto the screen, circling around the center of your screen. You can assume ...
0
votes
2answers
57 views

Counting the even digits in a number [closed]

I made a function to count the even digits in a number and when I enter a number that doesn't include any even digits. I need some one to tell me why and how to fix this. And please be simple in the ...
0
votes
2answers
41 views

Function to count the digits in a number is not working [closed]

I tried to test this function and the first test is OK, but the seconds test failes and I don't know why.... Note: I'm a beginner, so be easy and Please, don't give me complicated answers! import ...
5
votes
2answers
86 views

Copying data to a .txt, but making it look nice?

This is probably horribly sloppy, but here it is! This is a section from a large (imo) GUI program I am making. The below code is what triggers when a specific button is pressed. def ...
4
votes
4answers
180 views

Rock paper scissors

I've started learning Python recently and I wanted to test myself. So, can you tell me how many I would get out of 10 and how I could improve my future coding? import random import time ...
4
votes
1answer
43 views

Organization of complex empirical equations

I am working on coding up a set of models that are based on empirical models. These models tend to be quite complicated. In some cases, the logic used by the equations makes it impossible to compute ...
1
vote
2answers
82 views

Taking the sum and average of the keyboard inputs

I have an assignment as follows: Write a program where you can enter from the keyboard up to 10 integers. If the number entered is equal to -99, stop reading numbers from the keyboard and ...
2
votes
4answers
93 views

Printing a rectangle

I have an assignment as follows: Write a program which reads from the keyboard two integers n and m, and a character c from the keyboard. This program should define and call a function: ...
2
votes
1answer
68 views

Drawing a table in Python3

I have created a module to draw text tables. The relevant parts are below and here's the full code for txtable). class Row(Txtable): [...] def format(self, col_widths): """Return the ...
1
vote
0answers
73 views

A program that deliberately makes “mistakes” [closed]

I am confused about an assignment question. We are simply asked to code a program which deliberately makes mistakes. The assignment question is as follows: Write a program that deliberately makes ...
0
votes
1answer
35 views

Best way to print the value of a variable?

Which of the following is better coding style and why? print("We have x = %i" %x) print("We have x = "+str(x))
0
votes
1answer
113 views

Find scrambled words within word list

I'm just getting started with Python -- completing a simple exercise from a practice website. My code works -- just looking for feedback on better ways to achieve the goal. Goal: Find the ...
1
vote
2answers
62 views

How to make this random number game better?

I'm a newbie trying to teach myself Python. Could somebody tell me if there is a better way to write this? I am also curious as to whether or not there is anything I should not do. This code does ...
0
votes
2answers
49 views

How to enumerate the internal nodes and leaves of a tree more elegantly?

Might there be a simpler way to write this code? This is the code I have, and I'd like to learn to write code more efficiently. Thank you. def leaves_and_internals(self): """(BTNode) -> ...
2
votes
1answer
77 views

How to turn this working A/B optimization program into more pythonic code?

I have a working solution for the given problem of A/B Optimization, but you as an experienced Python (3) developer will agree with me, that this solution is not very pythonic at all. My Question ...
3
votes
2answers
179 views

Returning a list of the values from the binary tree

I want to return a list of the values from the binary tree. Is there a shorter and more efficient way to write the method for numbers? class BTNode(object): """A node in a binary tree.""" ...
2
votes
1answer
54 views

Subscriptable/Indexable generator

I'm not a Python developper, but I enjoy programming with it, and for a project I wanted to have generators that I can easily index. Using python's slice model is obviously the way to go, and here's ...
1
vote
1answer
31 views

More efficient method of checking user input

I am creating a simple Boxing sim engine and have got things working fairly well. A few months ago I was instructed to avoid copy and pasting code and to try and "conserve my logic". Anyways, I feel I ...
2
votes
2answers
486 views

Rock, Paper, Scissors game assignment

This is an assignment that I have already turned in. It works great, but I was wondering if there are any flaws in the code, or how to write it in a more pythonic way. import random tie = 0 pcWon = 0 ...
2
votes
1answer
107 views

Object Composition

So I have this Pygame 3.3.2 code of 2 classes. I tried to make it as simpler as possible, but ofcourse to show the problems I have with thedesign. import pygame import sys class MainWindow(object): ...
1
vote
0answers
108 views

Do I need inheritance or no?

I'm using Pygame to create a small game with few rectangle shapes. However, I want to learn more about classes, and I'm trying to use them. I'm very new to classes and inheritance, but I want to ...
3
votes
2answers
173 views

Building a number pyramid?

I have been working a number pyramid program. This is a question from Y. Daniel Laing's Introduction to programming using Python (which is based on Python 3.2). The question is from chapter 5, and it ...
2
votes
4answers
132 views

What are the drawbacks of this “multi-key” dictionary?

I was making a relatively large-scale project in Python as a learning experiment, and in the process I found that it would be useful to have a map (or, in Python, I guess they're called dictionaries) ...
4
votes
2answers
200 views

Simple Age Converter

Ok, code reviewers, I want you to pick my code apart and give me some feedback on how I could make it better or more simple. In my opinion this is the best code I have written, being that I'm new to ...
2
votes
1answer
117 views

Python infinite product generator

I have the code below to get an infinite generator of the products of an iterable (e.g. for the iterable "ABC" it should return A, B, C, AA, AB, AC, BA, BB, BC, CA, CB, CC, AAA, AAB, AAC etc. for ...
2
votes
3answers
369 views

Python Calculator

thelist = ["Add", "add", "Multiply", "multiply", "Divide", "divide","Subtract", "subtract"] def Multiply(x,y): z = x * y print(z) def Divide(x,y): x = float(x) y = float(y) z = x / y print(z) def ...
2
votes
1answer
101 views

Super simple spam program

x = int(input("Choose the number of times you want to spam. ")) y = input("What message do you want to spam? ") w = x + 1 for i in range(1,w): print(y) Any way to make this program better? ...
6
votes
2answers
175 views

Simple anagram game with names of colors

This is a very simple anagram game that plays only with names of colors. I tried to focus on code design. I also wanted it to support further updates. But, my init seems smelly and you will notice ...
1
vote
1answer
119 views

Automatically indent a whole block of text into str.format

I wondered if there was any better way of doing so. I had a hard time figuring it out (it seemed nobody else already tried to do so). import re import string class BetterFormat(string.Formatter): ...
5
votes
1answer
123 views

Review Python script for getting the stats of codereview.SE into a file for analysis

I wrote a script for getting the stats of codereview.SE from the front page into a file. Github link if someone prefers reading there. Here is data_file.txt's current contents 23-08-2013 dd-mm-yyyy, ...
2
votes
1answer
72 views

Streamlining repetitive class definitions in python with a class_factory() function

I forked this repo to be more concise. The code is here. I'll paste it below since that seems to be the style. I removed the class definitions at the bottom that I didn't change -- the edit I'm ...
0
votes
0answers
60 views

How to clean up python decorators

I wrote some type checking decorators for python. One checks the type that a funciton returns, the other checks the type that a function accepts. import functools def returns(t): """ Calls a ...
4
votes
1answer
90 views

Questions about opinions on prime number generator

What do you consider the readability of this program? Do you think it's easy to understand what the variables and functions do just by reading their names? Would you prefer more, less, or this amount ...
4
votes
2answers
399 views

counting letters in text file

As a beginner Python programmer, I wrote a simple program that counts how many times each letter appears in a text file. It works fine, but I'd like to know if it's possible to improve it. Thanks :) ...
-1
votes
1answer
42 views

Refactor Python Code

I'm looking for a way to make my code more simple. This code takes a list of vectors and returns all subset of that list that sum up to another vector. For example: {'a','b','c'} is the subset ...
1
vote
2answers
154 views

Is there a more efficient way for code reuse in Python - Largest prime factor?

I was doing timing with variations of function largest_prime_factor. I was able to write a better one largest_prime_factor2. There were some repetitions in the new function so I wrote another function ...
2
votes
0answers
98 views

When is (if ever) monkey-patching acceptable? Is there a better solution to this?

I am working on a project that involves xml parsing, and for the job I am using xml.dom.minidom. During development I identified several patterns of processing that I refactored into discrete methods. ...