[Tutor] randomly generated error
Hi, main idea was to : - get random characters word - randomly colour letters in my word - from this coloured word print all yellow letters As it is random i need to be sure that at least one letter is yellow so i put yellow color into final variable. This code works but randomly generates error. And i have no idea how solve this problem. Please help me Traceback (most recent call last): File "proj3", line 23, in w = "".join((colorpass[i.end()]) for i in re.finditer(re.escape(Y), colorpass)) File "proj3", line 23, in w = "".join((colorpass[i.end()]) for i in re.finditer(re.escape(Y), colorpass)) IndexError: string index out of range This is my code: from random import choice from string import ascii_letters, digits import re chars = ascii_letters + digits word = "".join([choice(chars) for i in range(10)]) R = '\033[31m' # red G = '\033[32m' # green B = '\033[34m' # blue P = '\033[35m' # purple Y = '\033[93m' # yellow colors = [R, G, B, P, Y] colorpass = "\033[93m" for char in word: colorpass += char + choice(colors) print(colorpass) w = "".join((colorpass[i.end()]) for i in re.finditer(re.escape(Y), colorpass)) print(w) I am using Python 3.5.2 on Ubuntu 16.04 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] randomly generated error
Freedom Peacemaker wrote: > Hi, > main idea was to : > - get random characters word > - randomly colour letters in my word > - from this coloured word print all yellow letters > > As it is random i need to be sure that at least one letter is yellow so i > put yellow color into final variable. Why? > This code works but randomly > generates error. And i have no idea how solve this problem. Please help me > > Traceback (most recent call last): > File "proj3", line 23, in > w = "".join((colorpass[i.end()]) for i in re.finditer(re.escape(Y), > colorpass)) > File "proj3", line 23, in > w = "".join((colorpass[i.end()]) for i in re.finditer(re.escape(Y), > colorpass)) > IndexError: string index out of range > > This is my code: > > from random import choice > from string import ascii_letters, digits > import re > > chars = ascii_letters + digits > > word = "".join([choice(chars) for i in range(10)]) > > R = '\033[31m' # red > G = '\033[32m' # green > B = '\033[34m' # blue > P = '\033[35m' # purple > Y = '\033[93m' # yellow > > colors = [R, G, B, P, Y] > > colorpass = "\033[93m" > for char in word: > colorpass += char + choice(colors) The character is followed by the color-code sequence. If the last color is yellow re.end() == len(colorpos) which is not a valid index into the string. > print(colorpass) > > w = "".join((colorpass[i.end()]) for i in re.finditer(re.escape(Y), > colorpass)) > print(w) > > I am using Python 3.5.2 on Ubuntu 16.04 To print a colored character you have to put the color sequence before it, so I'd do just that. Here's an example without regular expressions: word = "".join(choice(chars) for i in range(10)) colored_word = "".join(choice(colors) + c for c in word) yellow_chars = "".join(part[0] for part in colored_word.split(Y)[1:]) print(word) print(colored_word) print("\033[0m", end="") print(yellow_chars) As we know that at least one character follows the color it is OK to write part[0] above. If that's not the case it's easy to avoid the exception by using part[:1] as that works for the empty string, too: >>> "foo"[:1] 'f' >>> ""[:1] '' ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Using venv
It has been suggested to me that I should use a virtual environment and venv would be a good choice. I've read through PEP405 and this link [1]. Though some of it seems a little confusing to me, I'm sure I can get it up and running. This question seems a little dumb and maybe I am being a little dense, but then what? Your program/script is done how do you run it? Do you always have to activate your venv and run it from there? I like to run Tkinter programs from a launcher. Would that be possible and what would the command look like? Lately I have been writing some Libreoffice calc macros and evaluating pyspread for it's macro capability. Would modules installed in my venv be available to the spreadsheet programs? Thanks, Jim [1] https://realpython.com/blog/python/python-virtual-environments-a-primer/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using venv
On 27Jan2017 15:47, jim wrote: It has been suggested to me that I should use a virtual environment and venv would be a good choice. I've read through PEP405 and this link [1]. Though some of it seems a little confusing to me, I'm sure I can get it up and running. This question seems a little dumb and maybe I am being a little dense, but then what? Your program/script is done how do you run it? Do you always have to activate your venv and run it from there? I like to run Tkinter programs from a launcher. Would that be possible and what would the command look like? Lately I have been writing some Libreoffice calc macros and evaluating pyspread for it's macro capability. Would modules installed in my venv be available to the spreadsheet programs? The mere act of using the python from inside the venv invokes a python that runs off the venv, so all you really need to do is to contrive to execute that python instead of the system python where you want it. If your scripts start with something like this: #!/usr/bin/env python3 then you can simply contrive that "python3" is found from the virtualenv. You might put a symlink in your personal $HOME/bin directory, or source the venv's "activate" file from your shell's profile (this making the venv searched ahead of the system paths), etc. Or your launcher might simply invoke: $HOME/path/to/your/venv/bin/python your-tk-inter-script.py Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor