This is a program from “Automate the boring stuff” that really turned on the light on a lot of concepts for I believe Python 3.x specific syntax, and just in general (stuff also in 2.7).
I’ll run through this quick as getting on the lab was not easy tonight, but I want to nail these concepts down quick.
I will adjust some of the code above to see what effects it has, add lines, and play explain what I’ve learned from reading today and playing with the code.
The things that I have learned today through making this program (with errors)
First thing is the green # are not read by Python and are for comments, can be attached to any lines, I put one on every line but its good to know you can mark lines or put it as a header for different code to remind you exactly what a long program does.
The second, is I made some mistakes by thinking everything needed a , after it, for example I was hung up on this for about 10-15 minutes trying not to google it:
I am not sure if it was the 2.7 course / syntax, but I thought commas were needed to end any type string or function, but upon looking at the book I didn’t see it, and once I removed this allowed the program to continue to function.
I had another issue with Line 12, as I for some reason wanted to (str(int(myAge))) +1 where the first bracket enclosing str (string) was not needed, I am not sure why I thought that was needed there, though I feel like that might be a 2.7 thing (I may be wrong).
This is because len / str / int are functions which must be encased with () like print!
To demonstrate this I’ll bring up IDLE Shell to quickly demonstrate some thing that work, and that don’t work so well, one thing that surprised me was thing:
The times “Multiplier” operator will work on Strings to Print them multiple times, and of course the Assignment Operator = finally made Gooby = 5 Integer, just something I found after my last post I wanted to post here.
Moving onto a more important point though, is the input / output concept:
- something = input()
- User inputs something that then becomes the variable for ‘something’
- I can multiply ‘Gooby’ 10 times, but not without the Apostrophes!
- You now have a variable to build a conversation input from someone else
My curiosity killed the Cat question of the day – Can I put ANY word in their for input as long as it matches across the program?
**Keep in mind, variables are absolutely case sensitive, and so is their value.**
^^ Big concept there, remember that.
So I’m going to switch some things up, and run the same program, and see what happens:
Success! BoeTiE represents that name variable and ShoeStr1ng with a 1 in it worked perfectly as variables to assign instead of myAge and myName, the only thing that couldn’t be put there are literals of course like 10 = input() or something.
An awesome Noob Tip for Visual Studio Code, once you change a variable input somewhere in the code, it will highlight all old versions of that variable, which you can then right click and do “ChangeAllOccurences” command.
This won’t change them all to the new variable name, but it will allow you to delete and re-type them all at once (and will even offer a little auto-type to assist!)
Another thing I don’t know if I’ve shown, but you must keep the quotations consistent:
Bit hard to see but a double quote on the left and a single quote on the right, it will just kick out an error, Python likes when things match (syntax, indentation, etc).
I’m going to have to end it here, but want this posted for practice!
I know its a tiny post, but within it I’ve learned:
- str / int / len are functions just like print which request () for each one
- Commas are not needed, I forgot I left one in, 10 more minutes of tshooting!
- You can practice in IDLE for just very quick practice as well!
Using the concepts in this article you can imagine a lot of conversations, even in IDLE or the Native Console of your OS, and write the same program I wrote in Visual Studio!
That is pretty cool, I am going to spend some time practicing that and playing with the math concepts shown (like what will and won’t work together using Integers and Floating Integers) as I did not totally cover that – There is too much to entirely cover 🙂
Later fellow code warriors!