[Solved] How do I store a variable in Python

here is a simple way to do this : import random score = 0 number = int(raw_input (‘hello, please enter a times table you would like to practice \n’)) while number > 12 or number < 2: if number > 12: print ‘number is too big must be between 2 and 12 \n’ else: print … Read more

[Solved] how to remove all staff by using regular Expression [closed]

Whenever you think “I need to match a pattern”, you should think “Regular Expressions” as a good starting point. See doco. It is a little trickier since the input file is unicode. import re import codecs with codecs.open(“test.unicode.txt”,”rb”, “utf-8″) as f: words = [] for line in f.readlines(): matches = re.match(b”solution:.+\[(?P<word>\w+).*\]”, line, flags=re.U) if matches: … Read more

[Solved] How this code works in python?

In python when you try to access True/False in list as index it will consider True=1 and False=0. As a result when you wrote a[True] it actually means a[1] and a[False] means a[0]. To clarify this try a[-True] it will interpret it as a[-1] and print 9 a = [5,6,7,8,9] print(a[True]) #prints 6 print(a[False]) #prints … Read more

[Solved] Python lambda returns true even with a false condition

You’re not calling your lambda as I’ve explained in my comment, but if you want it inline you can test as: if (lambda x: True == True)(None): print(“yes”) # prints if (lambda x: False == True)(None): print(“yes”) # doesn’t print Or more general, since you’re not actually using any arguments in your lambdas: if (lambda: … Read more

[Solved] What is the Big O complexity of this program…I got O(2^n) but i am not able to find correct answer [closed]

A different argument: Your algorithm recurses until it “bottoms out” at fib(1) which returns 1. So you are getting the fibonacci number by doing (fibonacci number) calls and adding the 1s. So fib(n) is O(fib(n)). Binet’s Fibonacci formula can be written as fib(n) == int(0.5 + (phi ** n) / (5 ** 0.5)) where phi … Read more

[Solved] How do I ask a user continuously input something?

Try this: while(True): x = raw_input(“Enter something:”) if x == “”: break Essentially, this will continue to ask the user Enter something: until the user enters nothing. If you want to parse the input into numbers, then you will need to have a try:…except:… in your code, or else it will break. 4 solved How … Read more

[Solved] does (‘filename’,’w’) replace or add [closed]

f = open(‘file’,’w’) f.write(‘hi’+’\n’) f.close() output: “hi” in file nothing else, this is because ‘w’ will overwrite the file causing it to be blank before entering the text, f = open(‘file’,’a’) f.write(‘hi’+’\n’) f.close() output whatever’s in text file to begin with + “Hi”, this will add the text to the file f = open(‘file’,’r’) f.write(‘hi’+’\n’) … Read more