[Solved] Python (if, else, elif)

if louis_inventory and louis == 0: This will catch every cases where louis_inventory is truthy – e.g. anything nonzero in case of an int – and louis is zero. So the first elif case is unreachable. I have no idea what exactly you are trying to do but this might fix it: def example(): if … Read more

[Solved] Python Code Fails

The thing is that range and xrange are written in C, hence they are prone to overflows. You need to write your own number generator to surpass the limit of C long. def my_range(end): start = 0 while start < end: yield start start +=1 def conforms(k,s): k = k + 1 if s.find(“0” * … Read more

[Solved] Python urllib2 or requests post method [duplicate]

This is not the most straight forward post request, if you look in developer tools or firebug you can see the formdata from a successful browser post: All that is pretty straight forward bar the fact you see some : embedded in the keys which may be a bit confusing, simpleSearchSearchForm:commandSimpleFPSearch is the key and … Read more

[Solved] Coding a function in python wihch count the number of occurances [closed]

Use the collections module to use the Counter function. Such as: import collections def carCompt(*args): return collections.Counter(“”.join(map(str.upper, args))) This will return {‘A’:5, ‘D’:1, ‘E’:3, ‘H’:2, ‘L’:1, ‘N’:1, ‘O’:1, ‘P’:2, ‘R’:2, ‘S’:1, ‘X’:1} If you want it to be case sensitive then leave it like: import collections def carCompt(*args): return collections.Counter(“”.join(args)) which will return {‘a’: 4, … Read more

[Solved] How do I refresh tkinter window totally in python with a refresh button [closed]

The simplest way is to implement the entire window as a subclass of a tk Frame, and then destroy and recreate it. Your code might look something like this: import Tkinter as tk class Example(tk.Frame): def __init__(self, parent): tk.Frame.__init__(self, parent) <other code here…> class Application: def __init__(self): self.root = tk.Tk() self.frame = None refreshButton = … Read more

[Solved] What is the error in following python code

First things first, I think the code you originally posted (with Hello(sys.argv[0])) is not what you actually have. It doesn’t match the error, which states sys.argv[1], so what you probably have is: def main(): Hello(sys.argv[1]) As to the error then, it’s because you haven’t provided an argument when running. You need to do so, such … Read more

[Solved] How to allow caps in this input box program for pygame?

If you change the corresponding code to: elif inkey <= 127: if pygame.key.get_mods() & KMOD_SHIFT or pygame.key.get_mods() & KMOD_CAPS: # if shift is pressed or caps is on current_string.append(chr(inkey).upper()) # make string uppercase else: current_string.append(chr(inkey)) # else input is lower That should work. If you want more info on keyboard modifier states look here 1 … Read more

[Solved] Replace [1-2] in to 1 in Python

You could use a regex like: ^(.*)\[([0-9]+).*?\](.*)$ and replace it with: $1$2$3 Here’s what the regex does: ^ matches the beginning of the string(.*) matches any character any amount of times, and is also the first capture group\[ matches the character [ literally([0-9]+) matches any number 1 or more times, and is also the second … Read more

[Solved] Using zip_longest on unequal lists but repeat the last entry instead of returning None

itertools.izip_longest takes an optional fillvalue argument that provides the value that is used after the shorter list has been exhausted. fillvalue defaults to None, giving the behaviour you show in your question, but you can specify a different value to get the behaviour you want: fill = a[-1] if (len(a) < len(b)) else b[-1] for … Read more