r/inventwithpython • • Sep 06 '15

Problem with Pyerclip

1 Upvotes

I feel very stupid but I cannot get pyperclip to work. I can import it but when I try to use it I get this error. What am I doing wrong?

Traceback (most recent call last):

File "<ipython-input-18-dcb46002907e>", line 1, in <module> pyperclip.paste()

File "D:\Downloads\pyperclip-1.5.11\pyperclip_init_.py", line 34, in _pasteWindows d.user32.OpenClipboard(0 if PY2 else None)

ArgumentError: argument 1: <class 'TypeError'>: wrong type


r/inventwithpython • • Aug 29 '15

Problem with Reversi (Invent Games w/Python chap. 15)

1 Upvotes

Hi everyone, I'm having an issue with the code in chapter 15 of Invent Games with Python. I've entered the code for Reversi, but I keep on getting an error that seems to boil down to this line:

for xdirection, ydirection in [[0, 1], [1, 1], [1, 0], [1. -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]]:

And the error I'm getting is:

ValueError: need more than 1 value to unpack

Any ideas?


r/inventwithpython • • Aug 25 '15

1 month later and my problem still has no replies (chapter 7)

1 Upvotes

Pressing step on line 5 opens up pyshell.py and when I click out, all the buttons on the debug get grayed out. This wasn't mentioned in the tutorial once. I literally copied the example code word for word, and I'm using python 3.4.3. Can someone please explain what's happening?


r/inventwithpython • • Aug 12 '15

Chapter 9: replace string with list

1 Upvotes

I'm currently at chapter 9, and I wrote the entire program, but I have a question: how can I replace the words string with a list? I ask it mainly because I tried to insert it as it is, but it said PEP 8: Line too long (using PyCharm) + that I also think super long lines are unreadable, so I want to span it across multiple lines. Tried to do it, but I don't know what to do with the .split(), so... is it possible? If yes, show me how, and the parts of the program that will need changing if I replace the string with a list.


r/inventwithpython • • Aug 11 '15

ch.2 Gauss addition problem code question

1 Upvotes
total=0
for num in range(101):
    total=total+num
print(total)

I'm having a hard time wrapping my head around the equation part. I 'get it' on a surface level, glancing at it, makes sense, but if I had to point out each step involved at arriving to 5050 using this one equation I can't visualize how it works.

There are 3 keywords 'total'. The very first one at the top is just a starting point for the equation. It also happens to be a global variable? which is why it is okay to have the same word 'total' on both sides of the equation because the left-side total is a local one (inside the for function) and the right-side 'total' is different because it is the global one, defined earlier at the top, am I right?

I just don't understand how this is enough for python to know to sum all those iterations together into one number, especially with so many totals. I tried substituting the lest-side 'total' with 'theSum' as a variable name, and printing that variable, but it only gives me 100 instead of 5050:

total=0
for num in range(101):
    theSum=total+num
print(theSum)

Why isn't it iterated a 100 times and added up like in the previous example? I just can't see the first several steps the program is doing and differentiating between different total variables and remembering to which to add again and which total reuse in the next iteration.


r/inventwithpython • • Jul 29 '15

line 47 pyperclip wrong type returned - caesar

1 Upvotes

running the ceaser cypher in the book I get the correct translated output but pyperclip errors that it has a <TYPEERROR>. this wouldn't be noticed from standard console but is reported in ipython

I am on windows 7 64bit with python 3.4.3 λ python --version Python 3.4.3 :: Anaconda 2.3.0 (64-bit) C:\Users\sayth\OneDrive\repos\cipher

This is the error

In [1]: pwd Out[1]: 'C:\Users\sayth\OneDrive\repos\cipher'

In [2]: %run cipher.py GUVF VF ZL FRPERG ZRFFNTR

ArgumentError Traceback (most recent call last) C:\Users\sayth\OneDrive\repos\cipher\cipher.py in <module>() 33 print(translated) 34 ---> 35 pyperclip.copy(translated)

C:\Users\sayth\OneDrive\repos\cipher\pyperclip.py in _copyWindows(text) 45 if not isinstance(text, str): 46 text = text.decode('mbcs') ---> 47 d.user32.OpenClipboard(None) 48 d.user32.EmptyClipboard() 49 hCd = d.kernel32.GlobalAlloc(GMEM_DDESHARE, len(text.encode('utf-16-le')) + 2)

ArgumentError: argument 1: <class 'TypeError'>: wrong type


r/inventwithpython • • Jul 28 '15

I can't install pyperclip - get "no commands supplied error" (MAC)

1 Upvotes

I'm unable to install pyperclip.py... I've downloaded the folder and navigated to it in terminal. There I tried "python setup.py" and received the error message: "error: commands supplied".

I've researched it a bit and have heard things like my python "path", but I can't figure it out...

Please help!


r/inventwithpython • • Jul 24 '15

Chapter 7 debug doesn't work for me

1 Upvotes

The tutorial says that when you reach line 5 you should click step, and that "The debugger waits until the player enters a response into the program". For me, it opens up pyshell and when I go to the python 3.4.3 shell and type a number and press enter nothing happens. When I click "Out" to get out of pyshell, every button on the debug goes grey and I have to restart. If i try to click "over" the same thing happens. I've been trying to solve this for about 1/2 hour and it's really starting to drive me crazy. Please help.

Edit: Just to clarify, I have read the other issue someone else had with chapter 7 on this subreddit, but I did not understand the answer given


r/inventwithpython • • Jul 23 '15

chapter 5 dictionary method

1 Upvotes

what's different between get() and setdefault() ?


r/inventwithpython • • Jul 21 '15

Chapter 4 Issue

1 Upvotes

So, I'm trying to get through Chapter 4 and for some reason, on line 19 I get a TypeError: unorderable types: int() < str()

I don't understand what I'm doing wrong, I did a diff and it seems to be the same.

http://pastebin.com/9HH0wuP9


r/inventwithpython • • Jul 19 '15

IDLE does not load at all

1 Upvotes

I'm completely new to python and will use Sweigart's awesome book. I can't really start yet because I can't get the IDLE program to load. I try loading IDLE in two ways:

  1. going through the windows start menu (screenshot below)

  2. double clicking pythonw.exe (second screenshot below) When I try either of these, absolutely nothing happens. I right clicked/properties the IDLE icon from the first image and it says the target is: C:\Python\pythonw.exe "C:\Python\Lib\idlelib\idle.pyw"

I'm running windows 7, 64-bit. I downloaded/installed "python-3.5.0b3-amd64". The interactive shell works fine when I double click python.exe (a MS-DOS looking window pops up with the >>> prompt). I tried uninstall/re-install. I tried running the 32-bit. I hit CTRL-ALT-DEL and checked if the pythonw.exe process is running in the background. I googled for solutions, but can't find anything.

http://imgur.com/XPhd5WG http://imgur.com/JqIlArC

Thanks!!!!!!!!!!!!!!


r/inventwithpython • • Jul 16 '15

Chapter 4 - difficult question with list length and for loops.

1 Upvotes

I'm trying to write a program, that asks the user for input. Whatever they put is stored into the list as a new item, and each item is then printed on its own line. It then asks you again to add another value. This goes on forever, adding more and more values, and having the entire list printed back to you each time.

The complicated part is that I want to limit how long the list can get. Once the list gets beyond a certain length, I want the first value to be deleted, and only the most recent few lines to be printed. the length of the list is specified at the start. So far this works.

the problem happens once the list reaches this limit. for some reason, it just stops printing the list back. I don't know what's going on.

Pastebin: http://pastebin.com/Q6bGJjG2

Thanks for any help!


r/inventwithpython • • Jul 09 '15

Having trouble importing modules when running from Command Line

1 Upvotes

I'm at the end of chapter 6 where I'm making the password manager... When I run 'import pyperclip' with IDLE everything works fine, but when I try to access the .py file with the 'import pyperclip' line from the terminal I always get this message: "ImportError: No module named pyperclip" no idea why it works when using it with IDLE, but not with the command line, any solutions? edit: it's only with 3rd party modules.


r/inventwithpython • • Jul 08 '15

Too many playAgains in chapter 9

1 Upvotes

I just finished chapter 9 and i was confused about why are we asking the player to play 2 times, the first was at line 100 when defining play again, and the second was at line 139


r/inventwithpython • • Jul 07 '15

Automate the Boring Stuff Chapter 7 Practice Project - How to Use a Variable in Creating a Regex Pattern Object

1 Upvotes

I've been going through all the practice projects and have been loving all of them so far, but I've stumbled into a little roadblock. I was able to replicate the basic strip() string method to remove all whitespace characters at the beginning or end of the string. However, I am currently unable to create a Regex pattern object that uses an argument as part of the string. Many of my previous attempts have resulted in a SyntaxError: invalid syntax. I've tried Googling and searching on StackOverflow, but to no avail.

Could someone please provide some guidance on how to solve this problem? If I need to provide additional information, please let me know because this is the first time I have asked a coding related question online. Thanks!

My Current Solution

def newStrip(text, removedChar = ' '):
    whitespaceRegex = re.compile(r'^\s+|\s+$)
    otherRegex = re.compile(^removedChar|removedChar) #how do I use variables
    if removedChar == ' ':
        print(whitespaceRegex.sub('', text))
    else:
        print(otherRegex.sub('', text))

Question for Reference

Write a function that takes a string and does the same thing as the strip() string method. If no other arguments are passed other than the string to strip, then whitespace characters will be removed from the beginning and end of the string. Otherwise, the characters specified in the second argument to the function will be removed from the string.


r/inventwithpython • • Jul 06 '15

Appendix B: Running programs on Terminal on Mac

1 Upvotes

I keep getting the message: env: python3\r: No such file or directory whenever i try to run a script from the command line. i checked if /usr/bin/env exists and it does, so i'm not really sure what's wrong...


r/inventwithpython • • Jul 05 '15

Ch 6 Password Locker project

1 Upvotes

I don't understand how to run pw.py with the required arguments from the command line. Since this is the first time we do that in the book it would be helpful to have more clear instructions. There are several references to Appendix B but there the word "argument" is not even mentioned.


r/inventwithpython • • Jul 04 '15

Collatz Try and Except Clauses

1 Upvotes

I'm wondering if this is right. The try and except clause will run through smoothly if I type in a non-integer. But since I ran the program and typed in a non-integer the except clause will continue the code and run into an error with the while clause because a non-integer was typed in the first place. Is this the way the exercise was intended? Thanks for any help.

http://pastebin.com/b4fPehRY


r/inventwithpython • • Jul 03 '15

Collatz Sequence. Need some help.

1 Upvotes

Ch. 3 of the book on the Collatz Sequence. The program is supposed to stop once the collatz function returns one. My program returns "1" but continues another past that, for example: 3, 16, 8, 4, 2, 1, 4 Could someone look over my code?

http://pastebin.com/g0TseArG


r/inventwithpython • • Jun 27 '15

Please help me with running python programs from the command line.

1 Upvotes

Hi guys,

Sorry if this is a silly question. I've followed Appendix B and added C:\Python34\MyPythonScripts to the path.

I added a shebang line to the first hello world program (hello.py). And made a batch file for it and placed it in the same folder. I tested them by running those and they worked fine.

Then I tried going to start and run and tried typing in hello and it returns "Windows can not find 'hello'. Make sure you typed the name correctly and try again"

When i try to run it from CMD, I get "'hello' is not recognized as an internal or external command, operable program or batch file."

When I type "python" in cmd it launches python. When I then type 'hello', it returns "Traceback (most recent call last): File "<stdin>" line 1 in <module> nameError: 'hello' is not defined "

I'm on chapter 11 and was trying to run the mapIt program.

Also I installed the latest python 3.4.3. which also has the add python to path.

Please help! I'm a newbie and aside from Al's books, have no computing expertise.


r/inventwithpython • • Dec 09 '14

Small discrepancy in Chapter 7: Tetromino

1 Upvotes

I don't know if this belongs here, but I found a discrepancy between the html version of Chapter 7 and the downloadable tetromino.py found on the same page. In the .py source "PIECES" is used as the constant to hold all the shapes, but in the html chapter, "SHAPES" is used, which lead me to some confusion for a few minutes when following along with the getNewPiece() function.

Absolutely love this book and will be buying all of the books for my collection!


r/inventwithpython • • Feb 19 '14

Unable to open IDLE but can run the python.exe file just fine

1 Upvotes

error msg reads IDLE's subprocess didn't make connection. Can I just use the exe file or is it important to have access to the IDLE gui?


r/inventwithpython • • Feb 15 '14

New to Python problems

1 Upvotes

I installed Python 3.3 on my macbook pro (10.6.8) last week and was following the tutorials in the INVENT WITH PYTHON book. All was going well until I used IDLE to run the hello.py script. When I OPEN the file using IDLE mfile -open it appears for a second and then I get the IDLE QUIT UNEXPECTEDLY window. I installed 3.3 on another macbook and I get the same exact problem. I thought I'd give it another try today. I reinstalled python several times and now in addition to the original problem I have found I can't open a new file in IDLE. As soon as I do, I get a white window without text and the IDLE freezes. I have to force quit to close it. Any advice would be appreciated.


r/inventwithpython • • Jan 25 '14

I got error: Unable to open file 'beeps.wav'

1 Upvotes

I am following instruction in Making Game about playing sound. I downloaded "beeps.wav" file and put it into my working directory. Then I run ipython and tried to play the sound:

import pygame from pygame.locals import *

soundObj = pygame.mixer.Sound('beeps.wav') soundObj.play()

Then I got output:

error Traceback (most recent call last) <ipython-input-7-370652564b32> in <module>() ----> 1 soundObj = pygame.mixer.Sound('beeps.wav')

error: Unable to open file 'beeps.wav'

Can you please advise what did I do wrong? Thanks


r/inventwithpython • • Jan 18 '14

problems with K_SPACE

1 Upvotes

I seem to be having problems with K_SPACE I tried making searching for the event a def just to trouble shoot but it only worked for QUIT. Can anyone tell me why I am not see a print when I hit the space bar

import pygame, sys from pygame.locals import *

pygame.init() DISPLAYSURF = pygame.display.set_mode((400,300)) pygame.display.set_caption('Hello World!')

WHITE = (255, 255, 255) GREEN = (0, 255, 0) BLUE = (0, 0, 128)

fontObj = pygame.font.Font('freesansbold.ttf', 32) textSurfaceObj = fontObj.render ('Hello World!', True, GREEN, BLUE) textRectObj = textSurfaceObj.get_rect() textRectObj.center = (200, 150)

def main(): while True: # main game loop DISPLAYSURF.fill(WHITE) # draw the window DISPLAYSURF.blit(textSurfaceObj,textRectObj) checkForQuit() checkForSpaceBar() pygame.display.update()

def checkForQuit(): for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit()

def checkForSpaceBar(): for event in pygame.event.get(): if event.type == K_SPACE and KEYDOWN: print event.key

if name == 'main': main()