[Solved] Defining function with an argument, employs while loop and returns largest power of 2 that is less than or equal to number [closed]

A simple while loop works, just make sure you divide the number by 2, otherwise you’ll get the NEXT power of 2. def function(number): x = 1 while x <= number: x *= 2 return x / 2 solved Defining function with an argument, employs while loop and returns largest power of 2 that is … Read more

[Solved] I would like to total the amount of correct answers and display it at the end getting error local variable referenced

You can make count global: count = 0 def intro(start): if start == “yes” or start == “y”: print(“Lets begin.”) else: print(“Thanks for checking it out! Bye Bye!”) def question1(): global count count += 1 return count def question2(): global count count += 1 return count def main(): print(“Hello There! Welcome to an all new … Read more

[Solved] I wanna resist id in serial number

UUID is not a number but you can generate a new UUID simple import uuid from django.db import models # Create your models here. def get_next(): return uuid.uuid4() class Users(models.Model): id = models.UUIDField(primary_key=True, default=get_next, editable=False) sex = models.CharField(max_length=100, null=True, default=None) age = models.CharField(max_length=100, null=True, default=None) By default Django will store Integer Primary keys. In order … Read more

[Solved] Python function,issue with input file [closed]

The format of the input file depends on the extension that you use, a .txt file will be a Tab Separated Values (tsv) file while a .csv file will be a Comma Separated Values (csv) file (please note that this is not a general convention, it is something that was decided by that colleague of … Read more

[Solved] Python re: how to lowercase some words [closed]

Wanted solution using regex, here you are: >>> import re >>> s = “StringText someText Wwwwww SomeOtherText OtherText SOMETextMore etc etc etc” >>> def replacement(match): … return match.group(1).lower() >>> re.sub(r'([sS]\w+)’, replacement, s) ‘stringtext sometext Wwwwww someothertext OtherText sometextmore etc etc etc’ 10 solved Python re: how to lowercase some words [closed]

[Solved] Python script to ping linux server

You can use urllib2 and socket module for the task. import socket from urllib2 import urlopen, URLError, HTTPError socket.setdefaulttimeout( 23 ) # timeout in seconds url=”http://google.com/” try : response = urlopen( url ) except HTTPError, e: print ‘The server couldn\’t fulfill the request. Reason:’, str(e.code) except URLError, e: print ‘We failed to reach a server. … Read more

[Solved] What does the get method do on dictionaries? [closed]

If the key argument is in switcher, the .get() method returns the value for the key. If the key is not in the dictionary, the method returns the optional “nothing”. def numbers_to_strings(argument): switcher = {0: “zero”, 1: “one”, 2: “two”} return switcher.get(argument, “nothing”) Calling the above function with a key that is in the dictionary: … Read more

[Solved] Python Iterate Over String

Something like this? string = “AABBCCCASSDSFGDFGHDGHRTFBFIDHFDUFGHSIFUGEGFGNODN” for n in range(len(string)-15): print(string[n:n+16]) You have to iterate over every character up to the last character that has 16 characters after it (so the length of the string, minus 15 (because indexing starts at 0) : len(string)-15), and then print the string sliced at that starting index up … Read more