Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming


Friday: Creating Your First Program



Download 2,61 Mb.
bet21/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   17   18   19   20   21   22   23   24   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Friday: Creating Your First Program
Every Friday will be known as “Project Day,” where we will build a small application or game together, which uses the concepts learned throughout the week. This week, however, I’m just going to have you write some code into a cell so that you can see the power of Python. Since we haven’t gone over any Python just yet, I wanted you to be able to experience what we will learn over the upcoming weeks. The code your about to write will use concepts from weeks 2, 3, and 4. By the end of these weeks, you’ll be able to fully understand each line of the following code and make your own tweaks to make the program more challenging.
We’re going to be working from the Jupyter Notebook file from yesterday’s lesson. If you had closed out of the program since coming back to this book, go ahead and reopen the file.
Note if you forgot how to open Jupyter notebook, go back to yesterday’s lesson and redo the steps, except for creating a file.

Line Numbers Introduced


For larger projects, it becomes tough to follow along with books sometimes. For this project, and all other lessons going forward, I’ll be implementing line numbers. This will make it easier for you to follow along and check if you wrote the code correctly:

1| ←

Line numbers will now appear on the left side of all cells, as we will need to write all this code within a single cell. Be sure to pay attention to these numbers, as you may see them jump a couple lines:

1| # this is the first line in the cell
5| # this is the fifth line in the cell

This means that you should write the second line shown, on the 5th line.
Note turn lines on by pressing “L” after clicking the cell’s side.

Creating the Program


The first thing that we need to do is create a new cell below the current cell in our file. In order to do that, simply follow these steps:

  1. Click the last cell in the file.

  2. While it is highlighted, go to the “Inserttab in the menu bar, and click “Insert Cell Below.”

We now have a cell to work with for our project. If you’d like to create a markdown cell that says “Guessing Game” as the header, feel free to look back at the previous lesson and how we did it before. Within that new cell, let’s go ahead and write the following code:

1| # guessing game
2| from random import randint
3| from IPython.display import clear_output
5| guessed = False
6| number = randint(0, 100)
7| guesses = 0 9| while not guessed:
10| ans = input("Try to guess the number I am thinking of!")
# use tab to indent
12| guesses += 1
14| clear_output( ) 16| if int(ans) == number:
17| print("Congrats! You guessed it correctly.")
# use tab twice to indent twice
18| print( "It took you { } guesses!".format(guesses) )
19| break 20| elif int(ans) > number:
GettinG Started
21| print("The number is lower than what you guessed.") 22| elif int(ans) < number:
23| print("The number is greater than what you guessed.")

This program is not perfect by any means, but it’s certainly fun to try and guess the number that the computer is thinking of. Now, I know that this looks like a foreign language to you right now, but over the next couple of weeks, each line will begin to make sense. Eventually you’ll even be able to make your own changes and improvements to the game! What I want you to do now is run the cell and play the game. Begin to think like a developer, and ask yourself these questions while you play:

  • What improvements can I make?

  • What makes the program crash?

  • What would I do better?

Don’t be afraid if you get an error, it’s all part of the growth of becoming a developer! The fun part about testing the code that you write is that you try to break it. As we go forward, I’ll challenge you with questions about why a line in the code works the way it does. When this happens, try to think about it for a couple minutes, even try to Google the answer. As a developer you’ll find a lot of what you do is Googling a problem. This is what separates good developers from great ones… the ability to figure out problems on their own. With the rest of the lessons in this book, you’ll be well on your way to figuring out problems without my help.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   17   18   19   20   21   22   23   24   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish