[Tutor] str.replace error
I'm working wtih python 3.7 on Windows 10. I'm trying to write some code in order to clean up the data in the csv file. Using spreadsheet language, I want to replace part of a cell ("Basic P1") with an empty string and write it in the comments cell. I thought assigning a variable and replacing the string would be a good idea. Here is my code: import csv with open('somefile') as csvDataFile: csvReader = csv.reader(csvDataFile) for row in range(100): a = "Basic P1" str.replace(a, "") print(next(csvReader)) I get an error: Traceback (most recent call last): File "somefile", line 7, in str.replace(a, "") TypeError: replace() takes at least 2 arguments (1 given) But I think I have 2 arguments: a being the "old" argument as per the documentation, "" being the "new" argument as per the documentation. What am I missing? -- Roger Lea Scherer 623.255.7719 *Strengths:* Input, Strategic, Responsibility, Learner, Ideation ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Installing python
Hey, I have installed python 3.7 on my computer Windows10 (C:\Users\Roger\AppData\Local\Programs\Python\Python37), but when I go to the git repository that holds all my projects and type "python --version" in WindowsPowershell, I get Python 3.6.5. So I guess my question is am I supposed to install python 3.7 in the git repository? and a related question I think, am I supposed to install python 3.7 in every virtual environment I create, or is there a way to link python to each virtual environment or each repo or each project? I guess I'm so used to installing programs in Windows or Mac and having them accessible globally (I guess is the word) that I expected python 3.7 to do the same thing, although I understand about programs written earlier crashing because of upgrades, like from 2.7 to 3.4 for instance. I'm so confused I'm not sure I even know what questions to ask, but the above questions seem to be a missing link in my knowledge at this point. Thank you as always. -- Roger Lea Scherer 623.255.7719 *Strengths:* Input, Strategic, Responsibility, Learner, Ideation <https://www.avast.com/sig-email?utm_medium=email_source=link_campaign=sig-email_content=webmail_term=icon> Virus-free. www.avast.com <https://www.avast.com/sig-email?utm_medium=email_source=link_campaign=sig-email_content=webmail_term=link> <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] pip install
Hey all, I'm trying to set up a virtual environment. I've used Windows Powershell on my desktop with Windows 10. I was able only to get to 2.5 minutes of a 9.75 minute video before getting frozen, not just stuck. I was unable to activate the batch file even though everything appeared to be where it should be. I then found out about Linux Bash Shell for Windows and used that, I next successfully installed virtualenv, but when I went to run it it said command virtualenv not found but can be installed with sudo apt install virtualenv, which I did, then tried to run 'virtualenv pdxCrime' (without the apostrophes) and it errors out with command virtualenv not found, did you mean: command 'virtualenv' from deb virtualenv Try: sudo apt install My bash shell is Ubuntu not debian. Not sure if that makes a difference. I don't understand what is. I don't understand why virtualenv isn't found when it was successfully installed and every piece of documentation I've found tells me to use that word or command. The only reason I ask this forum is because the instructor of the video hasn't responded to my email yet and this seems to be related to python somehow although I'm a little foggy about that. If this is not the forum please tell me where to go (be nice). Thank you. -- Roger Lea Scherer 623.255.7719 *Strengths:* Input, Strategic, Responsibility, Learner, Ideation ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Numpy documentation
I truly don't think I'm this stupid, but I can't even understand the fourth paragraph of the numpy documentation. https://docs.scipy.org/doc/numpy/user/quickstart.html says: In the example pictured below, the array has 2 axes. The first axis has a length of 2, the second axis has a length of 3. [[ 1., 0., 0.], [ 0., 1., 2.]] (I think) I understand the 2 axes. [1,0,0] (I'm lazy and don't want to type the periods) is one axis and [0,1,2] is the second axis. But then things get goofy. The first axis has a length of 2. Is that because [1,0,0] and [0,1,2] are counted as one axis? (I think) I understand the second axis has a length of 3 because there are 3 elements within the [0,1,2] axis. Is that correct? But why does the first axis have a length of 2? Because the second zero doesn't count? Did they change the example and forgot to change the text? Thank you for your help as always. -- Roger Lea Scherer 623.255.7719 *Strengths:* Input, Strategic, Responsibility, Learner, Ideation ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] IDLE
Can you direct me to where or how to update from Python 3.6.5 Shell, I think it is also called IDLE 3.6.5, to Python 3.7 Shell? I looked at the documentation which seems like there is another one, slightly improved. I've downloaded Python 3.7, but can't figure out how to upgrade the IDLE/Shell. And I can't for the life of me remember how I originally got the Python 3.6.5 Shell. Thank you -- Roger Lea Scherer 623.255.7719 *Strengths:* Input, Strategic, Responsibility, Learner, Ideation ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] localhosting
Thank you all for your help. I am still chugging away at this problem. I've switched to Windows PowerShell Version 5.1 since I can select, copy, and paste more easily in PowerShell. (I can't figure out how to do that in Anaconda and I get the same errors in PowerShell.) I removed the "3" in "python3" and it works; along with moving the "www" folder to where python resides. Thanks for the help. So it appears the local host is running correctly, but when I run this code: print("Hello World") Chrome does not render and I get an error message in PowerShell: 127.0.0.1 - - [06/Sep/2018 11:22:46] "GET /cgi-bin/hello.py HTTP/1.1" 200 - 127.0.0.1 - - [06/Sep/2018 11:22:46] command: C:\Users\Roger\AppData\Local\Programs\Python\Python37-32\python.exe -u C:\Users\Roger\documents\roger\python\www\cgi-bin\hello.py "" 127.0.0.1 - - [06/Sep/2018 11:22:46] b' File "C:\\Users\\Roger\\documents\\roger\\python\\www\\cgi-bin\\hello.py", line 1\r\nprint ""\r\n ^\r\nSyntaxError: Missing parentheses in call to \'print\'. Did you mean print("")?\r\n' 127.0.0.1 - - [06/Sep/2018 11:22:46] CGI script exit status 0x1 BUT when I run this code: def fib(n): if n < 2: return n return fib(n-1) + fib(n-2) print(fib(15)) Chrome does not render but I DON'T get an error message, I get this: 127.0.0.1 - - [06/Sep/2018 11:33:30] "GET /cgi-bin/recursive%20fibonacci.py HTTP/1.1" 200 - 127.0.0.1 - - [06/Sep/2018 11:33:30] command: C:\Users\Roger\AppData\Local\Programs\Python\Python37-32\python.exe -u "C:\Users\Roger\documents\roger\python\www\cgi-bin\recursive fibonacci.py" "" 127.0.0.1 - - [06/Sep/2018 11:33:30] CGI script exited OK So I'm confused about 2 things: 1. Why does the one file cause an error in PowerShell (and Anaconda actually) when it seems to be the same as the other file which appears to run with no error, but just not render? and, 2. What am I missing: why won't it render since the instructions I have at the very beginning of this email say, basically, this is all I have to do to get it to render? I know this second question is maybe a little bit outside this forum, but I am struggling to make sense out of what little I know and, truly, this forum seems the nicest and most helpful I've encountered. Thank you as always. On Thu, Aug 30, 2018 at 6:01 AM Mats Wichmann wrote: > On 08/30/2018 02:54 AM, Alan Gauld via Tutor wrote: > > On 30/08/18 00:09, Roger Lea Scherer wrote: > >> I'm trying to implement a local host. My instructions tell me to type > the > >> following command in the command line, make sure I'm in the "www" folder > > > > So this is not the folder where python3 is installed. (See below) > > > >> python3 -m http.server --cgi 8000 > > > >> I'm running Anaconda in Windows 10. I get an error: 'python3' is not > >> recognized as an internal or external command, operable program or batch > >> file. > >> But this is the folder that I thought Python 3.6 was in. > > > > you said you were in www which is not where Python should > > be installed. > > > > It is likely that Python is not in your system PATH. > > You need to find out where it is installed and add it. > > In a standard Python install it would either be in > > C:\PROGRAM FILES\PYTHON or > > C:\PYTHON > > > > But Anaconda (version please?) could use its own path > > such as > > > > C:\ANACONDA\PYTHON > > > >> So how do I correct this? I suspect python3 isn't in this folder. I > know I > >> have python3 because I run python3.6 shell practically every day. > > > > How do you run it? > >>From a command line or via a menu/shortcut? > > > >> looked in the Program Files folder, came up empty as far as python is > >> concerned, but I don't know where else to look? > > Several things that could help: > > you can ask python itself to tell you where it is, since you say python > works for you. > > >>> import sys > >>> print(sys.executable) > > the standard Windows python defaults to a "user install", so it could be > in a place like > > {yourhomdirectory}/AppData/Local/Programs/Python > > Anaconda, as Alan says, likely puts it somewhere different. > > Python 3 isn't named python3 on Windows unless you take steps to make it > so, it's just called python. So modify your instructions accordingly. > > Also on Windows, if it was installed, there is a separate thing called > the Python Launcher, which lets you run the command "py" which typically > gets put in a place that is always found, and avoids the fiddling with > getting Python
[Tutor] localhosting
I'm trying to implement a local host. My instructions tell me to type the following command in the command line, make sure I'm in the "www" folder (which I am and not a subfolder, although there is a subfolder "cgi-bin") and then run this: python3 -m http.server --cgi 8000 I'm running Anaconda in Windows 10. I get an error: 'python3' is not recognized as an internal or external command, operable program or batch file. But this is the folder that I thought Python 3.6 was in. I tried Windows PowerShell and received this error message: python3 : The term 'python3' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. So how do I correct this? I suspect python3 isn't in this folder. I know I have python3 because I run python3.6 shell practically every day. I've looked in the Program Files folder, came up empty as far as python is concerned, but I don't know where else to look? Or if this is not the correct forum, can you please (re)direct me to the correct one? Thank you. -- Roger Lea Scherer 623.255.7719 *Strengths:* Input, Strategic, Responsibility, Learner, Ideation ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Accessing a tuple of a dictionary's value
So I'm trying to divide fractions, technically I suppose integers. So, for instance, when the user inputs a 1 as the numerator and a 2 as the denominator to get the float 0.5, I want to put the 0.5 as the key in a dictionary and the 1 and the 2 as the values of the key in a list {0.5: [1, 2]}, hoping to access the 1 and 2 later, but not together. I chose a dictionary so the keys won't duplicate. I can't find anything in StackOverflow or Python documentation specifically about this. They talk about accessing a list or a tuple or a dictionary, but not when the value is a tuple or list. I've tried: fractions.values([0]) which gives a TypeError: values() takes no arguments (1 given) I've tried fractions.values()[0] which gives a TypeError: 'dict_values' object does not support indexing Both of these errors sort of make sense to me, but I can't find a way to access the 1 or the 2 in the dictionary key:value pair {0.5: [1, 2]} Thank you for your help. -- Roger Lea Scherer 623.255.7719 *Strengths:* Input, Strategic, Responsibility, Learner, Ideation ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Removing duplicates
I'm trying to get a list of tuples to be a float, a numerator, and a denominator for all the fractions: halves, thirds, fourths etc up to ninths. 1/2 returns the same float as 2/4, 3/6, 4/8. I would like to keep only the 1/2. When I try (line 18) to "pop" from the list I get a "TypeError: integer argument expected, got float". When I try (line 18) to "remove" from the list, nothing happens: nothing is removed and I do not receive an error message. What do you think is a good way to solve this? Thank you as always. import math fractions = [(0, 0, 0)] for i in range(1, 10): for j in range(1, 10): if i < j: x = i/j if x not in fractions: fractions.append((x, i, j)) sortedFrac = sorted(fractions) print(sortedFrac) for i in range(len(sortedFrac)): try: if sortedFrac[i][0] == sortedFrac[i-1][0]: # so if the float equals the previous float sortedFrac.pop(sortedFrac[i][0]) # remove the second float else: sortedFrac.append(sortedFrac[i][0]) except ValueError: continue -- Roger Lea Scherer 623.255.7719 *Strengths:* Input, Strategic, Responsibility, Learner, Ideation ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Recursion
My foggy understanding of recursion is probably the reason I can't figure this out. When turtle draws this program there is an orange line in the green which I would prefer not to have. I've tried all I could think of, but can't get the orange line to go away, or maybe more accurately, not to be drawn. The program goes to the end of the recursion and then starts drawing? which seems wrong, because the trunk of the tree is drawn first. Maybe: How does it know to draw the second orange line? and how does it know when to stop with only two branches from the trunk? Thank you as always. - import turtle def tree(branchLen, width, t): if branchLen > 5: t.pensize(width) t.forward(branchLen) t.right(20) tree(branchLen-15, width-5, t) t.left(40) tree(branchLen-15, width-5, t) t.pencolor("orange") t.right(20) t.backward(branchLen) t.pencolor("green") def main(): t = turtle.Turtle() myWin = turtle.Screen() t.speed(0) t.left(90) t.up() t.backward(100) t.down() t.color("green") tree(75, 20, t) myWin.exitonclick() main() -- Roger Lea Scherer 623.255.7719 *Strengths:* Input, Strategic, Responsibility, Learner, Ideation ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iteration issues
I've given up again. I've tried the python documentation, I've tried to implement the above suggestions, I've tried the translate thing with a table that I did not include this time because I couldn't get it to work, I've looked at StackOverflow, Quora and another website, so many things seem so promising, but I just can't get them to work nor figure out why they won't. I've started from scratch again. ** import string gettysburg = open("C:/Users/Roger/Documents/GitHub/LaunchCode/gettysburg.txt", "r") puncless = "" for char in gettysburg: if char in string.punctuation: gettysburg.replace(char, "") else: puncless += char print(puncless) ** The "puncless" part works fine, except it does not replace the punctuation like I expect and want. Thank you for all your help already, but I need some more if you have any. :) Thanks. On Sat, May 12, 2018 at 12:40 AM, Peter Otten <__pete...@web.de> wrote: > Neil Cerutti wrote: > > > punctuation_removal_table = str.maketrans({c: None for c in > > string.punctuation}) > > Alternative spellings: > > >>> from string import punctuation > >>> (str.maketrans({c: None for c in punctuation}) > ... == str.maketrans(dict.fromkeys(punctuation)) > ... == str.maketrans("", "", punctuation)) > True > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Roger Lea Scherer 623.255.7719 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Turtle drawing
I give up again. Here's my code which gives no error, but draws something that looks sort of like a dinosaur. I've included the first few lines of the text file mystery so you can see the format I received from the instructions. Please help. Thank you. *Mystery excerpt* UP -218 185 DOWN -240 189 -246 188 -248 183 -246 178 ** # Draw a picture based on the txt file mystery # When UP go to the next line, read x,y coordinates, go to that point # When DOWN go to the next line, read x,y coordinates, go to that point and continue until UP import turtle wn = turtle.Screen() # Set up the window and its attributes wn.bgcolor("beige") hadir = turtle.Turtle() # create hadir hadir.color('orange') hadir.speed(0) datums = open("C:/Users/Roger/Documents/GitHub/LaunchCode/mystery.txt", "r") for aline in datums: splitted = aline.split() try: coords = [int(i) for i in splitted] x = coords[0] y = coords[1] hadir.goto(x, y) except: while splitted == "UP": hadir.pu() while splitted == "DOWN": hadir.pd() turtle.exitonclick() -- Roger Lea Scherer 623.255.7719 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Iteration issues
Hello, again. I want to count words in a text file. If a word repeats I want to increase the count by 1; if the word is new to the dictionary, I want to add the word to the dictionary. Everything works like I would like and expect, except for it only adds the last word of each line to the dictionary. What am I missing? import string file_name = 'oxford.txt' wordset = {} with open(file_name, 'r') as f: for line in f: sentence = line.strip() sentence = sentence.strip(string.punctuation) print(sentence) sentence = sentence.lower() word_list = sentence.strip() word_list = word_list.split(' ') for i in range(len(word_list)): word_list[i] = word_list[i].strip(string.punctuation) print(word_list) if word_list[i] in wordset: wordset[word_list[i]] += 1 else: wordset[word_list[i]] = 1 print(wordset) The output is: (I included only the first four lines) The Project Gutenberg EBook of Advice to a Young Man upon First Going to ['the', 'project', 'gutenberg', 'ebook', 'of', 'advice', 'to', 'a', 'young', 'man', 'upon', 'first', 'going', 'to'] {'to': 1} Oxford, by Edward Berens ['oxford', 'by', 'edward', 'berens'] {'to': 1, 'berens': 1} [''] {'to': 1, 'berens': 1, '': 1} This eBook is for the use of anyone anywhere at no cost and with ['this', 'ebook', 'is', 'for', 'the', 'use', 'of', 'anyone', 'anywhere', 'at', 'no', 'cost', 'and', 'with'] {'to': 1, 'berens': 1, '': 1, 'with': 1} Thank you as always. -- Roger Lea Scherer 623.255.7719 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] beginning encryption
I am absolutely stumped. I've tried a number of different scenarios and copied the answer more than I like, but I still can't figure this out. I don't want to copy the answer verbatim because then I won't learn. I'm doing the beginning cipher, mix up the letters routine. I get the entire Gettysburg address with no alterations in this form of the code (and a few others I've tried). I do not receive any error, but I expect the character in the Gettysburg address to change to the index position in the encryption variable. What am I not getting? Thank you as always. address = """Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth.""" alphabet = "abcdefghijklmnopqrstuvwxyz" encryption = "nopqrstuvwxyzabcdefghijklm" def encryptor(address): encrypted = "" for char in address: if char != alphabet: encrypted += char else: pos = alphabet.index(char) encrypted += encryption[pos] print(encrypted) encryptor(address) -- Roger Lea Scherer 623.255.7719 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Return problems
So I've examined my educational material, I've perused the python documentation and for the life of me I can't figure out why return doesn't print a result in the IDE. I use print, the program acts as expected. What am I missing? import math from decimal import Decimal def findHypot(a, b): c = math.sqrt((a ** 2) + (b ** 2)) if c == (round(c)): return round(c) else: return round(c, 4) findHypot(3, 4) from the IDE: = RESTART: C:\Users\Roger\Documents\GitHub\LaunchCode\hypotenuse.py = >>> Thank you as always. -- Roger Lea Scherer 623.255.7719 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Turtle color question
I've tried everything I can think of but cannot get the pencolor to change. (Although the arrow drawing the boxes changes color.) I've placed it in places I didn't even think it would work. The arrow draws what looks like the boxes I expect, but the color looks the same as the background. So I assigned a variable and printed that variable which says None. See: RESTART: C:/Users/Roger/AppData/Local/Programs/Python/Python36/turtle square draw.py None >>> Removing " color = " doesn't change the outcome. I hope this adequately explains things.Thanks, again. import turtle def drawSquare(t, sz): """Get turtle t to draw a square of sz side""" t.pu() t.forward(sz*2) for i in range(4): t.forward(sz) t.left(90) wn = turtle.Screen() wn.bgcolor("blue") alex = turtle.Turtle() color = alex.pencolor("darkgreen") print(color) def main(): for i in range(4): drawSquare(alex,20) main() wn.exitonclick() -- Roger Lea Scherer 623.255.7719 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Pi approximation
In one of my lessons I am asked to compare approximations for pi. I got everything to work properly and my attempt is successful and matches Python's approximation up to 15 digits to the right of the decimal, but I suspect I can do this programmatically rather than the repetitious way I did. I tried "for i in range(10):"; then I tried "c += c" so it would be a sum. Those attempts did not work. I tried math.fsum and though the documentation says it is for iterables I could not get it to work as I desired. I received an error that said TypeError: 'float' object is not iterable I included all the code so I wouldn't neglect any you might need. Can you help again? Thank you. # compare various approximations of pi import math import random # simplest estimate a = 22/7 print(a) # next simplest b = 355/113 print(b) # from wikipedia: # In 1910, the Indian mathematician Srinivasa Ramanujan found several rapidly converging infinite series c = (2*math.sqrt(2)/9801) * (((math.factorial(4*0))*(1103+26390*0)) / ((math.factorial(0)**4)*(396**(4*0 d = (2*math.sqrt(2)/9801) * (((math.factorial(4*1))*(1103+26390*1)) / ((math.factorial(1)**4)*(396**(4*1 e = (2*math.sqrt(2)/9801) * (((math.factorial(4*2))*(1103+26390*2)) / ((math.factorial(2)**4)*(396**(4*2 f = (2*math.sqrt(2)/9801) * (((math.factorial(4*3))*(1103+26390*3)) / ((math.factorial(3)**4)*(396**(4*3 g = (2*math.sqrt(2)/9801) * (((math.factorial(4*4))*(1103+26390*4)) / ((math.factorial(4)**4)*(396**(4*4 h = (2*math.sqrt(2)/9801) * (((math.factorial(4*5))*(1103+26390*5)) / ((math.factorial(5)**4)*(396**(4*5 i = (2*math.sqrt(2)/9801) * (((math.factorial(4*6))*(1103+26390*6)) / ((math.factorial(6)**4)*(396**(4*6 j = (2*math.sqrt(2)/9801) * (((math.factorial(4*7))*(1103+26390*7)) / ((math.factorial(7)**4)*(396**(4*7 k = (2*math.sqrt(2)/9801) * (((math.factorial(4*8))*(1103+26390*8)) / ((math.factorial(8)**4)*(396**(4*8 l = (2*math.sqrt(2)/9801) * (((math.factorial(4*9))*(1103+26390*9)) / ((math.factorial(9)**4)*(396**(4*9 m = c + d + e + f + g + h + i + j + k + l print(1/m) print(math.pi) -- Roger Lea Scherer 623.255.7719 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Polygon fill in turtle
I looked at Stackoverflow, followed, but could not succeed. I want to fill the polygon with a different color than the pen color. When I print out the filler it is the correct color, but the actual fill is not. Here it is: (tada!) # create a user-defined polygon import turtle wn = turtle.Screen() hadir = turtle.Turtle() hadir.speed(8) sides = int(input("Please enter the number of sides of a polygon you want drawn: ")) length = int(input("How long would you like each side? ")) outlineColor = input("What color would you like for the outline?") filler = input("And what color would you like to fill it with? ") hadir.begin_fill() hadir.color(outlineColor) print(filler) for i in range(sides): hadir.forward(length) hadir.left(360 / sides) hadir.up() hadir.end_fill() Thanks, as always. -- Roger Lea Scherer 623.255.7719 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Turtle Issues
I know I don't have to apologize, but I am so independent I hate asking for help and you guys and gals have been so helpful that now that I'm stuck again I'm sorry I have to. I've looked on StackOverflow and followed their advice (which I can't make work) and went to the python/turtle documentation which I tried to follow, but can't seem to make work either. All I want to do (famous last words) is randomize the color of the pen when the ciphers are created, but when the ciphers are created, the color remains black. The comments below colormode are things I've tried as well. I include all the code in case you want to run the program. So if you could please help like you do, that would be great and greatly appreciated. Thank you. from turtle import * import re import random # use turtle to draw ciphers of the Cistercian monks digits = input("Please enter an integer less than 10,000 greater than 0: ") r = random.randrange(0, 255) g = random.randrange(0, 255) b = random.randrange(0, 255) # ensure input is only digits p = re.compile(r'^\d+$') m = p.match(digits) if m: print(digits) m = digits #digits = input("Please enter an integer less than 10,000 greater than 0: ") else: print("No match") colormode(255) ##reddish = random.randrange(255) ##greenish = random.randrange(255) ##bluish = random.randrange(255) ##pencolor(reddish, greenish, bluish) # pencolor(random.randrange(255), random.randrange(255), random.randrange(255)) pencolor(r, g, b) mode("logo") # resets turtle heading to north speed(0) ht() fd(100) # if statements for the ones position if digits[-1] == "1": pu() goto(0, 100) seth(90) pd() fd(35) if digits[-1] == "2": pu() goto(0, 65) seth(90) pd() fd(35) if digits[-1] == "3": pu() goto(0, 100) seth(135) pd() fd(50) if digits[-1] == "4": pu() goto(0, 65) seth(45) pd() fd(50) if digits[-1] == "5": pu() goto(0, 100) seth(90) pd() fd(35) rt(135) fd(50) if digits[-1] == "6": pu() goto(30, 100) seth(180) pd() fd(35) if digits[-1] == "7": pu() goto(0, 100) seth(90) pd() fd(35) rt(90) fd(35) if digits[-1] == "8": pu() goto(0, 65) seth(90) pd() fd(35) lt(90) fd(35) if digits[-1] == "9": pu() goto(0, 100) seth(90) pd() fd(35) rt(90) fd(35) rt(90) fd(35) # if statements for the tens position if digits[-2:-1] == "1": pu() goto(0, 100) seth(-90) pd() fd(35) if digits[-2:-1] == "2": pu() goto(0, 65) seth(-90) pd() fd(35) if digits[-2:-1] == "3": pu() goto(0, 100) seth(-135) pd() fd(50) if digits[-2:-1] == "4": pu() goto(0, 65) seth(-45) pd() fd(50) if digits[-2:-1] == "5": pu() goto(0, 100) seth(-90) pd() fd(35) lt(135) fd(50) if digits[-2:-1] == "6": pu() goto(-30, 100) seth(180) pd() fd(35) if digits[-2:-1] == "7": pu() goto(0, 100) seth(-90) pd() fd(35) lt(90) fd(35) if digits[-2:-1] == "8": pu() goto(0, 65) seth(-90) pd() fd(35) rt(90) fd(35) if digits[-2:-1] == "9": pu() goto(0, 100) seth(-90) pd() fd(35) lt(90) fd(35) lt(90) fd(35) # if statments for the hundreds position if digits[-3:-2] == "1": pu() goto(0, 0) seth(90) pd() fd(35) if digits[-3:-2] == "2": pu() goto(0, 35) seth(90) pd() fd(35) if digits[-3:-2] == "3": pu() goto(0, 0) seth(45) pd() fd(50) if digits[-3:-2] == "4": pu() goto(0, 35) seth(135) pd() fd(50) if digits[-3:-2] == "5": pu() goto(0, 0) seth(90) pd() fd(35) lt(135) fd(50) if digits[-3:-2] == "6": pu() goto(30, 0) seth(0) pd() fd(35) if digits[-3:-2] == "7": pu() goto(0, 0) seth(90) pd() fd(35) lt(90) fd(35) if digits[-3:-2] == "8": pu() goto(0, 35) seth(90) pd() fd(35) rt(90) fd(35) if digits[-3:-2] == "9": pu() goto(0, 0) seth(90) pd() fd(35) lt(90) fd(35) lt(90) fd(35) # if statments for the thousands position if digits[-4:-3] == "1": pu() goto(0, 0) seth(-90) pd() fd(35) if digits[-4:-3] == "2": pu() goto(0, 35) seth(-90) pd() fd(35) if digits[-4:-3] == "3": pu() goto(0, 0) seth(-35) pd() fd(50) if digits[-4:-3] == "4": pu() goto(0, 35) seth(-135) pd() fd(50) if digits[-4:-3] == "5": pu() goto(0, 0) seth(-90) pd() fd(35) rt(135) fd(50) if digits[-4:-3] == "6": pu() goto(-30, 0) seth(0) pd() fd(35) if digits[-4:-3] == "7": pu() goto(0, 0) seth(-90) pd() fd(35) rt(90) fd(35) if
[Tutor] Another question, dictionaries
I'm trying to finish chapter 5 in my book and it has to do with dictionaries and creating simple games. We need a pool of points and the ability to exchange point between the pool and each of the, what I call, attributes of Strength, Health, Wisdom, and Dexterity. I got only to the first option and I'm stuck on how to get the points to change "permanently". The program will run well for option 1 (I haven't got to the rest of the options), but the next time I run the program it goes back to the original configuration. Did I do everything right, but I just need to write to a file, and that's what the problem is? or what? I've reread the chapter, I've gone on stackoverflow and thegeekstuff.com, I've thought about it for at least a day and I just can't figure out what the problem is. Please help? # Character creator program for role playing pool = 30 attributes = {"Strength": 0, "Health": 0, "Wisdom": 0, "Dexterity": 0} print("You have", pool, "points") print(""" You can apply them to your Strength, Health, Wisdom, or Dexterity. Press: 0 to Quit 1 to apply points to Strength 2 to apply points to Health 3 to apply points to Wisdom 4 to apply points to Dexterity 5 to apply points from Strength to your pool 6 to apply points from Health to your pool 7 to apply points from Wisdom to your pool 8 to apply points from Dexterity to your pool """) choice = input("Choice: ") print(attributes.get("Strength")) if choice == "0": print("Good-bye.") input("\n\nPress enter to exit.") elif choice == "1": strengthChoice = input("How many points would you like to transfer from pool to Strength?: ") print(strengthChoice) attributes["Strength"] += int(strengthChoice) print(attributes.get("Strength")) pool -= int(strengthChoice) print(pool) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Regex not working as desired
The first step is to input data and then I want to check to make sure there are only digits and no other type of characters. I thought regex would be great for this. The program works great, but no matter what I enter, the regex part does the same thing. By same thing I mean this: RESTART: C:\Users\Roger\Documents\GitHub\ciphers\cipher1.py Please enter an integer less than 10,000 greater than 0: 4jkk33 No match >>> RESTART: C:\Users\Roger\Documents\GitHub\ciphers\cipher1.py Please enter an integer less than 10,000 greater than 0: 4k33 No match >>> RESTART: C:\Users\Roger\Documents\GitHub\ciphers\cipher1.py Please enter an integer less than 10,000 greater than 0: 4jjk4 No match >>> RESTART: C:\Users\Roger\Documents\GitHub\ciphers\cipher1.py Please enter an integer less than 10,000 greater than 0: 4334 No match So I don't know what I'm doing wrong. The cipher will still draw, but I want to return an "error message" in this case print("No match"), but it does it every time, even when there are only digits; that's not what I want. Please help. Below is my code: from turtle import * import re # use turtle to draw ciphers of the Cistercian monks digits = input("Please enter an integer less than 10,000 greater than 0: ") """ ensure input is no other characters than digits sudocode: if the input has anything other than digits return digits """ #def digit_check(digits): # I thought making it a function might h p = re.compile(r'[^\D]') m = p.match(digits) if m: print("No match") else: print(digits) digits = m #digit_check(digits) mode("logo") # resets turtle heading to north speed(0) ht() fd(100) # if statements for the ones position if digits[-1] == "1": pu() goto(0, 100) seth(90) pd() fd(35) if digits[-1] == "2": pu() goto(0, 65) seth(90) pd() fd(35) if digits[-1] == "3": pu() goto(0, 100) seth(135) pd() fd(50) if digits[-1] == "4": pu() goto(0, 65) seth(45) pd() fd(50) if digits[-1] == "5": pu() goto(0, 100) seth(90) pd() fd(35) rt(135) fd(50) if digits[-1] == "6": pu() goto(30, 100) seth(180) pd() fd(35) if digits[-1] == "7": pu() goto(0, 100) seth(90) pd() fd(35) rt(90) fd(35) if digits[-1] == "8": pu() goto(0, 65) seth(90) pd() fd(35) lt(90) fd(35) if digits[-1] == "9": pu() goto(0, 100) seth(90) pd() fd(35) rt(90) fd(35) rt(90) fd(35) # if statements for the tens position if digits[-2:-1] == "1": pu() goto(0, 100) seth(-90) pd() fd(35) if digits[-2:-1] == "2": pu() goto(0, 65) seth(-90) pd() fd(35) if digits[-2:-1] == "3": pu() goto(0, 100) seth(-135) pd() fd(50) if digits[-2:-1] == "4": pu() goto(0, 65) seth(-45) pd() fd(50) if digits[-2:-1] == "5": pu() goto(0, 100) seth(-90) pd() fd(35) lt(135) fd(50) if digits[-2:-1] == "6": pu() goto(-30, 100) seth(180) pd() fd(35) if digits[-2:-1] == "7": pu() goto(0, 100) seth(-90) pd() fd(35) lt(90) fd(35) if digits[-2:-1] == "8": pu() goto(0, 65) seth(-90) pd() fd(35) rt(90) fd(35) if digits[-2:-1] == "9": pu() goto(0, 100) seth(-90) pd() fd(35) lt(90) fd(35) lt(90) fd(35) # if statments for the hundreds position if digits[-3:-2] == "1": pu() goto(0, 0) seth(90) pd() fd(35) if digits[-3:-2] == "2": pu() goto(0, 35) seth(90) pd() fd(35) if digits[-3:-2] == "3": pu() goto(0, 0) seth(45) pd() fd(50) if digits[-3:-2] == "4": pu() goto(0, 35) seth(135) pd() fd(50) if digits[-3:-2] == "5": pu() goto(0, 0) seth(90) pd() fd(35) lt(135) fd(50) if digits[-3:-2] == "6": pu() goto(30, 0) seth(0) pd() fd(35) if digits[-3:-2] == "7": pu() goto(0, 0) seth(90) pd() fd(35) lt(90) fd(35) if digits[-3:-2] == "8": pu() goto(0, 35) seth(90) pd() fd(35) rt(90) fd(35) if digits[-3:-2] == "9": pu() goto(0, 0) seth(90) pd() fd(35) lt(90) fd(35) lt(90) fd(35) # if statments for the thousands position if digits[-4:-3] == "1": pu() goto(0, 0) seth(-90) pd() fd(35) if digits[-4:-3] == "2": pu() goto(0, 35) seth(-90) pd() fd(35) if digits[-4:-3] == "3": pu() goto(0, 0) seth(-35) pd() fd(50) if digits[-4:-3] == "4": pu() goto(0, 35) seth(-135) pd() fd(50) if digits[-4:-3] == "5": pu() goto(0, 0) seth(-90) pd() fd(35) rt(135) fd(50) if digits[-4:-3] == "6": pu() goto(-30, 0) seth(0) pd()
[Tutor] xlrd has no attribute
I'm trying to learn how open a xls file in python. I thought this would be easier. I've read on a blog this code and seen it on other websites. I thought I'd try it out. I'm on Windows 10 with python 3.6.4. import xlrd def open_file(path): """ Open and read an Excel file """ book = xlrd.open_workbook(path) # print number of sheets print(book.nsheets) # print sheet names print(book.sheet_names()) # get the first worksheet first_sheet = book.sheet_by_index(0) # read a row print(first_sheet.row_values(0)) # read a cell cell = first_sheet.cell(0,0) print(cell) print(cell.value) open_file("C:/Users/Roger/Documents/Roger/archive/area codes.xls") But I get an error: Traceback (most recent call last): File "C:\Users\Roger\Documents\Roger\Python\xlrd.py", line 1, in import xlrd File "C:\Users\Roger\Documents\Roger\Python\xlrd.py", line 26, in open_file("C:/Users/Roger/Documents/Roger/archive/area codes.xls") File "C:\Users\Roger\Documents\Roger\Python\xlrd.py", line 7, in open_file book = xlrd.open_workbook(path) AttributeError: module 'xlrd' has no attribute 'open_workbook' I've looked at http://www.lexicon.net/sjmachin/xlrd.html and it looks like it should be ok. I've looked at Stack overflow and I have the opening/closing parentheses appropriately placed to make it a method. Can you help, please? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Floating decimal question
This is my first time in this "forum", please be patient I will do my best. As I was going through a book and came across this challenge, I did what I believe was a success. And I know there are ways of making the decimal place be limited to 2 places, but my question is more of understanding why the following happened. This is the code I wrote in python: bill = float(input("What is the price of the bill?: ")) tip15 = bill*1.15 tip20 = bill*1.20 print("Bill plus 15% gratuity is " + str(tip15)) print("Bill plus 20% gratuity is " + str(tip20)) This is the result I got after I ran the code in an IDE (obviously) and then entered 29.99 in the first line: What is the price of the bill?: 29.99 Bill plus 15% gratuity is 34.4884995 Bill plus 20% gratuity is 35.988 My question is why does the 15% gratuity go so far beyond the decimal place when really there should only be 4 places because of multiplication rules, you know, and I do understand sometimes things work behind the scenes that you don't see, but on the 20% gratuity it gives the "right" answer? So I guess I'm just asking why did this happen like this? Thank you. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor