Python For Beginners – Practice Functions

2Shares
Reading Time: 3 minutes

Python For Beginners – Learn how functions work the fun way with this Top Secret Password Reader

For those that are struggling with understanding how functions are called or work within python, then this script is for you. Some people are lucky enough to learn coding by just reading a book or watching a video. If you’re like me, it’s easier to grasp new concepts or ideas a lot faster with examples and hands-on experience.

A function is a block of code that runs only when it is called. You can pass data(parameters), into a function. A function can return data back as a result. A function in python is defined using the def keyword.

Example(Creating a Function):

def test_function():
  print("Hello there")

Then to call the function created, type out the function name with the parenthesis.

Example(Calling the Function):

def test_function():
  print("Hello there")

test_funtion()

 

The Top Secret Password Reader is a small python script that takes a users input, matches it with a text file, and either grants access or denies it. It does all of that within two functions. It might sound complicated, but it is very simple. Take a look below.


 

The Code

Download this Python script on Github.

 

How It Works

The script is broken up into two functions. There is the primary() function and the top_secret() function. This is essential how they work.

primary()

Notice the userInput inside the parenthesis, It is a parameter that will be passed inside this function.

Example(Passing a Parameter):

def name(name):
   print(name)

name("Joe")
#Output: Joe

First line in this function is the passwordFile variable. This variable opens a txt file where our secret password is stored. The password in this example is password.

Next is the secretPassword variable. This variable reads the contents of the txt file using the read() method.

Now this is where the fun begins. We use an If Else statement to match the userInput and the contents in secretPassword. If the values match, you will receive a message “You’re In” and it will call the top_secret() function. By call I mean it will run the contents inside our top_secret() function. If the password does not match, then the program tells you “Access Denied”.

top_secret()

This function is simple. It just prints out “Classified Insider Trading Information“.

In the last line, it asks for for the password with the input method and whatever we type will be passed into the main function.

Here is a flow chart on how it all works.

Python beginners

Thanks for reading. Feel free to leave a comment below with any questions or a function you were able to create.

Become a Python Pro!

Well come on over and take a look at “Automate the Boring Stuff with Python: Practical Programming for Total Beginners“. If you’ve ever spent hours renaming files or updating hundreds of spreadsheet cells, you know how tedious tasks like these can be. But what if you could have your computer do them for you?

In Automate the Boring Stuff with Python, you’ll learn how to use Python to write programs that do in minutes what would take you hours to do by hand—no prior programming experience required. Once you’ve mastered the basics of programming, you’ll create Python programs that effortlessly perform useful and impressive feats of automation to:
–Search for text in a file or across multiple files
–Create, update, move, and rename files and folders
–Search the Web and download online content
–Update and format data in Excel spreadsheets of any size
–Split, merge, watermark, and encrypt PDFs
–Send reminder emails and text notifications
–Fill out online forms

 

This post “Python For Beginners – Top Secret Password Reader” is located under the Programming Category

Learn how to Create a Website status monitor in Python!

2Shares

admin

Leave a Reply

Your email address will not be published.