All Questions

324
votes
11answers
201397 views

Asking the user for input until they give a valid response

I am writing a program that must accept input from the user. #note: Python 2.7 users should use `raw_input`, the equivalent of 3.X's `input` age = int(input("Please enter your age: ")) if age >= 1...
155
votes
14answers
401329 views

How can I read inputs as integers?

Why does this code not input integers? Everything on the web says to use raw_input(), but I read on Stack Overflow (on a thread that did not deal with integer input) that raw_input() was renamed to i...
228
votes
10answers
171790 views

Syntax error on print with Python 3

Why do I receive a syntax error when printing a string in Python 3? >>> print "hello World" File "<stdin>", line 1 print "hello World" ^ SyntaxError: invali...
100
votes
6answers
348584 views

input() error - NameError: name '...' is not defined

I am getting an error when I try to run this simple python script: input_variable = input ("Enter your name: ") print ("your name is" + input_variable) Lets say I type in "dude", the error I am get...
2766
votes
44answers
987520 views

How to merge two dictionaries in a single expression?

I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The update() method would be what I need, if it returned its result instead of mod...
160
votes
12answers
46369 views

Does Python have a built in function for string natural sort?

Using Python 3.x, I have a list of strings for which I would like to perform a natural alphabetical sort. Natural sort: The order by which files in Windows are sorted. For instance, the following l...
263
votes
5answers
221914 views

What's the difference between raw_input() and input() in python3.x?

What is the difference between raw_input() and input() in python3.x ?
1146
votes
43answers
592204 views

How can I represent an 'Enum' in Python?

I'm mainly a C# developer, but I'm currently working on a project in Python. How can I represent the equivalent of an Enum in Python?
50
votes
2answers
47067 views

Switch between two frames in tkinter

I have built my first few scripts with a nice little GUI on them, as the tutorials have shown me, but none of them address what to do for a more complex program. If you have something with a 'start ...
978
votes
13answers
883492 views

Convert bytes to a string?

I'm using this code to get standard output from an external program: >>> from subprocess import * >>> command_stdout = Popen(['ls', '-l'], stdout=PIPE).communicate()[0] The commun...

Previous Next