Home > Backend Development > Python Tutorial > python learning diary (1)

python learning diary (1)

巴扎黑
Release: 2017-06-23 10:57:51
Original
1494 people have browsed it

I studied Alex’s python tutorial before. Alex’s teaching is really great and humorous. hey-hey. Hope I can persevere.

I organized and reviewed the basic knowledge I learned before. I hope that I can go further and further on the road of learning python, and everyone can encourage each other.

First week review summary
1 Initial game code
number_of_kids =8
guess_number=int(input("guess_number_of_kids:"))
if guess_number==number_of_kids:
print("You got it!")
elif guess_number>number_of_kids:
print("think smaller...")
else:
print("think bigger...")


If you guess three times and exit without guessing, you need to use a loop


(while first acquaintance

Code 1:

count = 0
while True:
print("count:",count)
count = count +1 #count +=1
Wireless loop----- Applied to the number guessing game above)

How to set it if you want to stop at a certain place?


Simple example while
count = 0
while True:
print("count:",count)
count = count *2 #count*=2
if count == 1000:
break


Question: What should I do? add? Make the initial game loop guess?

Improvement 2

number_of_kids =8

while True:
guess_number=int(input("guess_number_of_kids:"))

if guess_number= = NUMBER_OF_KIDS:
PRINT ("You get it!")
Elif Guess_number & GT; Igger. ..")


Question: Why is while true placed in this position?

Answer: while true represents an infinite loop without interruption. If while true is placed below the next line,

will not be able to loop the next input, which will interrupt the game.

Running results: You can enter numbers to guess unlimited times. Infinite loop
Disadvantages: When you enter the correct answer, you still continue to guess without ending.

Question: Improve how to end the program directly when the guess is correct

Improvement 3:

number_of_kids =8

while True:

guess_number= int(input("guess_number_of_kids:"))

##if guess_number==number_of_kids:

("think smaller...")
else:

print("think bigger...")



The addition of break can end the game when the number is guessed correctly. But if you guess incorrectly, how can you end the game within the specified number of times?

Improvement 4:

number_of_kids =8

count = 0

while True:

if count ==3:

break

guess_number=int(input("guess_number_of_kids:"))

if guess_number==number_of_kids:

print("You got it!")

break

elif guess_number>number_of_kids:
print("think smaller...")
else:
print("think bigger...")

added if count ==3 After :break, it still didn't succeed on the third guess.
Reason: No counting added!

number_of_kids =8

count = 0

while True:


if count ==3:

break

guess_number =int(input("guess_number_of_kids:"))

##if guess_number==number_of_kids:

print("You got it!")

break

elif guess_number>number_of_kids:

print("think smaller...")
else:
print("think bigger...")

count+=1


Very Great!


Can it be optimized?


Q: What can replace if count ==3: break and count?

number_of_kids =8

count = 0


Can it still be optimized?

What can be used instead if count ==3: break

while True:

if count <3:

guess_number=int(input("guess_number_of_kids: "))

if guess_number==number_of_kids:
print("You got it!")
break
elif guess_number>number_of_kids:
print("think smaller...")
else ;

Only perform fuckoff if the third guess is not correct
Make the following modifications:

number_of_kids =8

count = 0

while count <3:

guess_number=int(input("guess_number_of_kids:"))

if guess_number==number_of_kids:

print("You got it!")

break

elif guess_number>number_of_kids:
print("think smaller...")
else:

print("think bigger...")


count+= 1

if count ==3:

print("you have tried too many times..fuck off")

Improvement :More advanced hahahaha

number_of_kids =8

count = 0

while count <3:

guess_number=int(input("guess_number_of_kids: ")

iF Guess_number == Number_of_kids:

PRINT (" You Got it! ")

Elif Guess_number & Fip_kids:

PRint (" Think S Maller ... ")
else:
print("think bigger...")

count+=1

else:
print("you have tried too many times. .fuck off")


Xiaosi Homework: 4/9 Sunday

How to change the code so that when it comes out for the third time, only the numbers will be displayed and no fuckoff will be displayed , when you enter it for the fourth time, it does not prompt the size, but directly outputs fuckoff?


First of all, clarify the idea of ​​the previous code: count starts from 0 and enters the while loop. As long as the guess is correct in the range less than 3, it will break.
If you don’t guess correctly, smaller or bigger will appear. When count+=1 is equal to 2, it enters the third loop. When the guess is wrong again
After bigger or smaller appears, go to the next program, that is, count+=1 is equal to 3, enter else operation, followed by fuckoff.

Operation modification idea: When it comes out for the third time, only the numbers and the small and large prompts will be displayed, and fuckoff will not be displayed. This means that when count+=1 is equal to 3,

do not directly go to fuckoff program, it means that the setting of fuckoff cannot appear in the previous subroutine of while count <3, but should be a parallel program.
When count=3, the interface for guessing numbers will continue to appear, regardless of whether the guess is wrong or correct. Both fuckoff and over.

number_of_kids =8

count = 0


while True:

if count <3:

      guess_number =int(input ("guess_number_of_kids:"))

if guess_number==number_of_kids:

                                                                                                                                                                                                                                                                     break smaller...")

                                                                          using   use with   using             use using using ’’s out through out out through   Through out through out out through out‐‐ through   ‐ ‐‐‐‐‐‐ and _ int(input("guess_number_of_kids:"))
print("you have tried too many times..fuck off")

break


while optimization lesson

How to use for instead of while


number_of_kids =8

count = 0

while count <3: #for i in range(3)


guess_number=int(input("guess_number_of_kids:"))

if guess_number==number_of_kids:

print("You got it!")

break

elif guess_number>number_of_kids:

print("think smaller...")

else:

print("think bigger...")

count+=1

else:

print("you have tried too many times..fuck off")



Change to:

number_of_kids =8

count = 0

#for i in range(3)

guess_number=int(input("guess_number_of_kids:"))

if guess_number==number_of_kids:

print ("You got it!")

")

count+=1

else:
print("you have tried too many times..fuck off")


The most basic for loop.

for i in range(0,10,1): #1 is the default value, you can not write it, it means 1.
print("loop",i)

for i in range(0,10,2):
print("loop",i)
Run result:
=== RESTART: C:/Users/dell/AppData/ Local/Programs/Python/Python35-32/12.py ===
loop 0
loop 2
loop 4
loop 6
loop 8
>>>


for i in range(0,10,3):
print("loop",i)
Run result:
=== RESTART: C:/Users /dell/AppData/Local/Programs/Python/Python35-32/12.py ===
=== RESTART: C:/Users/dell/AppData/Local/Programs/Python/Python35-32/12. py ===
loop 0
loop 3
loop 6
loop 9
>>>

Optimize the number guessing game again: guess it three times and exit. Very decisive, is it possible to set prompts according to personalized needs?
Added: Do you want to play?


number_of_kids=8

count=0

while count <3:
        guess_number = int(input("guess_number_of_kids:"))
if guess_number == number_of_kids:
print("yep, you got it!")
break
elif guess_number> number_of_kids:
print("think smaller...")
else ; )
                                                                                                                                                                                                                                     

#task: Type this code alone to clarify your thoughts.

New knowledge point: continue

for i in range(0,10):

if i<3:

print("loop",i)

else :

continue

print("hehe"..)



Execution result: === RESTART: C:/Users/dell/AppData/Local/Programs/Python/Python35- 32/4.py ===
loop 0
hehe...
loop 1
hehe...
loop 2
hehe...

Add a breakpoint later? ? ? What does it mean?
for i in range(0,10):
if i<3:
print("loop",i)
else:

continue(The function is to jump out of this loop and enter Next cycle)

print("hehe"..)
Q: Same code but no hehe?


New knowledge point: double loop

for i in range(10):
print("________",i)

for j in range(10) :

print(j)

Execution result:

=== RESTART: C:/Users/dell/AppData/Local/Programs/Python/Python35-32/4.py ===
________ 0

The above is the detailed content of python learning diary (1). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template