Cov txheej txheem:

Python Tus lej Yooj Yim Yooj Yim Yooj Yim: 11 Kauj Ruam
Python Tus lej Yooj Yim Yooj Yim Yooj Yim: 11 Kauj Ruam

Video: Python Tus lej Yooj Yim Yooj Yim Yooj Yim: 11 Kauj Ruam

Video: Python Tus lej Yooj Yim Yooj Yim Yooj Yim: 11 Kauj Ruam
Video: Data Science with Python! Extracting Metadata from Images 2024, Hlis ntuj nqeg
Anonim
Yooj Yim Python Number Guessing Game
Yooj Yim Python Number Guessing Game

Hauv qhov kev qhia no peb yuav qhia yuav ua li cas los tsim tus nab npawb nab npawb kwv yees kev ua si hauv daim ntawv thov Pycharm. Python yog hom lus sau uas zoo rau ob tus pib thiab cov kws paub ib yam nkaus. Coding style hauv Python yog ib qho yooj yim nyeem thiab ua raws. Lub hom phiaj kawg ntawm qhov kev qhia no yog kom pom lub teeb me me ntawm yuav ua li cas sau cov ntawv yooj yim rau kev lom zem uas yuav ua rau ib tus xav paub txog kev ua haujlwm.

Cov Ntsiab Lus:

1. Tau txais Python version 3.7 thiab nruab

2. Tau Pycharm thiab nruab

3. Kev teeb tsa Pycharm thawj zaug

4. Tsim tus lej tsis raug

5. Tau txais tswv yim los ntawm tus neeg siv

6. Tsim kom yooj yim thaum lub voj

7. Tsim ib qho "yog", "elif", "lwm yam" nqe lus

8. Muab lus qhia rau tus neeg siv

Ntxiv

Ntsiab lus

Txoj Cai Kawg

Kauj ruam 1: Txheej txheem cej luam

Txheej txheem cej luam
Txheej txheem cej luam

Qhov kev twv txiaj no tau tsim los txhawm rau qhia tus neeg siv qee cov txheej txheem kev siv yooj yim siv nab nab nrog pycharm IDE. Lub hauv paus ntawm qhov kev qhia no yuav siv lub tshuab hluav taws xob tus lej los tsim qhov kev twv twv yooj yim. Qhov kawg tshwm sim tuaj yeem ua rau ib tus neeg nkag siab tias cov lej raug tsim tawm zoo li cas. Piv txwv li, hauv kev ua yeeb yaj kiab game qhov kev puas tsuaj raug cuam tshuam hauv cov lej cov lej feem ntau tau tsim nrog cov lej tsim hluav taws xob uas muaj qhov tshwj xeeb xav tau zoo ib yam rau qhov peb tau tsim. Tus lej tsim hluav taws xob tuaj yeem nyuaj dua, tab sis ib tus tuaj yeem tau txais lub tswv yim yooj yim ntawm nws ua haujlwm li cas.

Kauj Ruam 2: Txhim Kho Python thiab Pycharm

Cov peev txheej:

www.python.org/

www.jetbrains.com/pycharm/

Kauj Ruam 3: Daim Video Qhia

Image
Image

Thov saib cov vis dis aus qhia saum toj no, thiab tom qab ntawv txheeb xyuas cov theem hauv qab no kom pab koj nkag siab ntxiv tias yuav sau qhov kev ua si li cas.

Kauj Ruam 4: Tsim Tus Zauv Random

Tau txais tswv yim los ntawm tus neeg siv
Tau txais tswv yim los ntawm tus neeg siv

Rau qhov kev ua si kom muaj kev sib tw yooj yim peb xav tsim tus lej tsis raug hauv qab 100. Tus lej no yuav yog tus uas xav tau los ntawm tus neeg uas ua ntawv. Tus nab npawb yuav nyob nruab nrab ntawm 1 txog 99. Peb ua tiav qhov no los ntawm kev sau cov lus hauv qab no:

randomNumber = random.randint (1, 99)

"randomNumber" yog qhov sib txawv uas peb yuav khaws tus lej tsis nyob hauv.

"random.randint (1, 99)" yog siv los tsim tus lej random ntawm 1 thiab 99.

*Nco tseg: Nco ntsoov nyob rau sab saum toj ntawm cov cai koj sau "ntshuam tsis raug" lossis koj yuav tsis tuaj yeem siv "random.randint (1, 99)"

Ib qho ntawv tseem ceeb yog ua raws qhov kev nkag ncaj nraim los ntawm cov piv txwv li Python tau teeb tsa los ntawm kev nkag mus. Yog tias nqe lus tau muab tso rau ntawm kab tsis raug qhov chaws qhov chaws yuav muab yuam kev thaum ib tus sim ua si qhov game.

Kauj Ruam 5: Tau Txais Cov Lus Qhia los ntawm Tus Neeg Siv

Txhawm rau kom peb kev ua si ua haujlwm peb yuav tsum muaj peev xwm tau txais cov neeg siv tswv yim. Peb yuav tsum tau kwv yees kwv yees tus lej uas yuav los ntawm tus neeg uas ua ntawv. Qhov ntau rau tus lej uas tuaj yeem kwv yees tau yog los ntawm 1 txog 99. Txoj haujlwm no tsis muab qhov yuam kev thaum tus lej nyob sab nraud ntawm qhov ntau, txawm li cas los xij lub voj yuav txuas ntxiv mus kom txog thaum tus lej raug kwv yees.

Peb ua qhov no los ntawm kev siv cov lus txib "input" uas koj tuaj yeem sau zoo li no.

twv = int (input ("sau tus lej nruab nrab ntawm 1 thiab 99:"))

Peb tab tom khaws cov neeg siv tswv yim hauv qhov sib txawv hu ua "kwv yees". "Int" txhais tau tias peb khaws cov tswv yim los ntawm tus neeg siv ua lub ntsiab lus nws yuav yog tus lej tus lej. Cov ntu rau kev nkag ("sau tus lej nruab nrab ntawm 1 thiab 99:") qhia rau lub khoos phis tawj peb tab tom siv cov neeg siv nkag mus, thiab tom qab ntawd tso tawm cov lus hauv qab no yog lub voj txuas ntxiv.

Kauj Ruam 6: Tsim Kev Yooj Yim Thaum Loop

Tsim Kev Yooj Yim Thaum Loop
Tsim Kev Yooj Yim Thaum Loop

Peb yuav tsum tam sim no tsim lub sijhawm voj. Txhawm rau ua tiav qhov no, peb yuav tsum sau ib nqe lus uas yuav ua haujlwm kom txog thaum nws tsis muaj tseeb. Lub voj thaum tsis nkag rau hauv qhov haujlwm no thiab suav nrog "Yog/Elif" cov nqe lus uas nkag rau hauv qab nws. Cov lus "Yog/Elif" yuav ua haujlwm txuas ntxiv mus txog rau thaum lub sijhawm cov lus qhia tsis muaj tseeb.

thaum randomNumber! = kwv yees:

Kauj Ruam 7: Tsim Yog Yog Elif Nqe Lus

Tsim Ib Daim Ntawv Elif
Tsim Ib Daim Ntawv Elif

Cov lus, "Yog/Elif" sawv rau yog tias qhov no raug lawm ces ua qhov no yog tsis yog, ua lwm yam. Cov lus sau tau sau kom tus neeg siv tuaj yeem nkag mus rau lub tswv yim tshiab yog tias thawj qhov kev twv tsis raug. Cov ntawv luam tawm hauv "Yog/Elif" yuav muab lawv cov lus qhia rau qhov seb qhov kev kwv yees yog siab dhau lossis qis dhau.

Kauj Ruam 8: Sau Kab Lus Kawg

Sau Daim Ntawv Kawg
Sau Daim Ntawv Kawg

Cov lus kawg tau sau rau sab nrauv ntawm lub voj thiab tsis muaj qhov nkag. Thaum tus neeg siv kwv yees tus lej raug thaum lub voj yuav "tawg" lossis "nres", thiab tom qab ntawd txav mus rau nqe lus kawg. Qhov no yuav tshwm sim thaum qhov sib txawv "kwv yees" thiab qhov sib txawv "randomNumber" sib npaug. Qhov kev ua si yuav xaus mus txog thaum txoj haujlwm pib dua.

Kauj ruam 9: Ntxiv

Tom qab ua tiav qhov kev twv ua si Qhia paub, ib tus yuav xav tshawb txog nab hab sej ntxiv. Nov yog ob peb lub tswv yim los tawm tsam koj qhov kev txawj ntse Python.

  1. Sim hloov tus lej ntau rau tus lej tsis raug.
  2. Hloov cov lus rau tus neeg siv rau qee yam zoo dua.
  3. Sim ua qhov program kom tau qhab nia ntawm pes tsawg sim nws kom tau txais cov lus teb raug.

Kauj ruam 10: Lo lus tseem ceeb

  1. Python yog hom lus programming.
  2. Pycharm yog qhov program uas pab tsim Python cov haujlwm.
  3. "random" yog tus lej zauv random
  4. "hloov pauv" yog lub cim uas tus nqi yuav hloov
  5. "int" yog hom ntaub ntawv luv rau cov lej ua ke
  6. "input" yog yuav ua li cas thiaj li nkag tau rau cov neeg siv khoom siv
  7. "luam tawm" yuav cia koj luam cov lus ntawm lub vijtsam rau tus neeg siv
  8. "thaum" yog kab lus hais uas hais tias ua qee yam thaum qhov no muaj tseeb
  9. "yog" ib nqe lus uas txhais tau tias yog qhov no yog qhov tseeb ua qhov no
  10. "! =" txhais tias tsis sib npaug rau
  11. "<" txhais tau tias tsawg dua
  12. ">" txhais tau ntau dua
  13. "elif" luv rau lwm qhov yog hais tawm

Pom zoo: