Gettin' User Input and Comments

Comments are really useful and user input is useful for calculators

Homework Answers from last time (ignore if you didn't read last lesson)

Tbh , you can do it a lot of ways , you don't have to have as many lines , but I did it , because this is supposed to be a beginner frendly tutorial.

Here's a way to make the function 2 lines without lambda :

Fun fact : My username before Arstotzka , was Razbi . This fact has nothing to do with the homework answers.

Gettin' User Input

What it is, why would I use it?

User Input is one of the ways a program can be interactive by requesting bits of data from users in the form of numbers/strings/etc . User Input can be really useful when making calculators , when making huge scripts and you wanna run only a part , or switching mode of a script or just giving it the full path to a file.

How to implement it?

Well the command is pretty easy input(<text displayed that asks for a thing>) .

Writting input() without anything in parenthesis will just stop the program 'till you press enter while it's selected and it can be useful when combined with pyinstaller which makes your file into 1 standalone .exe

Here's a program that will ask for your credit card number , of course this is fake ,because you also need expiration date and the pin unfortunatly. Anyways jokes aside , when you run this , the program will display the message in console and you will write just after it:

As you can see the user will have to type where my 'typing cursor' was , what happens if you write anything? Well if you press enter it will close the program , that's for sure , since that's the only line and it stops the program to do anything till you type something there or just press Enter.

Where Does the Input Get Saved?

Well , it doesn't , to get it saved you have to do something like variable = input("message : ")

Now that it's saved like a variable , you can do a lotta stuff with it like passing it in a function or in some loops

Fun fact: Here's a game I made using only print(), input(), IF statements [we gonna talk about that in next lesson hopefully] and a bit of pyGame for sound effects. https://glorytoarstotzka.itch.io/mercenary-simulator Note that it's a standalone .exe and it's just a text game and I did it some time ago . Also I'll make part 2 if people will ask for it.

Comments, the most useful part of writting a script

Script means any file with code in it , it doesn't mean malicious code or bots necesarily. It's just a fancy way of me saying 'file with code' or if we are in a python tutorial 'python file'

[If I'm wrong feel free to tell me on discord]

What it is , why SO USEFUL?

Imagine that you joined discord for help and you have a problem with your script , a messed up function and someone there sends you a new example of function . In case his function won't work you are gonna probably either make a clone file with only function changed or just save your 'not working function ' in a .txt or anywhere else. Well you don't have it to save anywhere outside the script since you can use THE POWE' OF comments.

Also let's say your script is gonna be used by a lot of people and you don't have time for them to ask that many questions , so you include comments to tell people what the script does or what parts of it do. Also comments can help even you in case you forgot that the script even existed so you forgot what it does.

Are you gonna tell me how to use it?

This ain't a question for you , it's a question you probably gonna ask and you already know the answer.

It's really easy : just write # before the code you want it to be 'commented out' and when you run the script it won't run . Example :

A scenario when commenting saved our drive

The command pass is like a placeholder that you put inside of functions that you don't have a use yet or you wanna specify in advance the name.

As you can see , the commented text gets grayed out and won't run by program , in this case saving our drive from formating . Of course the function above didn't do anything and if you don't believe me that comments make pieces of code not run , replace the name of the function that's commented with input('message') and you will see that it won't stop the program to ask for input.

Is there a magical way to comment out a LOT of lines at once?

Yeah , you guessed it perfectly right , to comment any number of lines just write '''[aka triple quotes] at the start of what you wanna comment and again'''where you want it to stop. Example :

Let's say that we just want the script to run 'Do you like turtles?' instead of all the script above , but you don't wanna rewrite them if you wanna run them. You will have to do something like that

the ''' thing is used for docstings or something [entire file for documentation inside modules] [Also RazbiTh3Player is actually my twitch]

Practice REALLY Makes Perfect , Believe Me!

For your homework today , I want you to remind yourself of the knowledge learnt at the 'Function Parameters' lesson and combine that with the today lesson. Your task is to make a function that will print out whatever you input in , and the thing you will have to input will be user input . Also something optional : try to do it again , without a function , and only in a single line

We'll talk about the answers at the start of the next lesson

Last updated

Was this helpful?