Cov txheej txheem:

Kev Qhia Python - Katsuhiko Matsuda & Edwin Cijo - Cov hauv paus: 7 Kauj Ruam
Kev Qhia Python - Katsuhiko Matsuda & Edwin Cijo - Cov hauv paus: 7 Kauj Ruam
Anonim
Python Taw Qhia - Katsuhiko Matsuda & Edwin Cijo - Cov hauv paus
Python Taw Qhia - Katsuhiko Matsuda & Edwin Cijo - Cov hauv paus

Nyob zoo, peb yog 2 tus tub ntxhais kawm hauv MYP 2. Peb xav qhia koj cov hauv paus ntawm yuav ua li cas rau tus lej nab npawb.

Nws tau tsim nyob rau xyoo 1980s los ntawm Guido van Rossum hauv Netherlands. Nws tau ua los ua tus sawv cev rau cov lus ABC. Nws lub npe yog "Python" vim tias thaum nws tab tom xav txog nab nab (nab), nws tseem tau nyeem, "Monty Python's Flying Circus". Guido van Rossum xav tias cov lus yuav xav tau lub npe luv, tshwj xeeb, yog li nws tau xaiv Python.

Khoom siv:

Khoos phis tawj thiab nab hab sej program lossis lub vev xaib (Pom zoo: repl.it)

Kauj ruam 1: Lus/Hashtags

Saib/Hashtags
Saib/Hashtags

Cov lus pom yog cov ntawv sau uas tuaj yeem siv hauv Python. Lawv tuaj yeem siv ua:

  • cov lus qhia
  • cov lus qhia
  • cov kauj ruam thiab lwm yam

Cov lus tsis muaj qhov tso tawm.

#Coding

Kauj Ruam 2: Luam tawm thiab cov lus tawm tswv yim

Luam tawm thiab cov lus tawm tswv yim
Luam tawm thiab cov lus tawm tswv yim
Luam tawm thiab cov lus tawm tswv yim
Luam tawm thiab cov lus tawm tswv yim

Luam cov nqe lus

Luam cov nqe lus, sau ua ntawv luam, yog cov lus siv los luam kab lus lossis lo lus. Yog li piv txwv:

print ("Nyob zoo ntiaj teb!")

Cov zis yuav yog:

Nyob zoo ntiaj teb!

Yog li koj tuaj yeem pom tias daim ntawv luam tawm yog siv los luam cov lus lossis kab lus.

Cov Lus Qhia Tawm

Cov lus hais tawm, sau ua tswv yim, yog cov lus siv rau "nug". Piv txwv li:

input ("Koj lub npe yog dab tsi?")

Cov zis yuav yog:

Koj lub npe hu li cas?

Txawm li cas los xij, nrog cov tswv yim, koj tuaj yeem sau rau hauv lawv. Koj tseem tuaj yeem "npe" cov lus qhia.

Zoo li no:

name = input ("Koj lub npe hu li cas?")

Koj tuaj yeem teb los ntawm kev ua qhov no:

Koj lub npe hu li cas? Katsuhiko

Tom qab ntawd koj tuaj yeem ntxiv daim ntawv yog tias ntxiv ib yam dab tsi rau cov ntaub ntawv pom.

Koj yuav kawm paub siv lawv li cas hauv Kauj Ruam 4.

Kauj Ruam 3: F Cov hlua

F txoj hlua
F txoj hlua

luam tawm (f "")

Cov zis tam sim no, tsis muaj dab tsi. Koj tsis tau luam dab tsi. Tab sis hais tias koj ntxiv qhov no:

print (f "Nyob zoo {lub npe}!")

Nws yuav ua haujlwm, tsuas yog lub npe raug xaiv. Hauv lwm lo lus, hais tias koj tau muaj tswv yim ua ntej thiab koj tau ua qhov no rau nws:

name = input (Koj lub npe yog dab tsi?)

Tom qab ntawd txoj hlua f yuav ua haujlwm. Hais rau lub tswv yim, koj muab koj lub npe. Tom qab ntawd thaum daim ntawv tshaj tawm yuav luam tawm:

Nyob zoo (txawm koj lub npe hu li cas)!

Lwm txoj hauv kev koj tuaj yeem ua qhov no yog nrog tus lej. Qhov no yuav tsis siv txoj hlua f ib yam. Lawv kuj zoo ib yam. Yog li koj yuav luam nws li cas:

npe = input ()

print ("Nyob zoo", npe, "!")

Kauj Ruam 4: Yog, Lwm Yam Yog (Elif), Cov Lus Qhia Ntxiv

Yog, Lwm Yam Yog (Elif), Cov Lus Tshaj Tawm
Yog, Lwm Yam Yog (Elif), Cov Lus Tshaj Tawm

Kuv tus lej nrog lwm lub npe siv Yog, Yog Lwm Tus Yog (Elif), Cov Lus Tshaj Tawm.

Yog cov ntsiab lus

Yog cov nqe lus, luam tawm zoo li, yog lus raws li lawv tau hu, yog kab lus. Lawv pom tias kab lus sib npaug lossis yog ib yam dab tsi rau ib qho khoom, nws tsim qhov cuam tshuam rau qee yam. Koj tuaj yeem xav tias yog nqe lus yog qhov ua rau thiab cuam tshuam. Piv txwv ntawm yog tias nqe lus yog:

name = input ("Koj lub npe hu li cas?")

#nug txog lub npe yog lub npe == "JBYT27": print ("Nyob zoo Tus Thawj Coj!")

Cov zis yuav yog:

Koj lub npe hu li cas? Katsuhiko

Nyob zoo Administrator!

Txawm li cas los xij, hais tias cov lus teb tsis yog Katsuhiko. Nov yog qhov twg lwm tus, tsim nyog, sim, thiab tshwj tsis yog cov nqe lus tuaj!

Cov ntsiab lus Elif

Cov ntsiab lus Elif, luam tawm raws li elif zoo nkauj heev yog cov lus hais. Nws tsuas yog lo lus ntxiv thiab yog tias ua ke. Yog li hais tias koj xav tau ntxiv yog tias nqe lus. Tom qab ntawd koj yuav ua qhov no:

yog lub npe == "Katsuhiko":

print ("Nyob Zoo Tus Thawj Coj!") elif name == "Coder": print ("Nyob Zoo Coder!")

Nws tsuas yog ntxiv ntau ntxiv yog tias nqe lus, tsuas yog ntxiv lwm qhov rau nws!.

Lwm Cov Lus Qhia

Lwm cov nqe lus, luam tawm ib yam li lwm tus, zoo li yog thiab cov lus hais tshwj xeeb. Lawv tau siv los qhia lub khoos phis tawj tias yog qee yam tsis yog thiab tsis yog, mus rau lwm qhov txiaj ntsig. Koj tuaj yeem siv nws zoo li no (txuas ntxiv los ntawm lwm qhov chaws sab saud):

yog lub npe == "Katsuhiko":

print ("Nyob Zoo Tus Thawj Coj!") elif name == "Squid": print ("Nyob Zoo Tus Tswv Squod!") else: print (f "Hello {name}!")

Kauj Ruam 5: Cov Qauv Txheej Txheem

Cov Qauv
Cov Qauv
Cov Qauv
Cov Qauv

Cov qauv sib xws suav nrog:

  • os
  • sij hawm
  • lej
  • sib
  • rov ua dua
  • vaub kib
  • tkinter
  • random
  • lwm yam.

Yog li txhua tus qauv no uas kuv tau teev tseg, kuv yuav qhia koj yuav siv li cas, ib qib zuj zus). Tab sis tos, modules yog dab tsi?

Cov qauv yog zoo li pob khoom uas tau teeb tsa ua ntej hauv sej. Koj tsuas yog yuav tsum tau nruab nws, uas yog tus qauv. Yog li zoo li cov cai no:

import os

Thaum koj ua qhov no, koj ua tiav ntshuam os module! Tab sis tos, koj tuaj yeem ua dab tsi nrog nws? Txoj kev uas tib neeg feem ntau siv os module yog tshem tawm nplooj ntawv. Los ntawm txhais tau tias, nws tshem tawm lub console (ntu dub) yog li nws ua rau koj lub vijtsam meej. Tab sis, txij li muaj ntau ntau, ntau, ntau tus qauv, koj tseem tuaj yeem tshem lub vijtsam siv qhov hloov pauv. Cov cai yog zoo li no:

ntshuam hloov

replit.clear ()

Tab sis ib qho tsis txaus ntseeg txog qhov kev ntshuam no yog koj tuaj yeem ua qee yam tshwj xeeb. Zoo li hais tias koj tsuas yog xav tau import pi thiab sqrt los ntawm pob lej. Nov yog qhov chaws:

los ntawm lej import pi, sqrt

Cia kuv hais tias thaum koj ua qhov no, tsis txhob, puas tau ntxiv ib qho thiab. Zoo li los ntawm… ntshuam… thiab…. Tsis txhob ua nws:)

Tom ntej no yog lub sijhawm sijhawm: Koj tuaj yeem siv lub sijhawm ua haujlwm rau:

  • ncua sij hawm
  • scroll ntawv

Tom ntej no yog tkinter, vaub kib

Koj tuaj yeem siv tkinter module rau GUI's (tshuaj ntsuam ua si), koj tuaj yeem ntshuam nws hauv cov nab hab sej ib txwm, lossis koj tuaj yeem ua qhov no hauv qhov hloov tshiab. Koj tuaj yeem siv tus vaub kib rau kev kos duab, nws tsis siv ntau rau kev tsim web txawm hais tias. Cov lej thiab sys Cov lej yog siv rau kev suav ua lej, los laij lej. Cov sys yog siv rau kev nkag mus siv cov kev hloov pauv. Kuv tsis paub tiag yuav ua li cas kuv tuaj yeem piav qhia rau koj, tab sis xav paub ntau ntxiv, nyem qhov no Random Tus qauv tsis sib xws yog siv rau kev hloov pauv qhov sib txawv thiab cov hlua. Hais tias koj xav randomize cov npe. Nov yuav yog qhov chaws:

ntshuam ntshuam

a_list = ["Katsuhiko", "ncuav qab zib", "miv", "dev"] random.choice (a_list)

Cov zis yuav yog qhov kev xaiv tsis raug los ntawm qhov sib txawv/npe. Yog li nws tuaj yeem yog ncuav qab zib, Katsuhiko, miv, lossis dev. Los ntawm cov qauv tsis sib xws, muaj ntau yam uas koj tuaj yeem nqa tau, tab sis feem ntau yog:

  • xaiv
  • ntau yam
  • lwm yam.

Qhov ntawd yog nws!

Kauj Ruam 6: Thawj Qhov Kev Ua Si! Siv Random Module

Thawj Zaug! Siv Random Module
Thawj Zaug! Siv Random Module

Tam sim no koj yuav tsim koj thawj qhov kev ua si uas siv cov qauv tsis sib xws.

Ua ntej tshaj plaws peb ntshuam tus qauv tsis sib xws

Tom qab ntawd peb yuav tsum sau qhov no:

ntshuam tus lej random2 = random.randint (1, 100) #Qhov no txhais tau tias cov lej yuav raug xaiv los ntawm 1-100, koj tuaj yeem hloov pauv yog xav tau kev kwv yees = 10 #Qhov no yog pes tsawg tus kwvtij uas tau txais

Tom qab ntawd peb luam lub npe (Tus lej nab npawb!)

Tom qab ntawd peb nkag mus rau qee yam tshiab hu ua, Thaum Tseeb:. Cov lus no yuav tso cai rau txoj cai txuas mus ntxiv.

Tom qab ntawd peb ntxiv cov lus hais tawm:

num = int (input ("Guess a number 1-100 / n:") #The / n txhais tau tias mus rau kab tom ntej

Peb ntxiv int ua ntej cov lus nug los ua nws cov lej teb tso cai rau peb kom sib txawv thiab ua lej yam nrog tus lej 2 thiab tus lej. Cov lus nug tseem ceeb no yuav tsum yog sab hauv Thaum Muaj Tseeb:.

Tom qab ntawd peb hais tias yog tus lej loj dua tus lej 2 tom qab ntawd hais tias nws siab dhau thiab nws yuav hais tias muaj pes tsawg tus kwv yees koj tau tso tseg li no:

if num> num2: print (f "Siab dhau lawm. Koj muaj {kwv yees} kwv yees sab laug") kwv yees- = 1

Tom qab ntawd koj ua qhov qub tab sis tom qab ntawd lwm txoj hauv kev ib pliag yog (tseem nyob sab hauv lub sijhawm).

yog hais tias tus lej <num2: print (f "Tsawg heev. Koj muaj {guesses-1} kwv yees sab laug") guesses- = 1

Tom qab ntawd koj ntxiv ob qho yog qhov kwv yees mus rau 0 ces koj poob thiab yog tus lej = num2 ces peb yeej

if num == num2: print (f "Koj tau txais txoj cai! Koj ua tiav nrog {twv-1} kwv yees sab laug") tawg # Qhov tawg txhais tau tias cov cai nres. yog kwv yees == 0: print (f "Koj poob lawm! Tus lej raug yog {num2}") so

Nov yog tag nrho cov cai rau tus lej twv ua si.

Txhua qhov chaws ua ke yuav tsum zoo li no:

print ("Game Number!") thaum Tseeb: num = int (input ("Guess a number 1-100 / n:")) if num> num2: print (f "Siab heev. Koj muaj {guesses-1} kwv yees sab laug ") kwv yees- = 1 yog hais tias tus lej <num2: luam tawm (f" Tsawg heev. Koj muaj {kwv yees-1} kwv yees sab laug ") kwv yees- = 1 yog tus lej == tus lej 2: luam tawm (f" Koj tau txais nws lawm! Koj ua tiav nrog {kev kwv yees-1} kwv yees sab laug ") tawg yog tias kwv yees == 0: print (f" Koj poob lawm! Tus lej raug yog {num2} ") tawg

Nov yog kuv qhov remixed version ntawm Number Game:

Cov remixed version muaj qib nyuaj thiab lwm yam kev zais.

Nyob zoo! Peb ua nws dhau los tsis tsaug zog!

Ua tsaug rau pom peb cov lus qhia. Vam tias koj tau kawm qee yam tshiab.:)

Cov kauj ruam tom ntej yog kev ua si ntau dua. Cov kauj ruam tom ntej yuav piav qhia txhua ntu ntawm txoj cai kom nkag siab tias koj tab tom ua dab tsi. Qhov no yog nyob ntawm yeem

Kauj ruam 7: Brutal Force Password Cracker

Brutal Force Password Cracker
Brutal Force Password Cracker

ntshuam ntshuam

cim = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', ' y ',' z ',' 1 ',' 2 ',' 3 ',' 4 ',' 5 ',' 6 ',' 7 ',' 8 ',' 9 ',' 0 ','! ', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', ' = ',' ~ ',' '' 'cha =' 'ba = rau cov khoom hauv cov cim: cha+= cov khoom luam tawm ("cim:"+cha)

Cov cai saum toj no yog cov cai sau txhua tus cim uas tuaj yeem siv hauv tus password

password = input ("Nkag mus rau plaub tus lej lej.").lower ()

twv = Tseeb x = 0 q = 11 w = 11 e = 11 r = 11 kaum tawm = 0 sawv daws = 1 pua = 0 txhiab = 0 thaum kwv yees: r+= 1 x+= 1 yog r == 62: e+= 1 r = 11 yog e == 62: w+= 1 e = 11 yog w == 62: q+= 1 w = 11 twv = '' a = cim [q-11] b = cim [w-11] c = cim [e -11] d = cov cim [r-11] twv+= kwv yees+= b twv+= c twv+= d

Cov cai saum toj no qhia cov txheej txheem twv thiab yuav ua li cas thiaj nrhiav tau txhua tus lej 4 tus lej cim nrog tus cim

yog twv == tus password:

print ("Guess number"+str (x)) print ("Guess:"+guess) so else: print ("Guess:"+kwv yees)

Cov cai ntawm no qhia tus lej ntawm tus lej nws tau tshuaj xyuas kom pom "tus password" uas koj tau sau.

Nov yog qhov txuas ntawm Brute Force Password Cracker:

Nws siv tag nrho 7171112 kwv yees txhawm rau tawg "" "".

Pom zoo: