Re: [Tutor] Shifting arrays as though they are a 'word'
On Fri, Oct 05, 2018 at 04:17:53PM -0400, Chip Wachob wrote: > Hello, > > I was not able to find any answers in the archive on this one. > > I'm wondering if this task can be done in a better way than what I've > attempted.. > > I have an array of bytes. Up to 64, which makes for 512 bits. > > I am reading these bytes in serially, and once I have a collection of > them, I want to shift them by 'n' bits. The size of the array and the > number of bits are both variable up to the limit of 64/512. Code speaks much more strongly than description. Can you demonstrate an example of: - the actual data you have; - the result you want; - what you've tried? The answers we give will be very different according to what you have, e.g.: data = [0, 1, 2, 3, 4, 5, 6, 7] # eight bytes in a list data = b"\0\x01\x02\x03\x04\x05\x06\x07" # eight bytes in a bytes object data = 0x0001020304050607 # eight bytes in an int -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] [spoiler] Re: Shifting arrays as though they are a 'word'
Chip Wachob wrote: > Hello, > > I was not able to find any answers in the archive on this one. > > I'm wondering if this task can be done in a better way than what I've > attempted.. > > I have an array of bytes. Up to 64, which makes for 512 bits. > > I am reading these bytes in serially, and once I have a collection of > them, I want to shift them by 'n' bits. The size of the array and the > number of bits are both variable up to the limit of 64/512. > > Now, I've played around with looping through the bytes and taking the > LSByte and shifting it by 'n' bits using >> or << and that works for > the first byte. But then I need to take the next byte in the sequence > and shift it in the opposite direction by 8-n bits using << or >> > (opposite the LSByte direction), respectively. Then I OR the two > bytes and save them into the location of the LSByte and then move to > the next byte in the sequence and so on. While this works most of the > time, I sometimes get strange behavior at the 'fringes' of the bytes. > Sometimes I end up with zero, or the shift seems to 'roll over'. > > I'm thinking that maybe there's a way to treat the array / list and > shift allowing the bits to transfer from byte to byte as needed. > Maybe by concatenating the bytes into one huge word and then breaking > it apart again? > > I'm thinking that you folks out there know of a built-in function, or, > an easier and more predictable way to accomplish the same. Here are two ways to implement the left shift: def bitshift(b, n, byteorder="big"): size = len(b) + (n + 7) // 8 shifted = int.from_bytes(b, byteorder) << n return shifted.to_bytes(size, byteorder) def bitshift2(b, n): nbytes, nbits = divmod(n, 8) if nbits: a = [0] for bb in b: hi, lo = divmod(bb << nbits, 256) a[-1] |= hi a.append(lo) b = bytes(a) return b + b"\x00" * nbytes assert bitshift(b"\xaa\xbb", 12) == b"\x0a\xab\xb0\x00" assert bitshift2(b"\xaa\xbb", 12) == b"\x0a\xab\xb0\x00" ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] guess my number game (reversed)
Hi, sorry for "resurrecting" this thread. I have tried doing as suggested but with no luck. I now managed to make the programme work "partially". It works as desired for "too high" and "right" answer input, but when the users input "too low" the programme closes. here is what the code looks like right now. print ("Think of a number from 1 to 100, and I will try to guess it") input ("press enter when ready...") import random number = random.randint(1,100) print (number) answer = input ("right, too low, too high?") while answer != "right": if answer == "too high": number2 = random.randint (1,number) print (number2) elif answer == "too low": number3 = random.randit (number,100) print (number3) answer = input ("is this right, too high, or too low?") print ("I finally guessed your number") input ("\n\n press the enter key to exit") any help is really appreciated :) Chiara Il giorno ven 1 giu 2018 alle ore 18:15 Alan Gauld via Tutor < tutor@python.org> ha scritto: > On 01/06/18 14:00, chiara pascucci wrote: > > > the user's input. The programme works fine if the it guesses the number > > right on its first try, but when the users inputs "too low" or "too high" > > an infinite loop is returned. I thinkI have done something wrong in my > > while loop and that my sentry variable is somehow wrong > > Nope, it's simpler than that. > > You only ask the user for input once, outside the loop. > Move the input() statement inside the loop before the if > statements and all should be well. (You will also need to > initialise answer to some value - an empty string > say - before entering the while loop.) > > > print("think of a number from 1 to 100") > > print("I will try and guess it!") > > > > import random > > > > number = random.randint(1,100) > > print(number) > > > > answer = input("is this right, too high, or too low?") > > > > while answer != "right": > > if answer == "too high": > > number2 = random.randint(1,number) > > print(number2) > > elif answer == "too low": > > number3 = random.randint(number,100) > > print(number3) > > > > print("I finally guessed your number. Thank you for playing") > > HTH > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] guess my number game (reversed)
chiara pascucci wrote: > Hi, > > sorry for "resurrecting" this thread. I have tried doing as suggested but > with no luck. > I now managed to make the programme work "partially". It works as desired > for "too high" and "right" answer input, but when the users input "too > low" the programme closes. > here is what the code looks like right now. > > print ("Think of a number from 1 to 100, and I will try to guess it") > input ("press enter when ready...") > > import random > > number = random.randint(1,100) > print (number) > > answer = input ("right, too low, too high?") > > while answer != "right": > if answer == "too high": > number2 = random.randint (1,number) > print (number2) > > elif answer == "too low": > number3 = random.randit (number,100) > print (number3) > > answer = input ("is this right, too high, or too low?") > > print ("I finally guessed your number") > > input ("\n\n press the enter key to exit") > > any help is really appreciated :) > > Chiara When you run your script from the command line it not just terminates, it also prints an error message and important debug information (called "traceback") that helps you fix the code: $ python3 guess_number_chiara.py Think of a number from 1 to 100, and I will try to guess it press enter when ready... 63 right, too low, too high?too low Traceback (most recent call last): File "guess_number_chiara.py", line 17, in number3 = random.randit (number,100) AttributeError: 'module' object has no attribute 'randit' The problem is in line 17 and there's a module that doesn't have a randit attribute. The only module mentioned in the line is random which indeed does not contain a randit() function, the actual function is named randint(). So the bug turns out to be a misspelt function name. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Pip issue
On 10/05/2018 09:50 AM, Adam Eyring wrote: > Hi all, > I just joined since I'm new to working in python for data management and > have been stumped on using pip. My understanding is that it should be in my > scripts folder of Windows Python3.6 (downloaded from python.org), but it's > not, so it doesn't work to run "pip install ". I see there's > get-pip.py, but the documentation on using it is confusing. Thanks for your > help. > Adam wherever you're told to pip install foo instead do python -m pip install foo it should take care of the path problems you're having, and is now the recommended way anyway since that way the things you install are sure to match the running python version. (the "python -m name" stanza means "python, please run 'name' as a module) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor