/r/inventwithpython
Discuss Al Sweigart's programming books from InventWithPython.com
/r/inventwithpython
Hello Community.
I am currently working on going over all the chapters in the book. Would anyone like me to share the notes for each chapter? it will also include code snippets and some additional information.
Hello. I took that code from practice questions in chapter 2 of "Automate the Boring Stuff with Python Practical Programming" (Al Sweigart). I wrote exactly how it should be, but the MU says that I have a mistake in line one.
Can someone explain what is wrong?
if spam == 1:
print('Hello')
elif spam == 2:
print('Howdy')
else:
print('Greetings')
If you want to learn to code, I've released 1,000 free sign ups for my course following my Automate the Boring Stuff with Python book (each has 1,000 sign ups, use the other one if one is sold out):
https://udemy.com/course/automate/?couponCode=JUL2024FREE
If you are reading this after the sign ups are used up, you can always find the first 15 of the course's 50 videos are free on YouTube if you want to preview them. YOU CAN ALSO WATCH THE VIDEOS WITHOUT SIGNING UP FOR THE COURSE. All of the videos on the course webpage have "preview" turned on. Scroll down to find and click "Expand All Sections" and then click the preview link. You won't have access to the forums and other materials, but you can watch the videos.
NOTE: Be sure to BUY the course for $0, and not sign up for Udemy's subscription plan. The subscription plan is free for the first seven days and then they charge you. It's selected by default. If you are on a laptop and can't click the BUY checkbox, try shrinking the browser window. Some have reported it works in mobile view.
I'm also working on another Udemy course that follows my recent book "Beyond the Basic Stuff with Python". So far I have the first 15 of the planned 56 videos done. You can watch them for free on YouTube.
Frequently Asked Questions: (read this before posting questions)
hello everyone, i have a problem with my code. In chapter 5, its explained how to do a game called dragon realm. I wrote the code and when i tried to run it, but no text showed up. I tried copypasting the code and running it but it didnt work either. I dont know if im supposed to do something and i need help. i'm using python 3.12.4.
this is what shows up when i try to run the code.
please help me, ive did all the other games introduced before this one and something like this never happened.
(This photo is not me irl btw, found it when i googled "waving picture")
Anyways, just me saying hello to the community, i doubt anyone is gonna care anyways. :D
beginner programmer here, so I'm having trouble running a simple code that displays "Hello World" on another window when the program running successfully, ive compared it to the book and still nothing i am running it through Pycharm book is Invent Your Own Computer Games With Python (Fig.17-1)pg.257
Hello!
I want to learn Python by using either the Invent Your Own Computer Games or the Making Games with Python and Pygame book.
It's not the first time I've learned Python, I learned the very basics in 2014 and 2021. But I've got more experience in Unity and C#. I finished a Unity 2D course in 2022 which covered C#, and so I'd say that I'm confident in the basics of that language (at least for Unity), and I've been practicing that fairly frequently.
Based on that, would I be better off going with the Invent Your Own Computer Games book or should I just jump straight to the Making Games with Python and Pygame book?
Hi! I've been using EZsheets etc for automating a twice annual reporting process with Google sheets, mail, and drive... which collides into the 180 day expiration of Google authorization keys.
This go around, I've found my efforts to reauthorize -- including creating a new "app" -- foiled in the end by the unhelpful error message:
google.auth.exceptions.RefreshError: ('invalid_grant: Bad Request', {'error': 'invalid_grant', 'error_description': 'Bad Request'})
Any advice on what I can try to get this working? I promise to run the app monthly henceforth to keep my access current. Thanks!
Hi, Al. Reading Automate the Boring Stuff. I've tried linking to https://author.com/vampire2, among others. My browser returns a message saying it's unable to find the server. Is there a problem with the site? Thanks.
If you want to learn to code, I've released 2,000 free sign ups for my course following my Automate the Boring Stuff with Python book (each has 1,000 sign ups, use the other one if one is sold out):
https://udemy.com/course/automate/?couponCode=NOV2023FREE
https://udemy.com/course/automate/?couponCode=NOV2023FREE2
If you are reading this after the sign ups are used up, you can always find the first 15 of the course's 50 videos are free on YouTube if you want to preview them. YOU CAN ALSO WATCH THE VIDEOS WITHOUT SIGNING UP FOR THE COURSE. All of the videos on the course webpage have "preview" turned on. Scroll down to find and click "Expand All Sections" and then click the preview link. You won't have access to the forums and other materials, but you can watch the videos.
NOTE: Be sure to BUY the course for $0, and not sign up for Udemy's subscription plan. The subscription plan is free for the first seven days and then they charge you. It's selected by default. If you are on a laptop and can't click the BUY checkbox, try shrinking the browser window. Some have reported it works in mobile view.
Some people in India and South Africa get a "The coupon has exceeded it's maximum possible redemptions" error message. Udemy advises that you contact their support if you have difficulty applying coupon codes, so click here to go to the contact form. If you have a VPN service, try to sign up from a North American or European proxy. Please post in the comments if you're having trouble signing up and what country you're in.
I'm also working on another Udemy course that follows my recent book "Beyond the Basic Stuff with Python". So far I have the first 15 of the planned 56 videos done. You can watch them for free on YouTube.
Frequently Asked Questions: (read this before posting questions)
If you want to learn to code, I've released 2,000 free sign ups for my course following my Automate the Boring Stuff with Python book (each has 1,000 sign ups, use the other one if one is sold out):
https://udemy.com/course/automate/?couponCode=OCT2023FREE
https://udemy.com/course/automate/?couponCode=OCT2023FREE2
If you are reading this after the sign ups are used up, you can always find the first 15 of the course's 50 videos are free on YouTube if you want to preview them. YOU CAN ALSO WATCH THE VIDEOS WITHOUT SIGNING UP FOR THE COURSE. All of the videos on the course webpage have "preview" turned on. Scroll down to find and click "Expand All Sections" and then click the preview link. You won't have access to the forums and other materials, but you can watch the videos.
NOTE: Be sure to BUY the course for $0, and not sign up for Udemy's subscription plan. The subscription plan is free for the first seven days and then they charge you. It's selected by default. If you are on a laptop and can't click the BUY checkbox, try shrinking the browser window. Some have reported it works in mobile view.
Some people in India and South Africa get a "The coupon has exceeded it's maximum possible redemptions" error message. Udemy advises that you contact their support if you have difficulty applying coupon codes, so click here to go to the contact form. If you have a VPN service, try to sign up from a North American or European proxy. Please post in the comments if you're having trouble signing up and what country you're in.
I'm also working on another Udemy course that follows my recent book "Beyond the Basic Stuff with Python". So far I have the first 15 of the planned 56 videos done. You can watch them for free on YouTube.
Frequently Asked Questions: (read this before posting questions)
I'm going through Automate the boring stuff book and instead of downloading the comic images for the exercise project, I decided to try scraping Sotheby auction site. I've written a script that goes through all the pages on https://sealed.sothebys.com (that have listings of auctioned items), collecting all the items' url, then open each url and download the 1st image of each item.
There are 2 specific points in the execution where the HTTP2 protocol ERROR (this site is unsecure) bug could happen:
I've isolated just the code for those 2 parts for debugging
from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.get('https://sealed.sothebys.com')
time.sleep(5)
# click on Next button
n = 0
while True:
next_button = browser.find_element('css selector', 'button.sc-dd495492-1:nth-child(5)')
if not next_button.is_enabled():
print('End of current item on auction catalogue.')
break
browser.execute_script("arguments[0].click()", next_button)
n += 1
print(n)
time.sleep(2)
When this works, it outputs in order: 1 2 'End of current item on auction catalogue.'
(there are only 3 listings pages at this moment)
When it doesn't work, it outputs: 1 <Error message
I have to remove the https:// part and replace '.' in the url with '_' to avoid issues with posting
from selenium import webdriver
import time
new_items = ['sealed_sothebys_com/YF23/auction',
'google_com',
'sealed_sothebys_com/BC23/auction',
'sealed_sothebys_com/michael-jordan/auction',
'google_com',
'sealed_sothebys_com/the-black-rose/auction',
]
for url in new_items:
browser.get(url)
time.sleep(2)
try:
item_name_ele = browser.find_element('tag name', 'h3')
except:
print('Error')
60-70% of the time, the error starts happening with the 2nd url and every url afterwards, 30-39% of the time, the first few urls will have no problems (the number of the working urls varies, could be 3 ,5, 10, more than 10 ..) and only 1% of the time or less that 100% of the urls work. Once the error happens with 1 url, all the urls after it will have the error as well. I've inserted 2 google links in the list to test, and they still work fine even if the error happens with the sothebys url right before them.
browser.delete_all_cookies()
then browser.refresh()
when the code encounters error finding element on page. Didn't work. (I did this because if I manually do this on the page opened with selenium: delete cookies and refresh -> the error will disappear, but it will appear again when I click on any link on that page)
from selenium.webdriver.chrome.options import Options as ChromeOptions
options = ChromeOptions()
# cloud_options = {}
options.accept_insecure_certs = True
options.add_argument('--ignore-ssl-errors=yes')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--allow-insecure-localhost')
options.add_argument('--allow-running-insecure-content')
# options.set_capability('cloud:options', cloud_options)
browser = webdriver.Chrome(options=options)
The above block of code added before browser.get('https://sealed.sothebys.com')
does absolutely nothing. How do I make my code work? I really really appreaciate any help and insights
If you want to learn to code, I've released 2,000 free sign ups for my course following my Automate the Boring Stuff with Python book (each has 1,000 sign ups, use the other one if one is sold out):
https:// udemy. com/course/automate/?couponCode=SEP2023FREE
https://udemy.com/course/automate/?couponCode=SEP2023FREE2
If you are reading this after the sign ups are used up, you can always find the first 15 of the course's 50 videos are free on YouTube if you want to preview them. YOU CAN ALSO WATCH THE VIDEOS WITHOUT SIGNING UP FOR THE COURSE. All of the videos on the course webpage have "preview" turned on. Scroll down to find and click "Expand All Sections" and then click the preview link. You won't have access to the forums and other materials, but you can watch the videos.
NOTE: Be sure to BUY the course for $0, and not sign up for Udemy's subscription plan. The subscription plan is free for the first seven days and then they charge you. It's selected by default. If you are on a laptop and can't click the BUY checkbox, try shrinking the browser window. Some have reported it works in mobile view.
Some people in India and South Africa get a "The coupon has exceeded it's maximum possible redemptions" error message. Udemy advises that you contact their support if you have difficulty applying coupon codes, so click here to go to the contact form. If you have a VPN service, try to sign up from a North American or European proxy. Please post in the comments if you're having trouble signing up and what country you're in.
I'm also working on another Udemy course that follows my recent book "Beyond the Basic Stuff with Python". So far I have the first 15 of the planned 56 videos done. You can watch them for free on YouTube.
Frequently Asked Questions: (read this before posting questions)
LocateonScreen() is not working. Whenever I type and press it, the result is none. Can somebody please help?
currently i practice on one dumpy ETL based project.
in this project i used tools such as python, sql server, power BI application.
my doubts are, can i create staging area database and data warehouse database are else company create if incase company create how to find it both of them.
and after transfer the data into DW what will do next please share any one our experience like real sceneries. it will help full for my career and skills.
Hi sir i'm a 8th grade student from india .I just brought your 'automate the boring stuff with python' and I have doubt from it that: Print('what is your age?')#ask for their age myAge=input() Print('you will be '+str(int(myAge)+1)+'in a year.') Page no.17 Here I converted myAge into int() then what's the use of str() here?
I'm having trouble getting this to work. I've literally downloaded the source files from the website and run that, but I get some version of an error where the x or y coordinate is negative. Any idea why?
File "C:\Python310\lib\site-packages\bext\__init__.py", line 521, in _goto_win32_api
raise BextException('x coordinate is negative')
bext.BextException: x coordinate is negative
On page 108:
print('Chance of streak: %s%%' % (numberOfStreaks / 100))
should be:
print('Chance of streak: %s%%' % (numberOfStreaks / 10000))
The full project, showing the percentage found with the simulation and with probability:
import random
numberOfStreaks = 0
for experimentNumber in range(10000):
# Code that creates a list of 100 'heads' or 'tails' values.
flips = []
for i in range(100):
flips.append(random.randint(0, 1))
# Code that checks if there is a streak of 6 heads or tails in a row.
count = 1
for i in range(1, len(flips)):
if flips[i] == flips[i - 1]:
count += 1
else:
count = 1
if count % 6 == 0:
numberOfStreaks += 1
print('Chance of streak (SIMULATION): %s%%' % (numberOfStreaks / 10000))
print('Chance of streak (MATH): %s%%' % ((1/2)**6 * 100))
On page 95:
In the first example, the list value that eggs ends up with is the same
list value it started with.
Should be:
In the previous example, the list value that eggs ends up with is the same
list value it started with.
In the middle of the page it's written:
The first half is factorial(number - 1). This involves calculating number - 1
and making a recursive function, causing a new frame object to be pushed
to the call stack. This happens before the recursive call is made.
I think this should be something like this:
The first half is factorial(number - 1). This involves calculating number - 1
and making a recursive call to this function, causing a new frame object to be pushed
to the call stack.
Am I misunderstanding it?
I'm doing projects in Automate the boring stuff. This particular project asks me to Write a program to find out how often a streak of six heads or a streak of six tails comes up in a randomly generated list of heads and tails.
The #comments are hint by the author. This program I created ran just fine but the chance of streak is surprisingly high to me: ~152% - 153%. So I'm wondering if I got the logic right? Any help will be greatly appreciated. Thank you
import random
def flip():
random_num = random.randint(0, 1)
if random_num == 1:
return "H"
else:
return 'T'
numberOfStreaks = 0 #author wrote this line
for experimentNumber in range(10000): #author wrote this line
# Code that creates a list of 100 'heads' or 'tails' values.
head_tail_str = ""
side = flip()
head_tail_str += side
for _ in range(99):
prev_side = side
side = flip()
if side != prev_side:
head_tail_str += f",{side}"
else:
head_tail_str += side
# Code that checks if there is a streak of 6 heads or tails in a row.
head_tail_lst = head_tail_str.split(',')
for item in head_tail_lst:
if len(item) >= 6:
numberOfStreaks += (int(len(item)) // 6)
print('Chance of streak: %s%%' % (numberOfStreaks / 100)) #author wrote this line
p/s: I've been learning Python for 1 month
EDIT: The sign ups are all used up. Remember that the Free Preview is enabled for all videos, so you can still watch them all on the course home page. And the first 15 videos are also on YouTube.
If you want to learn to code, I've released 2,000 free sign ups for my course following my Automate the Boring Stuff with Python book (each has 1,000 sign ups, use the other one if one is sold out):
https ://udemy .com/course/automate/?couponCode=APR2023FREE
https ://udemy .com/course/automate/?couponCode=APR2023FREE2
Udemy has changed their promo code and severely limited the number of sign ups I can provide each month, so only sign up if you are reasonably certain you can eventually finish the course. The first 15 of the course's 50 videos are free on YouTube if you want to preview them.
YOU CAN ALSO WATCH THE VIDEOS WITHOUT SIGNING UP FOR THE COURSE. All of the videos on the course webpage have "preview" turned on. Scroll down to find and click "Expand All Sections" and then click the preview link. You won't have access to the forums and other materials, but you can watch the videos.
NOTE: Be sure to BUY the course for $0, and not sign up for Udemy's subscription plan. The subscription plan is free for the first seven days and then they charge you. It's selected by default. If you are on a laptop and can't click the BUY checkbox, try shrinking the browser window. Some have reported it works in mobile view.
Sometimes it takes an hour or so for the code to become active just after I create it, so if it doesn't work, go ahead and try again a while later.
Some people in India and South Africa get a "The coupon has exceeded it's maximum possible redemptions" error message. Udemy advises that you contact their support if you have difficulty applying coupon codes, so click here to go to the contact form. If you have a VPN service, try to sign up from a North American or European proxy.
I'm also working on another Udemy course that follows my recent book "Beyond the Basic Stuff with Python". So far I have the first 15 of the planned 56 videos done. You can watch them for free on YouTube.
Side note: My latest book, Python Programming Exercises Gently Explained is a set of 42 programming exercises for beginners for free or as a 99 cent ebook.
Frequently Asked Questions: (read this before posting questions)
Estoy aprendiendo a programar. Empecé leyendo un libro de introducción a la lógica que usa pseudocódigo y un libro de como escribir mejor código de programación. Estaba impaciente y quería empezar a programar y encontré el libro "Inventa tus propios juegos de computadora con Python 3ra edición" que es una versión traducida del libro de quien creo hizo este blog.
Gracias a los autores del libro y a los que lo tradujeron a español.
Hice mi versión del primer juego del libro para practicar XD.
Los saltos de línea (\n), las cadenas multi-líneas (\) y convertir strings a mayúscula ( upper() ) lo busque en internet para saber como se hace.
Cualquier observación para mejorar es bienvenida.
Saludos.
/*-------------------------------------------*/
I am learning to program. I started by reading an introductory book on logic that uses pseudocode and a book on how to write better programming code. I was impatient and wanted to start programming and I found the book "Invent your own computer games with Python 3rd edition" which is a translated version of the book by who I think made this blog.
Thanks to the authors of the book and to those who translated it into Spanish.
I made my version of the first game of the book to practice XD.
Line breaks (\n), multi-line strings (\) and converting strings to uppercase ( upper() ) look it up on the internet to find out how to do it.
Any observation to improve is welcome.
Greetings.
(Use google translate. Apologies)
This may or may not be the best place to ask this question, but I recently installed Python 3.11 (latest) and using pip install automateboringstuff2ndedition
gives numerous warnings of outdated installer dependencies. It also seems to not like the version of pillow that is outlined from automateboringstuff2ndedition 1.0.3. As an alternative I installed Pillow 9.4.0, but when I reran the 2nd edition pip install, I got a notice that it uninstalled Pillow 9.4.0. As a test then, I went to IDLE for 3.11 and import PIL worked just fine (no errors). Am I missing something? Should the 2nd edition pip installer be used for 3.11?
Pillow error:
RuntimeWarning: Pillow 9.0.0 does not support Python 3.11 and does not provide prebuilt Windows binaries. We do not recommend building from source on Windows.
Deprecation error (note I found this on another install as well, so it may not be 100% accurate to the 2nd edition pip install):
DEPRECATION: future is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559 Running setup.py install for future ... done DEPRECATION: pefile is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559
I've gone through the documentation, and don't see how to do this...
Using .inputMenu, is there a way to print (to the console) some sort of prompt to indicate where the user should enter the data? For example:
Make a selection:
1. choice1
2. choice2
>>>
The ">>>" is where the user should input his/her choice.
Thanks.
For the book ( Invent Your Own Computer Games ), Should I pay for the source code, or its free,
if it's free plz I need the URL for the source code
best