From phillor9 at gmail.com Wed Dec 6 23:07:15 2023 From: phillor9 at gmail.com (Phil) Date: Thu, 7 Dec 2023 14:07:15 +1000 Subject: [Tutor] Serial communication problem Message-ID: <1558cac6-3a53-4f66-a23e-ac9f42e87b18@gmail.com> Thank you for reading this. I've come to the end of my limited abilities with this and I know that this is a long shot but someone may spot an obvious error. Two days of Internet searching has not led to a solution. This code sends a "1" and "0" to a pico board to turn it's LED on and off. So far so good and this works. This code then waits for the pico to send "ok" as an acknowledgement. This also works. Next, this code sends an "s" asking for the LED's status. This where the problem is. The pico does not receive the "s", yet the "1" and "0" are received using the same read method. It seems to me that perhaps the serial mechanism is missing something between sending characters, receiving a response and then sending another character. Initially, I thought that using the " with" block may have been the cause of the problem but it's not. import time from serial_write import SerialWrite # these are my simple classes containing a write, a read from serial_read import SerialRead # and close methods # header byte to indicate start of response HEADER_BYTE = "ok" with SerialWrite("/dev/ttyACM0", 9600) as ser_write, SerialRead( ??? "/dev/ttyACM0", 9600 ) as ser_read: ??? while True: ??????? for char in (b"1", b"0"): ??????????? ser_write.write(char) ??????????? time.sleep(1) ??????????? # Wait for header byte to indicate response is ready ??????????? while True: ??????????????? print("in the loop") ??????????????? byte = ser_read.read() ??????????????? print(byte) ??????????????? if byte == HEADER_BYTE: ??????????????????? print(f"after if: {byte}") ??????????????????? ser_write.write(bytes("s", "utf-8"))? # b"s") ??????????????????? break ??????????? print("outside the loop") ??????????? # Read the actual response data ??????????? # data = ser_read.read() ??????????? time.sleep(2) ??????????? data = ser_read.read() ??????????? print(f"Received: {data}") ??????????? time.sleep(1) ??????????? print() ??????????? print() If it helps, this code prints: in the loop ok after if: ok outside the loop Received: ok The last line should the LED's status and not the acknowledgement "ok" -- Regards, Phil From cs at cskk.id.au Thu Dec 7 01:02:05 2023 From: cs at cskk.id.au (Cameron Simpson) Date: Thu, 7 Dec 2023 17:02:05 +1100 Subject: [Tutor] Serial communication problem In-Reply-To: <1558cac6-3a53-4f66-a23e-ac9f42e87b18@gmail.com> References: <1558cac6-3a53-4f66-a23e-ac9f42e87b18@gmail.com> Message-ID: On 07Dec2023 14:07, Phil wrote: >Next, this code sends an "s" asking for the LED's status. This where >the problem is. The pico does not receive the "s", yet the "1" and "0" >are received using the same read method. I would be inclined to verify this for real. Write a small function: def sread(ser): data = ser.read() print("read", repr(data), "from", ser) return data and call `sread(ser_read)` wherever you're calling `ser_read.read()`. The objective here is to see all the data received, regardless of where your `read()` is taking place. (If `SerialReader` is your own class, it would be even better to stil the above `print()` in its `read()` method instead.) I don't, for example, see anything in your code about receiving the `0` or `1` status result yet you say they're received. So print out everything received, to be sure. Cheers, Cameron Simpson From alan.gauld at yahoo.co.uk Thu Dec 7 04:20:53 2023 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 7 Dec 2023 09:20:53 +0000 Subject: [Tutor] Serial communication problem In-Reply-To: <1558cac6-3a53-4f66-a23e-ac9f42e87b18@gmail.com> References: <1558cac6-3a53-4f66-a23e-ac9f42e87b18@gmail.com> Message-ID: On 07/12/2023 04:07, Phil wrote: > This code sends a "1" and "0" to a pico board to turn it's LED on and > off. So far so good and this works. Presumably you see the LED turn on/off? That's how you know it works, right? > This code then waits for the pico to send "ok" as an acknowledgement. > This also works. Because you enter the if statement? > Next, this code sends an "s" asking for the LED's status. This where the > problem is. The pico does not receive the "s", yet the "1" and "0" are > received using the same read method. How do you know the board does not receive the 's'? Is there something that happens on the board that tells you when it receives it? How do you know the board is not receiving but ignoring it? You say it should return the LED status. But what exactly would that look like? BTW, shouldn't you be converting the received signals to strings? You may get away with simple messages like OK but I assume the data received is actually bytes? > ??????????? # Wait for header byte to indicate response is ready > ??????????? while True: > ??????????????? print("in the loop") > ??????????????? byte = ser_read.read() > ??????????????? print(byte) > ??????????????? if byte == HEADER_BYTE: > ??????????????????? print(f"after if: {byte}") > ??????????????????? ser_write.write(bytes("s", "utf-8"))? # b"s") > ??????????????????? break I'd normally write this like: STATUS = b"s" while True: byte = ser_read.read() if byte == HEADER_BYTE: break ser_write.write(STATUS) The write() is not logically part of the loop, it's a one off event so should be outside. But that's just a cognitive dissonance type issue... > ??????????? print("outside the loop") > ??????????? # Read the actual response data > ??????????? # data = ser_read.read() > ??????????? time.sleep(2) What does the sleep() do? Are you sure the board hasn't removed the data in the interim? > outside the loop > Received: ok > > The last line should the LED's status and not the acknowledgement "ok" -- 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 From phillor9 at gmail.com Thu Dec 7 16:27:50 2023 From: phillor9 at gmail.com (Phil) Date: Fri, 8 Dec 2023 07:27:50 +1000 Subject: [Tutor] Serial communication problem In-Reply-To: References: <1558cac6-3a53-4f66-a23e-ac9f42e87b18@gmail.com> Message-ID: <29fb94bf-610d-4cc3-ab08-fec527f635c6@gmail.com> Thank you Alan, Cameron and Rauf for your replies. I know that the "1" and "0" are received by the pico because the LED turns on and off. I know that the "ok" ready signal is sent by the pico because the python code prints "ok". The pico seems to be ignoring the "s" sent by the python code, I'm not certain of that. Debugging the running? pico code is difficult, I'm relying on print statements. The delays are sprinkled among the code just to be sure that the serial transmissions aren't being overwhelmed. The delays haven't helped. Here is the latest python code: # synchronised write and read to and from the pico import time import serial with serial.Serial("/dev/ttyACM0", 9600) as ser: ??? while True: ??????? for char in (b"1", b"0"): ??????????? ser.write(char) ??????????? time.sleep(1) ??????????? # Wait for acknowledgment ??????????? while True: ??????????????? print("in the loop") ??????????????? ready = ser.readline().decode().strip("\r\n") ??????????????? print(ready) ??????????????? if ready == "ok": ??????????????????? print(f"after if: {ready}") ??????????????????? time.sleep(1) ??????????????????? #ser.flush() ??????????????????? #ser.reset_input_buffer() ??????????????????? #ser.reset_output_buffer() ??????????????????? break ??????????? ser.write(b"s") ??????????? print("outside the loop") ??????????? time.sleep(2) ??????????? data = ser.readline().decode().strip("\r\n") ??????????? print(f"Received: {data}") ??????????? time.sleep(1) ??????????? print() ??????????? print() And here is the pico code: import uselect import sys import machine import utime class LEDController: ??? def __init__(self): ??????? self.led = machine.Pin('LED', machine.Pin.OUT) ??????? self.led_status = 'led off' ??? def set_led_status(self, new_ch): ??????? if new_ch == '1': ??????????? self.led.value(1) ??????????? self.led_status = 'led on' ??????????? #print(self.led_status) ??????? elif new_ch == '0': ??????????? self.led.value(0) ??????????? self.led_status = 'led off' ??? def get_led_status(self): ??????? return self.led_status ??????? #return "X" ??? def read_char(self): ??????? poll_obj = uselect.poll() ??????? poll_obj.register(sys.stdin,1) ??????? if poll_obj.poll(0): ??????????? #read one character ??????????? ch = sys.stdin.read(1) ??????? else: ??????????? ch = None ??????? return ch ??? def send_acknowledgement(self): ??????? print('ok') controller = LEDController() while True: ??? new_ch = controller.read_char() ??? print(new_ch) ??? if new_ch is not None: ??????? controller.set_led_status(new_ch) ??????? controller.send_acknowledgement() ??????? # After sending acknowledgement, get LED status ??????? status_request = controller.read_char() ??????? #utime.sleep(1) ??????? #print(status_request) ??????? #status_request = 's' ??????? if status_request == 's': ??????????? led_status = controller.get_led_status() ??????????? print(led_status) ??????????? #print("Z") utime.sleep(.5) I just ran this project and I see the LED is not blinking, so I've stuffed something up. I've got a medical appointment so I've go to scoot. I'm sorry to be such a pest. -- Regards, Phil From phillor9 at gmail.com Thu Dec 7 23:34:55 2023 From: phillor9 at gmail.com (Phil) Date: Fri, 8 Dec 2023 14:34:55 +1000 Subject: [Tutor] Serial communication problem In-Reply-To: References: <1558cac6-3a53-4f66-a23e-ac9f42e87b18@gmail.com> Message-ID: <3c660fbc-084a-4e2e-9eaa-d63e53fe2faf@gmail.com> I've since had a bright idea but disappointingly it doesn't work. The returned result is still "None". Pico code: ??????? # Wait for a maximum of 1 second for the status request ??????? status_request = None ??????? start_time = utime.ticks_ms() ??????? while utime.ticks_diff(utime.ticks_ms(), start_time) < 1000: ??????????? status_request = controller.read_char() ??????????? #print(status_request) ??????????? if status_request is not None: ??????????????? break ??????? print(f"status_request: {status_request}") The "s" is sent by the python programme but for some unfathomable reason the pico code is ignoring it. -- Regards, Phil From alan.gauld at yahoo.co.uk Tue Dec 12 13:46:31 2023 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 12 Dec 2023 18:46:31 +0000 Subject: [Tutor] Serial communication problem In-Reply-To: <29fb94bf-610d-4cc3-ab08-fec527f635c6@gmail.com> References: <1558cac6-3a53-4f66-a23e-ac9f42e87b18@gmail.com> <29fb94bf-610d-4cc3-ab08-fec527f635c6@gmail.com> Message-ID: On 07/12/2023 21:27, Phil wrote: > Thank you Alan, Cameron and Rauf for your replies. > > I know that the "1" and "0" are received by the pico because the LED > turns on and off. > > I know that the "ok" ready signal is sent by the pico because the python > code prints "ok". > > The pico seems to be ignoring the "s" sent by the python code, I'm not > certain of that. Debugging the running? pico code is difficult, I'm > relying on print statements. > > The delays are sprinkled among the code just to be sure that the serial > transmissions aren't being overwhelmed. The delays haven't helped. > > Here is the latest python code: > > # synchronised write and read to and from the pico > > import time > import serial > > with serial.Serial("/dev/ttyACM0", 9600) as ser: > ??? while True: > ??????? for char in (b"1", b"0"): > ??????????? ser.write(char) > ??????????? time.sleep(1) > > > ??????????? # Wait for acknowledgment > ??????????? while True: > ??????????????? print("in the loop") > > ??????????????? ready = ser.readline().decode().strip("\r\n") > ??????????????? print(ready) > > ??????????????? if ready == "ok": > ??????????????????? print(f"after if: {ready}") > > ??????????????????? time.sleep(1) > ??????????????????? #ser.flush() > ??????????????????? #ser.reset_input_buffer() > ??????????????????? #ser.reset_output_buffer() > ??????????????????? break > > ??????????? ser.write(b"s") > ??????????? print("outside the loop") > ??????????? time.sleep(2) > > ??????????? data = ser.readline().decode().strip("\r\n") > ??????????? print(f"Received: {data}") > > ??????????? time.sleep(1) > ??????????? print() > ??????????? print() > > And here is the pico code: > > import uselect > import sys > import machine > import utime > > > class LEDController: > ??? def __init__(self): > ??????? self.led = machine.Pin('LED', machine.Pin.OUT) > ??????? self.led_status = 'led off' > > ??? def set_led_status(self, new_ch): > ??????? if new_ch == '1': > ??????????? self.led.value(1) > ??????????? self.led_status = 'led on' > ??????????? #print(self.led_status) > ??????? elif new_ch == '0': > ??????????? self.led.value(0) > ??????????? self.led_status = 'led off' > > ??? def get_led_status(self): > ??????? return self.led_status > ??????? #return "X" > > ??? def read_char(self): > ??????? poll_obj = uselect.poll() > ??????? poll_obj.register(sys.stdin,1) > > ??????? if poll_obj.poll(0): > ??????????? #read one character > ??????????? ch = sys.stdin.read(1) > ??????? else: > ??????????? ch = None > ??????? return ch > > ??? def send_acknowledgement(self): > ??????? print('ok') > > controller = LEDController() > > while True: > ??? new_ch = controller.read_char() > ??? print(new_ch) > > ??? if new_ch is not None: > ??????? controller.set_led_status(new_ch) > ??????? controller.send_acknowledgement() > > ??????? # After sending acknowledgement, get LED status > ??????? status_request = controller.read_char() > ??????? #utime.sleep(1) > ??????? #print(status_request) > > ??????? #status_request = 's' > ??????? if status_request == 's': > ??????????? led_status = controller.get_led_status() > ??????????? print(led_status) > ??????????? #print("Z") > > utime.sleep(.5) > > I just ran this project and I see the LED is not blinking, so I've > stuffed something up. I've got a medical appointment so I've go to scoot. > > I'm sorry to be such a pest. > No easy answer here but a suggestion based on the apparent fact that you have no way to generate debug output other than turning the LED on/off.... Try writing a program to send the 's' to the pico and make it flash the LED continuously. Then send another letter to turn it off again. See if you can get that to work. That way you will have a positive indication that sending a Python bytestring object to the pico can be done sucessfully. From the code above I don't see any proof that the pico successfully received and recognised the 's' bytestring. We know it can receive numbers but not bytes/strings. PS. It may be that you only need to convert the received char to a bytes object in the pico code? I also don't much like the fact that you do two reads inside the while True loop. It might be better to see if you can reduce that to a single read per iteration. Particularly since, if the 's' did wind up being read as new_char instead of status_request you'd have no way of knowing... That might require a dispatch table, something like(pseudo code): def led_on():... def led_off():... def send_status():... def read():... dispatch = {1: led_on, 0: led_off, 's': send_status } while True: ch = read() if ch in dispatch.keys(): dispatch[ch]() else: ... # what happens on error? -- 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 From phillor9 at gmail.com Tue Dec 12 16:20:20 2023 From: phillor9 at gmail.com (Phil) Date: Wed, 13 Dec 2023 07:20:20 +1000 Subject: [Tutor] Serial communication problem In-Reply-To: References: <1558cac6-3a53-4f66-a23e-ac9f42e87b18@gmail.com> <29fb94bf-610d-4cc3-ab08-fec527f635c6@gmail.com> Message-ID: <967d5522-f4e7-4307-92e5-690e26e25a78@gmail.com> On 13/12/23 04:46, Alan Gauld via Tutor wrote: Thank you again Alan, > I also don't much like the fact that you do two reads inside > the while True loop. This was the cause of the problem. The read function is non blocking which explains why I could only read the first byte before things went awry. The cure was to make the second read blocking and I felt like a real dill for not realising that sooner. I also got rid of the class structure which made the code far simpler. There was also a logic error in the python code and the length of that code has been greatly reduced as well. Thank you for your interest in my problems and your time is not wasted. I often pick up useful tips from the answers to other's questions. By the way, the object of this exercise was to gain experience in reading and writing to the pico via USB and then to build a dashboard to monitor a cyclone that has since crossed the coast a little north of my current location.? The dashboard also works correctly and the cyclone has veered away from it's initial predicted course. -- Regards, Phil From cs at cskk.id.au Tue Dec 12 22:58:54 2023 From: cs at cskk.id.au (Cameron Simpson) Date: Wed, 13 Dec 2023 14:58:54 +1100 Subject: [Tutor] Serial communication problem In-Reply-To: <967d5522-f4e7-4307-92e5-690e26e25a78@gmail.com> References: <967d5522-f4e7-4307-92e5-690e26e25a78@gmail.com> Message-ID: On 13Dec2023 07:20, Phil wrote: >On 13/12/23 04:46, Alan Gauld via Tutor wrote: >Thank you again Alan, >>I also don't much like the fact that you do two reads inside >>the while True loop. > >This was the cause of the problem. The read function is non blocking >which explains why I could only read the first byte before things went >awry. The cure was to make the second read blocking and I felt like a >real dill for not realising that sooner. Is there any reason for any of the reads to be nonblocking? Personally I find blocking reads easier to reason about. Cheers, Cameron Simpson From phillor9 at gmail.com Wed Dec 13 02:01:53 2023 From: phillor9 at gmail.com (Phil) Date: Wed, 13 Dec 2023 17:01:53 +1000 Subject: [Tutor] Serial communication problem In-Reply-To: References: <967d5522-f4e7-4307-92e5-690e26e25a78@gmail.com> Message-ID: <6d8456ad-f7d2-43d9-b1de-b643a2701408@gmail.com> On 13/12/23 13:58, Cameron Simpson wrote: > Is there any reason for any of the reads to be nonblocking? Personally > I find blocking reads easier to reason about. No real reason Cameron, and thank you for mentioning it, other than non-blocking reads have always worked, providing there's only one in an infinite loop. The dashboard code uses? what could be described as a semi-blocking read; it blocks for a preset time. This is useful because the same pico code can be used with it's own display and with or without a serial a connection. -- Regards, Phil From nikitaanihovskiy9 at yandex.by Wed Dec 13 13:35:53 2023 From: nikitaanihovskiy9 at yandex.by (=?utf-8?B?0J3QuNC60LjRgtCwINCQ0L3QuNGF0L7QstGB0LrQuNC5?=) Date: Wed, 13 Dec 2023 21:35:53 +0300 Subject: [Tutor] literature recommendations Message-ID: <759741702492054@mail.yandex.by> Good day. I recently started learning Python and became interested in becoming an artificial intelligence developer in this language. In this regard, I would like to ask you to recommend a list of literature that could help me with this (I am self-taught).** I would also be extremely grateful if someone could recommend a list of references for understanding the language in general, I want to understand it as well as possible. As good as it can be... Thank you very much in advance for your attention and answer! From threesomequarks at proton.me Wed Dec 13 19:57:44 2023 From: threesomequarks at proton.me (ThreeBlindQuarks) Date: Thu, 14 Dec 2023 00:57:44 +0000 Subject: [Tutor] literature recommendations In-Reply-To: <759741702492054@mail.yandex.by> References: <759741702492054@mail.yandex.by> Message-ID: Welcome, Nikita Anikhovsky, I can't supply a very simple reply other than that you need a solid basis in lots of more routine Python before learning various modules that may help with specific tasks. This is especially true if you are self-taught and have little experience in some computer language or other. AI has become quite a broad field so you need to think about what aspects might be useful. There are some general purpose modules such as numpy or pandas or scipy or scikit-learn that are heavily used directly or by other modules starting with tensorflow and layers above that at increasing levels of abstraction such as keras. If you need to visualize the data or your transformations, there are other modules and so on. But the specific ones depend on the area you are working on. If you search for something like "python modules used with AI" and other variations, there is plenty out there. There are lots of books you can find which include Python and AI or perhaps Machine Learning and so on. Lots of online and free info too including tutorials. Note, lots of AI work is being done in languages or frameworks other than Python, and if you get a job later, expect to have to relearn how to do things in whatever way it is being done then. Knowing python is barely a start. Some helpful background in areas like computer science, statistics and probability, data analysis and manipulation and more specific knowledge of things commonly used in AI including data structures like multi-dimensional matrices and quite a bit of math can be helpful. So you are correct, it is good to understand the language in general and especially the parts that are commonly used in the kind of AI applications you commonly encounter. I will mention that some of the modules form a partially complete world of their own with various layers hiding much of the underlying details so that you can set up a neural network and run it without hardly doing more than importing some modules and following a recipe of sorts. But for serious work, you may well have to use other basic parts of python or special modules to do things to set ip or transform your data into forms the functions ask for. Good Luck, - ????? Sent with Proton Mail secure email. On Wednesday, December 13th, 2023 at 1:35 PM, ?????? ?????????? wrote: > Good day. I recently started learning Python and became interested in > becoming an artificial intelligence developer in this language. In this > regard, I would like to ask you to recommend a list of literature that > could help me with this (I am self-taught).** > I would also be extremely grateful if someone could recommend a list of > references for understanding the language in general, I want to understand > it as well as possible. As good as it can be... > Thank you very much in advance for your attention and answer! > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From alan.gauld at yahoo.co.uk Thu Dec 14 13:07:28 2023 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 14 Dec 2023 18:07:28 +0000 Subject: [Tutor] literature recommendations In-Reply-To: <759741702492054@mail.yandex.by> References: <759741702492054@mail.yandex.by> Message-ID: On 13/12/2023 18:35, ?????? ?????????? wrote: > Good day. I recently started learning Python and became interested in > becoming an artificial intelligence developer in this language. There are probably libraries to help with that but to use them effectively you need to understand what AI is and which branch of it you wish to pursue/utilise. When I was at university 40 years ago there were 5 spheres of AI: Vision(recognition) Speech Recognition and Synthesis Knowledge Based Systems Robotics Textual analysis/generation I'm sure this will have changed and new areas added or areas merged but it gives some clue about the range of activities coming under the AI umbrella. You may need to study some generic AI texts to understand the what/how and where each of these areas is at. Then decide which ones you want to use and research available libraries in those topics. Its a lot of work. > references for understanding the language in general, I want to understand > it as well as possible. As good as it can be... This is easier and there are many Python text books and video courses. Which ones suit your personal learning preferences depends a lot on you. Personally I favour the O'Reilly series and "Programming in Python 3" by Sommerville. I should probably put in a plug for my own web site(see below) and the related print book "Python Projects" -- 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 From mattathome08823 at yahoo.com Mon Dec 18 14:18:34 2023 From: mattathome08823 at yahoo.com (Matt Athome) Date: Mon, 18 Dec 2023 19:18:34 +0000 (UTC) Subject: [Tutor] Try Exclude and operators References: <1964113273.2345271.1702927114706.ref@mail.yahoo.com> Message-ID: <1964113273.2345271.1702927114706@mail.yahoo.com> Python version Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct? 2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] on win32 Purpose Give test score for input between 0.0 to 1.0 Use the try and exclude to throw up a message for invalid user input. code gives the following error. ?score >= 0 and <= 1.0 ????????????????? ? ? ? ^^ SyntaxError: invalid syntax # Score between 0.0 and 1.0 score = float(input("Enter Score: ")) try: ??? score >= 0 and <= 1.0 except: ??? print("Enter a score between 0.0 and 1.0") ??? quit() if score >= 0.9: ??? print("A") elif score >= 0.8: ??? print("B") elif score >= 0.7: ??? print("C") elif score >= 0.6: ??? print("D") elif score < 0.6: ??? print("F") From bouncingcats at gmail.com Mon Dec 18 17:58:29 2023 From: bouncingcats at gmail.com (David) Date: Mon, 18 Dec 2023 22:58:29 +0000 Subject: [Tutor] Try Exclude and operators In-Reply-To: <1964113273.2345271.1702927114706@mail.yahoo.com> References: <1964113273.2345271.1702927114706.ref@mail.yahoo.com> <1964113273.2345271.1702927114706@mail.yahoo.com> Message-ID: On Mon, 18 Dec 2023 at 22:48, Matt Athome via Tutor wrote: > Python version > Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct 2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] on win32 > > Purpose > Give test score for input between 0.0 to 1.0 > Use the try and exclude to throw up a message for invalid user input. > > code gives the following error. > score >= 0 and <= 1.0 > ^^ > SyntaxError: invalid syntax Hi Matt, You see a SyntaxError because your syntax is invalid Python. I hope the below example demonstrates valid syntax that does what you want ... $ python3 Python 3.9.2 (default, Feb 28 2021, 17:03:44) [GCC 10.2.1 20210110] on linux Type "help", "copyright", "credits" or "license" for more information. >>> score = 42 >>> score >= 0 and score <= 1.0 False >>> 0 <= score <= 1.0 False >>> From mats at wichmann.us Mon Dec 18 18:11:57 2023 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 18 Dec 2023 16:11:57 -0700 Subject: [Tutor] Try Exclude and operators In-Reply-To: <1964113273.2345271.1702927114706@mail.yahoo.com> References: <1964113273.2345271.1702927114706.ref@mail.yahoo.com> <1964113273.2345271.1702927114706@mail.yahoo.com> Message-ID: <5e48e966-739d-4936-9d4b-be095af80d58@wichmann.us> On 12/18/23 12:18, Matt Athome via Tutor wrote: > Python version > Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct? 2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] on win32 > > Purpose > Give test score for input between 0.0 to 1.0 > Use the try and exclude to throw up a message for invalid user input. > > code gives the following error. > ?score >= 0 and <= 1.0 > ????????????????? ? ? ? ^^ > SyntaxError: invalid syntax > > # Score between 0.0 and 1.0 > score = float(input("Enter Score: ")) > try: > ??? score >= 0 and <= 1.0 > except: > ??? print("Enter a score between 0.0 and 1.0") Also consider other kinds of invalid input: >>> score = float(input("Enter Score: ")) Enter Score: A+ Traceback (most recent call last): File "", line 1, in score = float(input("Enter Score: ")) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ValueError: could not convert string to float: 'A+' >>> From alan.gauld at yahoo.co.uk Mon Dec 18 18:18:36 2023 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 18 Dec 2023 23:18:36 +0000 Subject: [Tutor] Try Exclude and operators In-Reply-To: <1964113273.2345271.1702927114706@mail.yahoo.com> References: <1964113273.2345271.1702927114706.ref@mail.yahoo.com> <1964113273.2345271.1702927114706@mail.yahoo.com> Message-ID: On 18/12/2023 19:18, Matt Athome via Tutor wrote: > Purpose > Give test score for input between 0.0 to 1.0 > Use the try and exclude to throw up a message for invalid user input. I assume this should read try/except not exclude? > code gives the following error. > ?score >= 0 and <= 1.0 > ????????????????? ? ? ? ^^ > SyntaxError: invalid syntax As the error says that is not valid Python. The normal way to write the test in a programming language would be: score >= 0.0 and score <= 1.0 But python has a special syntax for these kinds of comparisons: 0.0 <= score <= 1.0 In both cases the line will evaluate to a boolean True or False. Neither of these will raise an error for the except to catch. Normally it would be used inside an if or while statement. if not 0.0 <= score <= 1.0: raise ValueError("Score must be between 0 and 1) And now you can wrap that in a try/except to catch any errors: try: score = float(....) if not 0.0 <= score <= 1.0: raise ValueError("Score must be between 0 and 1) except ValueError: print(value error message) > # Score between 0.0 and 1.0 > score = float(input("Enter Score: ")) > try: > ??? score >= 0 and <= 1.0 > except: > ??? print("Enter a score between 0.0 and 1.0") > ??? quit() That would be a very confusing message since the program quits. It might be better to report that the given score was not in the correct range rather than saying Enter another score. Alternatively put the input statement into a loop: while True: score = float(...) if 0.0 <= score <= 1.0: break. # score is good print(error message here....) But that doesn't use try/except! -- 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 From daroldhayes at gmail.com Wed Dec 20 14:54:24 2023 From: daroldhayes at gmail.com (Darold Hayes) Date: Wed, 20 Dec 2023 14:54:24 -0500 Subject: [Tutor] Assignment Assistance Message-ID: Afternoon, I am a cybersecurity fellow struggling with an assignment I have to make adjustments to code that has been written. Hoping to connect with someone no more than hour to support. Please feel free to email me back. I hope I have the right place for this sort of assistance. Best regards, Darold From mats at wichmann.us Thu Dec 21 12:59:21 2023 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 21 Dec 2023 10:59:21 -0700 Subject: [Tutor] Assignment Assistance In-Reply-To: References: Message-ID: On 12/20/23 12:54, Darold Hayes wrote: > Afternoon, > I am a cybersecurity fellow struggling with an assignment I have to make > adjustments to code that has been written. Hoping to connect with someone > no more than hour to support. Please feel free to email me back. I hope I > have the right place for this sort of assistance. The way this particular list operates is you ask questions (with detail!), and folks try to guide you. If it's an assignment, we don't want to give you "the answer", but help you learn. There aren't private tutors here, unless someone responds to you off-list with an offer. From alan.gauld at yahoo.co.uk Thu Dec 21 13:30:30 2023 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 21 Dec 2023 18:30:30 +0000 Subject: [Tutor] Assignment Assistance In-Reply-To: References: Message-ID: On 21/12/2023 17:59, Mats Wichmann wrote: >> I am a cybersecurity fellow struggling with an assignment > detail!), and folks try to guide you. If it's an assignment, we don't > want to give you "the answer", but help you learn. It will help if you make it clear if its a professional assignment - ie. a work project - or an educational one. We provide more direct help for work tasks -- 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 From ethanrosenberg414 at gmail.com Fri Dec 22 06:49:22 2023 From: ethanrosenberg414 at gmail.com (Ethan Rosenberg) Date: Fri, 22 Dec 2023 06:49:22 -0500 Subject: [Tutor] Program xy.py Message-ID: Thank you for the excellent help you give to python programmers. This program should put i*j in a 2x2 matrix. It does not. #xy.py #Program to put i*j [indexes] in a 2x2 matrix mat=[][] for i =0: for j in range(0,1): mat[0][j] = i*j for j=0: for i in range(0,1): mat[i][0] = i*j print(mat) Thank you in advance. Ethan Rosenberg From alan.gauld at yahoo.co.uk Fri Dec 22 14:16:02 2023 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 22 Dec 2023 19:16:02 +0000 Subject: [Tutor] Program xy.py In-Reply-To: References: Message-ID: On 22/12/2023 11:49, Ethan Rosenberg wrote: > This program should put i*j in a 2x2 matrix. It does not. Its always good to tell us what it does do including showing the output. If the output is an error message (which I assume is the case here!) its even more important to paste the full error message into the mail. It saves us guessing! > mat=[][] > for i =0: That is not legal python, it should throw an error. I'm not sure what you are even trying to do? If it's just to set i to zero you don't need the for, just use: i = 0 And don't indent the next line > for j in range(0,1): > mat[0][j] = i*j But if i is zero you don't need the math since zero times anything is always zero. mat[0] = [0,0] > for j=0: > for i in range(0,1): > mat[i][0] = i*j And the same here. Except this only sets the first element so you get the equivalent of mat = [0,0][0] I think you actually want something like for i in (0,1): for j in (0,1): may[i][j] = i*j Which should give you mat = [0,0][0,1] Is that what you were looking for? -- 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 From mats at wichmann.us Fri Dec 22 14:21:42 2023 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 22 Dec 2023 12:21:42 -0700 Subject: [Tutor] Program xy.py In-Reply-To: References: Message-ID: <786ac723-8d21-449f-91e6-6440eb46bbd1@wichmann.us> On 12/22/23 04:49, Ethan Rosenberg wrote: > Thank you for the excellent help you give to python programmers. > > This program should put i*j in a 2x2 matrix. > for j in range(0,1): From the Python interpreter itself: >>> help(range) Help on class range in module builtins: class range(object) | range(stop) -> range object | range(start, stop[, step]) -> range object | | Return an object that produces a sequence of integers from start (inclusive) | to stop (exclusive) by step. Notice it doesn't include the stop value, so if you think you're getting an iterator that produces two values from range(0, 1) - you won't. You'd need range(0, 2) for that. From ethanrosenberg414 at gmail.com Sun Dec 24 15:41:13 2023 From: ethanrosenberg414 at gmail.com (Ethan Rosenberg) Date: Sun, 24 Dec 2023 15:41:13 -0500 Subject: [Tutor] Program matx.py Message-ID: Help Desk - Thank you for the excellent help you give to Python programmers. Do I have to be registered to submit a question? This program is supposed to insert i*j into the cells of a 2x2 matrix. I receive a syntax error. #matx.py #program to add i*j to cells of 2x2 matrix mat = [][] for i in range(0,1): for j in range(0,1): mat[i][j] = i*j print(mat) Thank you in advance. From lggeorgieva at gmail.com Sun Dec 24 13:22:55 2023 From: lggeorgieva at gmail.com (Lilia Georgieva) Date: Sun, 24 Dec 2023 18:22:55 +0000 Subject: [Tutor] System architecture Message-ID: Hi, What are good resources for system architecture? I struggle with more complex designs and would like to learn more about how to approach the design. My case study is big data processing but I am really asking about pointers to the generic approach. Thank you! Lilia From threesomequarks at proton.me Mon Dec 25 14:42:43 2023 From: threesomequarks at proton.me (ThreeBlindQuarks) Date: Mon, 25 Dec 2023 19:42:43 +0000 Subject: [Tutor] System architecture In-Reply-To: References: Message-ID: ?????, That is a fairly broad question and many of the concepts transcend the particular programming language to be used. We can assume you intend to use python for much of whatever the project is and indeed there are considerations that work well in python to modularize your code so you can plan out at various levels. I assume someone else may point you at various books and resources or maybe you can search and find things like this: https://medium.com/@xstrengthofonex/software-architecture-in-python-cda7a873cc47 Too much depends on your project and what you are looking for and how much time and effort you want to do. Many projects have a slew of planning and documents along the lines of architecture and systems engineering but before you start, you need to examine some of the rules that may apply in your case. For example, can you use public domain software in your project or perhaps pay to license the use of decent quality parts or development environments. And are you looking to use python with a focus on objects or functions or other things it does well, is the program interactive, how important is speed, do parts run asynchronously and so on. I have seen projects effectively killed because the designer was not allowed to use software that did not address various security concerns or ended up requiring lots of payments or was violating some rules of use in a product by using open software. Some here suggest this group does better at being asked focused questions along with some indications such as error messages. You seem at a much earlier stage. Nonetheless, as Alan might say, it is a topic that may be of interest to many. Larger projects tend to get complex enough that some planning in advance can be very helpful and keep you from constantly changing the requirements as you go along and run into snags. Sent with Proton Mail secure email. On Sunday, December 24th, 2023 at 1:22 PM, Lilia Georgieva wrote: > Hi, > > What are good resources for system architecture? I struggle with more > complex designs and would like to learn more about how to approach the > design. > > My case study is big data processing but I am really asking about pointers > to the generic approach. > > Thank you! > > Lilia > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From alan.gauld at yahoo.co.uk Mon Dec 25 14:49:47 2023 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 25 Dec 2023 19:49:47 +0000 Subject: [Tutor] Program matx.py In-Reply-To: References: Message-ID: On 24/12/2023 20:41, Ethan Rosenberg wrote: > Help Desk - > > Thank you for the excellent help you give to Python programmers. > > Do I have to be registered to submit a question? > > This program is supposed to insert i*j into the cells of a 2x2 matrix. I > receive a syntax error. > > #matx.py > #program to add i*j to cells of 2x2 matrix > > mat = [][] This is the syntax error. You need to nest the lists. mat = [[]] ie create a list of lists. But... > for i in range(0,1): this will only yield a value for i of 0 since range(0,1) is just 0. Better would be to say: for i in range(2): > for j in range(0,1): Same here > mat[i][j] = i*j This will fail because the second list is empty so [j] doesn't exist. You need to create a list of initialised values. Lets say None: mat = [[None,None][None,None]] Or more generically: mat = [[None]*2 for size in range(2)] Now your loops should work. -- 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 From PythonList at DancesWithMice.info Mon Dec 25 14:52:33 2023 From: PythonList at DancesWithMice.info (DL Neil) Date: Tue, 26 Dec 2023 08:52:33 +1300 Subject: [Tutor] Program matx.py In-Reply-To: References: Message-ID: On 12/25/23 09:41, Ethan Rosenberg wrote: > Help Desk - > > Thank you for the excellent help you give to Python programmers. > > Do I have to be registered to submit a question? You have already subscribed to the list, ie registered! > This program is supposed to insert i*j into the cells of a 2x2 matrix. I > receive a syntax error. When writing to the list, please copy-paste both code and the (full) 'stack trace' of any error messages. That way no-one has to work so hard to figure-out where the error occurred. > #matx.py > #program to add i*j to cells of 2x2 matrix > > mat = [][] In this case, it was easy-enough to see this is the error. > for i in range(0,1): > for j in range(0,1): > mat[i][j] = i*j > print(mat) In Python (as distinct from add-on math/stats libraries such as NumPy) the basic collection data-structure is a list. A list is one-dimensional, ie it has length (revealed by the len() function) but no 'height'. However, a list can be composed of scalar-elements and/or of collections - there is no requirement that every element be of the same type (as there may be in other languages). Accordingly, a two-dimensional approach is to build a list of lists. This means that the inner for-loop should build a single row (or column - depending how you intend to visualise the problem - think of a larger and non-square matrix). Whereas the outer for-loop will build a collection of those inner-constructs (the matrix). Recommendation: eschew short-forms, eg matx, i, j, mat; and use complete words to reduce cognitive-load (a decent IDE will save typing!). The above shows how to solve the problem/implement the solution in Python. If that's your challenge, stop reading now! To check the above solution, try: https://www.pythonpool.com/python-2d-list/ -- Regards =dn From mats at wichmann.us Mon Dec 25 16:34:11 2023 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 25 Dec 2023 14:34:11 -0700 Subject: [Tutor] System architecture In-Reply-To: References: Message-ID: <7dd0035f-b3fa-410c-8bd7-d50bd9053b39@wichmann.us> On 12/25/23 12:42, ThreeBlindQuarks via Tutor wrote: > > ?????, > > That is a fairly broad question and many of the concepts transcend the particular programming language to be used. I'd certainly look at the Clean Architecture concepts (that's one of the links in the article pointed to)... just as a way of looking at how you partition a large system in a way that it remains flexible and maintainable - that's a lot of what "architecture" is about these days, leaving you with flexibility to make changes, even very large changes like forklifting out an entire section, without destabilizing your application. Brandon normally expresses himself very well, but this is slide-ware from a talk, so as a standalone read it's not perfect. From alan.gauld at yahoo.co.uk Tue Dec 26 05:09:25 2023 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 26 Dec 2023 10:09:25 +0000 Subject: [Tutor] System architecture In-Reply-To: References: Message-ID: On 24/12/2023 18:22, Lilia Georgieva wrote: > What are good resources for system architecture? I struggle with more > complex designs and would like to learn more about how to approach the > design. That is a huge topic and even the term "system architecture" is multi-layered and means different things in different contexts. For example a programmer may think it means the structure of a program in terms of modules, classes, functions and data. A Operations person will think in terms of the physical deployment onto clients, servers and virtualised machines, databases, external sources, firewalls, networks etc... But an Enterprise Architect, at the other end of the scale, thinks of it as a collection of systems cooperating to achieve some enterprise goal or function (eg billing or payroll or manufacturing) > My case study is big data processing but I am really asking about pointers > to the generic approach. Narrowing the domain certainly helps to identify approaches. And common big data architectures and frameworks exist - eg Hadoop. There are many books and web sites dedicated to systems architecture and several methodologies and notations to support it. (eg. UML, SSADM, SDL, BPAL etc.) There are even formal mathematical approaches that are usually reserved for mission or safety critical subsets of the system (eg Z, OCL or VDM). And there are at least a couple of formal qualifications that you can study for - TOGAF is the one I know best but Zachman and Microsoft also do schemes. Coming back to the typical users of this mailing list the best bet is most likely one of the many books on software design/architecture. I'll highlight a few that I personally like but I'm sure there are others out there (and my preference is for OO design so that shows in the list!) Grady Booch - Object Oriented Design with Applications Jackson - System Development Fowler - UML Distilled Malveau/Mowbry - Software Architecture Bootcamp Peter Coad - Object Models; Strategies, Patterns and Applications Cook - Building Enterprise Information Architectures And for general priciples of software construction, including architectural things: Bentley - Programming Pearls Hunt/Thomas - The Pragmatic Programmer McConnell - Code Complete There are dedicated books on Big Data but since the only one I've read is the Dummies Guide(don't laugh!) I can't really comment. Others will doubtless have their own favourites. And of course there are whole web sites dedicated to the topic. The Open Group has several sections of their site geared to architecture and design. ZIFA coveres Zachman IBM also has a good section on their site. -- 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 From leamhall at gmail.com Tue Dec 26 07:44:36 2023 From: leamhall at gmail.com (Leam Hall) Date: Tue, 26 Dec 2023 06:44:36 -0600 Subject: [Tutor] System architecture In-Reply-To: References: Message-ID: On 12/26/23 04:09, Alan Gauld via Tutor wrote: > On 24/12/2023 18:22, Lilia Georgieva wrote: > >> What are good resources for system architecture? I struggle with more >> complex designs and would like to learn more about how to approach the >> design. > Grady Booch - Object Oriented Design with Applications > Jackson - System Development > Fowler - UML Distilled > Malveau/Mowbry - Software Architecture Bootcamp > Peter Coad - Object Models; Strategies, Patterns and Applications > Cook - Building Enterprise Information Architectures Percival, Gregory - Architecture Patterns with Python Leam -- Software Engineer (reuel.net/resume) Scribe: The Domici War (domiciwar.net) General Ne'er-do-well (github.com/LeamHall) From ethanrosenberg414 at gmail.com Tue Dec 26 15:27:03 2023 From: ethanrosenberg414 at gmail.com (Ethan Rosenberg) Date: Tue, 26 Dec 2023 15:27:03 -0500 Subject: [Tutor] 2x2 matrix Message-ID: Tutor - Thank you for the wonderful help you provide to Python programmers. I want to put i*j in the cells of a 2x2 matrix and have had no luck. Help please. Ethan Rosenberg From alan.gauld at yahoo.co.uk Tue Dec 26 20:36:42 2023 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 27 Dec 2023 01:36:42 +0000 Subject: [Tutor] 2x2 matrix In-Reply-To: References: Message-ID: On 26/12/2023 20:27, Ethan Rosenberg wrote: > I want to put i*j in the cells of a 2x2 matrix and have had no luck. > Help please. You've asked this a few times now and been given several solutions. What are you trying currently? What error messages are you getting? If no errors what output are you getting? I could just post a one line solution, but you'd learn nothing from that. We are here to help you learn not to provide you with solutions. Tell us what is puzzling you or you find difficult and we are happy to help. -- 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 From threesomequarks at proton.me Tue Dec 26 22:10:14 2023 From: threesomequarks at proton.me (ThreeBlindQuarks) Date: Wed, 27 Dec 2023 03:10:14 +0000 Subject: [Tutor] 2x2 matrix In-Reply-To: References: Message-ID: Alan, I thought an earlier questions asked not about a "matrix" but a two by two list of lists. The answers can be substantially similar, albeit you may have to use a module like numpy that supports a matrix. The question keeps showing up in several places and, as you note, the answers do not appear to be evaluated so maybe the right answer is to not. Or, throw a comprehension at them instead of an explicit loop. But this keeps smelling like homework and not part of a larger purpose. Sent with Proton Mail secure email. On Tuesday, December 26th, 2023 at 8:36 PM, Alan Gauld via Tutor wrote: > On 26/12/2023 20:27, Ethan Rosenberg wrote: > > > I want to put i*j in the cells of a 2x2 matrix and have had no luck. > > Help please. > > > You've asked this a few times now and been given > several solutions. > > What are you trying currently? > What error messages are you getting? > If no errors what output are you getting? > > I could just post a one line solution, but you'd > learn nothing from that. We are here to help you > learn not to provide you with solutions. > > Tell us what is puzzling you or you find difficult > and we are happy to help. > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From threesomequarks at proton.me Wed Dec 27 00:24:53 2023 From: threesomequarks at proton.me (ThreeBlindQuarks) Date: Wed, 27 Dec 2023 05:24:53 +0000 Subject: [Tutor] 2x2 matrix In-Reply-To: References: Message-ID: I decided to try another tack but want to make clear we have to know if the OP wants to make a list of lists or make some form of mathematical object that acts like a matrix so you can do things like multiply with it. WARNING: This solution depends on things the OP may not yet be using such as comprehensions or use of the range() function and of course numpy. And it is way more general than requested but that can be a good thing. So here is the plan. Make it in steps. The following code is not a one-liner, albeit it could be. # if you want a real matrix at the end, import numpy. import numpy as np # Number of rows/columns in multiplication table # reset this to 2 or whatever size you want n = 5 # generate a list containing 1:n sides = list(range(1,n+1)) # Make a flat list with all multiplications one row at a time. flat = [i*j for i in sides for j in sides] # Now take every n items in list (a row) and make a list of such lists. nested = [flat[i : i+n] for i in range(0, len(flat), n)] # If you want a real matrix, use numpy. may_tricks = np.array(nested) # END CODE So I chose n=5 to make a point that this will work for anything that is 1 or above. Here is the output of each step if n==5: >>> n 5 >>> sides [1, 2, 3, 4, 5] >>> flat [1, 2, 3, 4, 5, 2, 4, 6, 8, 10, 3, 6, 9, 12, 15, 4, 8, 12, 16, 20, 5, 10, 15, 20, 25] >>> nested [[1, 2, 3, 4, 5], [2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20], [5, 10, 15, 20, 25]] >>> may_tricks array([[ 1, 2, 3, 4, 5], [ 2, 4, 6, 8, 10], [ 3, 6, 9, 12, 15], [ 4, 8, 12, 16, 20], [ 5, 10, 15, 20, 25]]) For n=2 it is simpler: >>> n 2 >>> sides [1, 2] >>> flat [1, 2, 2, 4] >>> nested [[1, 2], [2, 4]] >>> may_tricks array([[1, 2], [2, 4]]) Note you can add, multiply and so on using the matrix or array format: >>> may_tricks + may_tricks array([[2, 4], [4, 8]]) >>> may_tricks * may_tricks array([[ 1, 4], [ 4, 16]]) And, if this is something you want to do regularly (no idea why) then a function would make sense like this: def times_table(n): flat = [i*j for i in list(range(1,n+1)) for j in list(range(1,n+1))] return(np.array([flat[i : i+n] for i in range(0, len(flat), n)])) Below I show it for n being invoked as 2 or 12: >>> times_table(2) array([[1, 2], [2, 4]]) >>> times_table(12) array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24], [ 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36], [ 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48], [ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60], [ 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72], [ 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84], [ 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96], [ 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108], [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120], [ 11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121, 132], [ 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144]]) DISCLAIMER, some other languages make something like this easier and perhaps some python way is along the same lines. For example, in R a matrix is really a vector with added info and rules that provide dimensions so you can do something like ask for a matrix and give it the linear vector (or list) from the above and tell it you want it to be 12 by 12 or 9 by 16 and it does it for you. Sent with Proton Mail secure email. On Tuesday, December 26th, 2023 at 10:10 PM, ThreeBlindQuarks wrote: > Alan, > > I thought an earlier questions asked not about a "matrix" but a two by two list of lists. > > The answers can be substantially similar, albeit you may have to use a module like numpy that supports a matrix. > > The question keeps showing up in several places and, as you note, the answers do not appear to be evaluated so maybe the right answer is to not. > > Or, throw a comprehension at them instead of an explicit loop. > > But this keeps smelling like homework and not part of a larger purpose. > > > > > Sent with Proton Mail secure email. > > > On Tuesday, December 26th, 2023 at 8:36 PM, Alan Gauld via Tutor tutor at python.org wrote: > > > > > On 26/12/2023 20:27, Ethan Rosenberg wrote: > > > > > I want to put i*j in the cells of a 2x2 matrix and have had no luck. > > > Help please. > > > > You've asked this a few times now and been given > > several solutions. > > > > What are you trying currently? > > What error messages are you getting? > > If no errors what output are you getting? > > > > I could just post a one line solution, but you'd > > learn nothing from that. We are here to help you > > learn not to provide you with solutions. > > > > Tell us what is puzzling you or you find difficult > > and we are happy to help. > > > > -- > > 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 at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor From ethanrosenberg414 at gmail.com Wed Dec 27 17:34:30 2023 From: ethanrosenberg414 at gmail.com (Ethan Rosenberg) Date: Wed, 27 Dec 2023 17:34:30 -0500 Subject: [Tutor] matx3.py Message-ID: Tutor - Thank you for the wonderful help you provide to Python programmers. Here is the program #matx3.py #Put i*j in cells of 3x3 matrix Row = 1 Column =1 mat = [[1],[2],[3]],[[4],[5],[6]],[[7],[8],[9]] #print(mat) for i in range(0,3): print(mat[i]) for i in range(0,2): for j in range(0,2): for k in range(0,8): mat[k] = i*j #print(mat) for i in range(0,3): print(mat[i]) I receive an error that mat[k] = i*j is a tuple?? Thanks in advance for your help. Ethan Rosenberg From PythonList at DancesWithMice.info Wed Dec 27 17:45:17 2023 From: PythonList at DancesWithMice.info (dn) Date: Thu, 28 Dec 2023 11:45:17 +1300 Subject: [Tutor] matx3.py In-Reply-To: References: Message-ID: On 28/12/23 11:34, Ethan Rosenberg wrote: > Tutor - > > Thank you for the wonderful help you provide to Python programmers. > > Here is the program > > #matx3.py > #Put i*j in cells of 3x3 matrix > > Row = 1 > Column =1 > mat = [[1],[2],[3]],[[4],[5],[6]],[[7],[8],[9]] > #print(mat) > for i in range(0,3): > print(mat[i]) > for i in range(0,2): > for j in range(0,2): > for k in range(0,8): > mat[k] = i*j > #print(mat) > for i in range(0,3): > print(mat[i]) > > I receive an error that mat[k] = i*j is a tuple?? > > Thanks in advance for your help. Please copy-paste the full set of error-messages rather than your summary/interpretation. mat is indeed first defined as a tuple. What did you intend it to be? -- Regards, =dn From mats at wichmann.us Wed Dec 27 23:01:42 2023 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 27 Dec 2023 21:01:42 -0700 Subject: [Tutor] matx3.py In-Reply-To: References: Message-ID: <1d5a2043-b3f7-4c38-8df5-eaf1a064c4ff@wichmann.us> On 12/27/23 15:34, Ethan Rosenberg wrote: > Tutor - > mat = [[1],[2],[3]],[[4],[5],[6]],[[7],[8],[9]] > I receive an error that mat[k] = i*j is a tuple?? believe it or not your initialization of mat sets it to a tuple - a tuple consisting of three lists, each consisting of three lists containing a single element. The interactive interpreter will try to show it as a tuple by adding parens, but it's not the parens that make it a tuple: comma-separated elements that aren't otherwise enclosed in known container syntax (e.g. list, set, dict) makes it a tuple. >>> mat = [[1],[2],[3]],[[4],[5],[6]],[[7],[8],[9]] >>> type(mat) >>> mat ([[1], [2], [3]], [[4], [5], [6]], [[7], [8], [9]]) >>> It's doubtful this is what you want. Nor your later code where you nest loops three deep and will end up repeating stuff you don't expect. A print statement here will show you what's happening: .... for k in range(0,8): print(f"Setting mat[{k}] to {i * j}") mat[k] = i*j Giving: Setting mat[0] to 0 Setting mat[1] to 0 Setting mat[2] to 0 Setting mat[3] to 0 Setting mat[4] to 0 Setting mat[5] to 0 Setting mat[6] to 0 Setting mat[7] to 0 Setting mat[0] to 0 Setting mat[1] to 0 Setting mat[2] to 0 Setting mat[3] to 0 Setting mat[4] to 0 Setting mat[5] to 0 Setting mat[6] to 0 Setting mat[7] to 0 Setting mat[0] to 0 Setting mat[1] to 0 Setting mat[2] to 0 Setting mat[3] to 0 Setting mat[4] to 0 Setting mat[5] to 0 Setting mat[6] to 0 Setting mat[7] to 0 Setting mat[0] to 1 Setting mat[1] to 1 Setting mat[2] to 1 Setting mat[3] to 1 Setting mat[4] to 1 Setting mat[5] to 1 Setting mat[6] to 1 Setting mat[7] to 1 Notice the repeat setting of mat[0] through mat[7] (and no mat[8]), to the wrong values, and think for a while until you understand why it's working this way. As I expect people have pointed out to you (sorry haven't followed the whole thread), Python itself has no concept of a two-dimensional matrix. If you want that, use the (external, and very popular) numpy module. You can fake it with a list (single-dimensional array), as you're trying to do. If I've twigged what you want, something like this can set it up, trying to avoid zero values - again, it's kind of a guess that this is what you're after: mat = [] for i in range(1, 4): for j in range(1, 4): mat.append(i * j) You can write that whole thing as a "list comprehension", which is a bit more advanced: mat = [i * j for i in range(1, 4) for j in range(1, 4)] And to print it out so it "looks" like a matrix you can get a little tricky: for idx, elem in enumerate(mat, start=1): print(elem, end=" " if idx % 3 else "\n") That causes each third element to emit a line break after it's printed, the other two will emit a space. The print function syntax shown is slightly advanced, don't worry if you haven't seen that yet, you can come back to that later. From threesomequarks at proton.me Wed Dec 27 23:21:31 2023 From: threesomequarks at proton.me (ThreeBlindQuarks) Date: Thu, 28 Dec 2023 04:21:31 +0000 Subject: [Tutor] matx3.py In-Reply-To: References: Message-ID: That is interesting code but it is far from clear if it does what we thought you wanted. First, you create a tuple containing a list of lists. >>> mat = [[1],[2],[3]],[[4],[5],[6]],[[7],[8],[9]] >>> mat ([[1], [2], [3]], [[4], [5], [6]], [[7], [8], [9]]) >>> type(mat) My guess is you did not mean to provide two lists (with contents) with a comma between them. The interpreter saw mat = stuff, stuff What you wanted was probably this: >>> mat = [[[1],[2],[3]],[[4],[5],[6]],[[7],[8],[9]]] >>> type(mat) >>> len(mat) 3 >>> mat[1] [[4], [5], [6]] >>> mat[0] [[1], [2], [3]] >>> mat[2] [[7], [8], [9]] The extra external brackets make this a list. So the rest of the code will not work well until you fix the data structure. The purpose of doing this SEEMS to be to initialize the data structure to be used later and the entries chosen are meant to be over-written. If properly done, this would be a reasonable strategy but reading on, maybe not. This code may not do what you want. for i in range(0,2): for j in range(0,2): for k in range(0,8): mat[k] = i*j You are going out of range because of the way you are assigning. mat has THREE elements. Each of those has three inner elements. But you are trying to write to the fourth and more positions in mat and they simply do not exist. Here is how you address inner parts and I need to mention you did something unexpected as the deepest entries in mat are not atomic but lists with one entry. >>> mat[0] [[1], [2], [3]] >>> mat[0][0] [1] >>> mat[0][1] [2] >>> mat[0][2] [3] >>> mat[2][2] [9] To get an actual entry you need a third level as in: >>> mat[2][2][0] 9 What you asked in the loop thus makes no real sense as you want to write to a top-level item which would simply replace much of your data structure with a smaller one. You have been offered multiple working ideas and ignored them while displaying examples that suggest you are missing some basics. And, I think your approach is flawed even if done right. There is no need for a k if you do it right. Have fun. Sent with Proton Mail secure email. On Wednesday, December 27th, 2023 at 5:34 PM, Ethan Rosenberg wrote: > Tutor - > > Thank you for the wonderful help you provide to Python programmers. > > Here is the program > > #matx3.py > #Put ij in cells of 3x3 matrix > > Row = 1 > Column =1 > mat = [[1],[2],[3]],[[4],[5],[6]],[[7],[8],[9]] > #print(mat) > for i in range(0,3): > print(mat[i]) > for i in range(0,2): > for j in range(0,2): > for k in range(0,8): > mat[k] = ij > #print(mat) > for i in range(0,3): > print(mat[i]) > > I receive an error that mat[k] = i*j is a tuple?? > > Thanks in advance for your help. > > Ethan Rosenberg > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From asad.hasan2004 at gmail.com Thu Dec 28 02:00:05 2023 From: asad.hasan2004 at gmail.com (Asad) Date: Thu, 28 Dec 2023 12:30:05 +0530 Subject: [Tutor] HTTP Request to simulate the interactions with the web application Message-ID: Hi All , I am planning to automate a daily task on a App . The objective is build a python script to Configure the HTTP Request to simulate the interactions with the web application. This might involve sending POST requests, handling cookies, etc I am planning to use this python script with Selenium . I need some guidance or links which can make me understand how to write apython script to simulate the interactions with the web application like login and authentication to https://hudle.in and use then use the booking using SSO or OTP . If there is any script already available please share . -- Asad Hasan +91 9582111698 From mats at wichmann.us Thu Dec 28 14:42:06 2023 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 28 Dec 2023 12:42:06 -0700 Subject: [Tutor] HTTP Request to simulate the interactions with the web application In-Reply-To: References: Message-ID: On 12/28/23 00:00, Asad wrote: > Hi All , > > I am planning to automate a daily task on a App . The objective is > build a python script to Configure the HTTP Request to simulate the > interactions with the web application. This might involve sending POST > requests, handling cookies, etc I am planning to use this python script > with Selenium . I need some guidance or links which can make me understand > how to write a python script to simulate the interactions with the web > application like login and authentication to https://hudle.in and use then > use the booking using SSO or OTP . If there is any script already available > please share . > It's hard to say - there are so many options here. If auth specifically is the target that's a very well defined workflow, and there are some choices. I read this article a while back when I thought i needed that, then it almost immediately ended up being Someone Else's Problem and I dropped it. See if this gives you any ideas for your case: https://dev.to/filipemir/mocking-auth0-tokens-in-python-and-beyond-5dki but if the target is to simulate the physical interaction with the app - screen or mouse events like taps, drags, clicks etc. then you really do need Selenium (not something I've ever done anything with, so no pointers from me :-( ) From alan.gauld at yahoo.co.uk Thu Dec 28 17:22:54 2023 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 28 Dec 2023 22:22:54 +0000 Subject: [Tutor] HTTP Request to simulate the interactions with the web application In-Reply-To: References: Message-ID: On 28/12/2023 07:00, Asad wrote: > I am planning to automate a daily task on a App . The objective is > build a python script to Configure the HTTP Request to simulate the > interactions with the web application. I can't offer much here because its so long since I did this kind of thing that my knowledge is mostly no longer relevant. However, I would point out that it's worth checking that the site doesn't offer an API because those that do often actively try to stop screen scraping - for example they randomly add invisible elements to the page, change the names of components, move them slightly on screen etc. This is to prevent the server being overloaded by robotic actions. Some also explicitly ban scraping and will blacklist any IP address found to be so-doing. A Restful API will make life much easier for you as a programmer and make the server much more responsive for normal users. If you decide you still need to go that route you mentioned Selenium and that's the current best practice for that task.I think there's a support forum/list on the Selenium site that can probably help you out. -- 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 From mats at wichmann.us Fri Dec 29 12:39:51 2023 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 29 Dec 2023 10:39:51 -0700 Subject: [Tutor] tkinter designer Message-ID: Ran across this elsewhere... people here sometimes ask about why a tkinter design attempt doesn't come out as expected. It seems someone has made an effort at building a designer for tkinter: https://github.com/ParthJadhav/Tkinter-Designer it's on PyPi as tkdesigner. Have no experience with it, but maybe it might of some use to someone someday... It uses another project which has a free tier but also paid versions, just so that's mentioned. From mats at wichmann.us Fri Dec 29 12:42:17 2023 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 29 Dec 2023 10:42:17 -0700 Subject: [Tutor] tkinter designer In-Reply-To: References: Message-ID: <0727bb91-9b1d-4926-a53a-d463bd32b1e3@wichmann.us> On 12/29/23 10:39, Mats Wichmann wrote: > > Ran across this elsewhere... people here sometimes ask about why a > tkinter design attempt doesn't come out as expected.? It seems someone > has made an effort at building a designer for tkinter: > > https://github.com/ParthJadhav/Tkinter-Designer > > it's on PyPi as tkdesigner.? Have no experience with it, but maybe it > might of some use to someone someday...? It uses another project which > has a free tier but also paid versions, just so that's mentioned. and... it seems there's an even more mature designer here: https://sourceforge.net/projects/page/ From ethanrosenberg414 at gmail.com Fri Dec 29 15:43:13 2023 From: ethanrosenberg414 at gmail.com (Ethan Rosenberg) Date: Fri, 29 Dec 2023 15:43:13 -0500 Subject: [Tutor] uplownumpnct.py Message-ID: Tutor - Thank you for the wonderful help you provide to Python programmers!! What is syntax error? sent = 'THIS is the way we wash 1 2 3 , , !' lgn = len(sent) lgn2 = lgn if letter in sent: x=letter.isupper(): if x = 1: print('wow!') Ethan Rosenberg From alan.gauld at yahoo.co.uk Fri Dec 29 20:37:15 2023 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 30 Dec 2023 01:37:15 +0000 Subject: [Tutor] uplownumpnct.py In-Reply-To: References: Message-ID: On 29/12/2023 20:43, Ethan Rosenberg wrote: > What is syntax error? Please always post the full error message. Python error messages are extremely informative (once you figure out how to read them! And even more so in the most recent versions) > lgn = len(sent) > lgn2 = lgn You don't user either iof these variables although I guess you plan to later?) so they are irrelevant to this example. Cutting out irrelevant code helps us (and you?) locate the error more quickly. > if letter in sent: > x=letter.isupper(): > if x = 1: There is no need to indent the if statement - and in fact it is a syntax error to do so. (Although it will reported as an Indentation error.) Also you should be using == to test equality. A single = is an assignment and not allowed in an if expression. So two errors in a single line!. Also since isupper() is a predicate you should really test for truth not for a number so either: if x == True: or if x: or even better, combine the lines: if letter.isupper(): Or even combine all three lines: if letter in sent and letter.isupper(): print('wow') -- 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 From PythonList at DancesWithMice.info Fri Dec 29 21:00:22 2023 From: PythonList at DancesWithMice.info (dn) Date: Sat, 30 Dec 2023 15:00:22 +1300 Subject: [Tutor] uplownumpnct.py In-Reply-To: References: Message-ID: <30610283-4f7b-4c99-8880-c5548875f73d@DancesWithMice.info> On 30/12/23 09:43, Ethan Rosenberg wrote: > Tutor - > > Thank you for the wonderful help you provide to Python programmers!! Glad you appreciate the help. > What is syntax error? Perhaps then, you could help those who help you? What is the exact error-message received? If you were to copy-paste same into this message, would it ease the work-load? > sent = 'THIS is the way we wash 1 2 3 , , !' > > lgn = len(sent) > lgn2 = lgn > if letter in sent: > x=letter.isupper(): > if x = 1: > print('wow!') What are the rules about 'code-blocks' denoted by indentation? -- Regards, =dn From threesomequarks at proton.me Fri Dec 29 21:54:40 2023 From: threesomequarks at proton.me (ThreeBlindQuarks) Date: Sat, 30 Dec 2023 02:54:40 +0000 Subject: [Tutor] uplownumpnct.py In-Reply-To: References: Message-ID: Ethan, Regular thanks expressed does not get you answers. Taking a bit more time to explain what your code is supposed to do would be more helpful. As others keep asking, since we can not actually run the code with so many mistakes, you should copy a transcript of the code running, perhaps a line or two at a time, so we can see where the rror messages pop up. Others have noted code you are not using, at least not yet, and that can safely be removed when you show a sample that generates the erros. But what we do NOT see is whether you initialized the variable you call "letter" and if so, to what. When I set letter to a single character, it sort of works: >>> letter='e' >>> if letter in sent: ... x=letter.isupper() ... >>> x False But why you are checking if it is upper case is not stated. But why is there a colon at the end of that line? And why is the rest of the code indented deeper and why are you comparing a True/False value to 1? I mean, it sort of works as like some languages, True and 1 both evaluate to Truthy. There are many possible errors above and perhaps even higher up in the code you may not be showing. Python will normally show an error near where it catches it. Many syntax errors are possible. Until and unless you can show us better, perhaps it is best if others work with you and perhaps even someone local who can look over your shoulder. Good Luck. Sent with Proton Mail secure email. On Friday, December 29th, 2023 at 3:43 PM, Ethan Rosenberg wrote: > Tutor - > > Thank you for the wonderful help you provide to Python programmers!! > > What is syntax error? > > sent = 'THIS is the way we wash 1 2 3 , , !' > > lgn = len(sent) > lgn2 = lgn > if letter in sent: > x=letter.isupper(): > if x = 1: > print('wow!') > > Ethan Rosenberg > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor