Python 3.x is the latest version of Python. Use this tag if you are using Python 3. Most people look in the `Python` tag so add that also.

learn more… | top users | synonyms

0
votes
1answer
18 views

A better way to print variable value

Which of the following is better coding style and why? x=2 print("We have x = %i" %x) print("We have x = "+str(x))
0
votes
1answer
55 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
50 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
36 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
67 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 ...
2
votes
1answer
38 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
25 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 ...
1
vote
2answers
177 views

Review Rock, Paper, Scissors game (Starting out with python 2nd ed.)

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. Thanks. import random tie = 0 ...
2
votes
1answer
65 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
104 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
103 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
107 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
125 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
94 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
98 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
64 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
140 views

Heavy criticism on small game

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
89 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
106 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
65 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
42 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
83 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
255 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
41 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
128 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 ...