My python diary --- DAY1, python diary --- DAY1

Source: Internet
Author: User

My python diary --- DAY1, python diary --- DAY1

I learned how to teach Alex in the python tutorial of Alex. Hey. I hope I can stick to it.

I reviewed the basic knowledge I learned earlier. I hope that I can go further and further on the way to learning python.

Weekly 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 ...")


Three guesses, three guesses, No guesses, exit, and use a loop.

 


(While first met

Code 1:

Count = 0
While True:
Print ("count:", count)
Count = count + 1 # count + = 1
Wireless loop-apply to the above guess number game)

How do I set it to stop somewhere?


Simple Example: while
Count = 0
While True:
Print ("count:", count)
Count = count * 2 # count * = 2
If count = 1000:
Break


Q: How can I add a new one? So that the initial game can be cyclically guessed?

Improvement 2

Number_of_kids = 8

While True:
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 ...")


Q: Why should while true be placed in this position?
A: while true indicates an infinite loop without interruption. If while true is placed under the following row,
The game will be interrupted if the next input cannot be recycled.

Running result: You can enter a number to guess the infinite loop infinitely.
Disadvantage: When the correct answer is entered, the guess continues, but it does not end.

Q: How can I end the program directly when I guess it is correct?

IMPROVEMENT 3:

Number_of_kids = 8

While True:
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 ...")


The addition of break can end the game when you guess the number. But if you guess not
How many times does the game end?

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 ...")

If count = 3: break is added, it is still not successful in the third guess.
Cause: no count is 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


Great!



Can it be optimized?

Q: Can I replace if count = 3: break and count?

Number_of_kids = 8

Count = 0


Can it be optimized?

Can I replace 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:
Print ("think bigger ...")

Count + = 1

Print ("you have tried too times .. fuck off ")

 

Perform fuckoff only when you have not guessed it for the third time.
Make the following changes:

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 times .. fuck off ")




Improvement: Advanced

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

Else:
Print ("you have tried too times .. fuck off ")


Silly homework: 4/9 Sunday


How can I change the code so that only numbers are displayed and fuckoff is not displayed when the third input is made? When the fourth input is made, the fuckoff is output without prompting the size?

First, clarify the idea of the previous Code: count starts from 0 and enters the while LOOP, but break only needs to be guessed if it is less than 3.
Smaller or bigger appears if you don't guess it. When count + = 1 is equal to 2, that is, the third cycle is entered.
If bigger or smaller occurs, run the next program, that is, count + = 1 is equal to 3, and enter else to run, followed by fuckoff.

Job change ideas: when the third time you want to make a job, only the number and the prompt are displayed with a small size. If the fuckoff is not displayed, it means that when count + = 1 equals 3,
If you do not directly use the fuckoff program, it means that the fuckoff settings cannot appear in the previous while count <3 subroutine, but should be tied to the program,
When count = 3, the page for guessing numbers is displayed, and the result is fuckoff regardless of whether you guess the number or not.

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:
Print ("You got it! ")
Break
Elif guess_number> number_of_kids:
Print ("think smaller ...")
Else:
Print ("think bigger ...")

Count + = 1

If count = 3:
Guess_number = int (input ("guess_number_of_kids :"))
Print ("you have tried too times .. fuck off ")
Break

 

While optimization course

How to use for to replace 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 times .. fuck off ")


Changed:

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! ")
Break
Elif guess_number> number_of_kids:
Print ("think smaller ...")
Else:
Print ("think bigger ...")

Count + = 1

Else:
Print ("you have tried too times .. fuck off ")


The most basic for loop.

For I in range (, 1): #1 is the default value. If this parameter is left empty, 1 is returned.
Print ("loop", I)

For I in range (0, 10, 2 ):
Print ("loop", I)
Running 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)
Running 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
>>>

Optimization of the guess digital game again: it seems that it is absolutely no choice to exit after three guesses. Can you set a prompt Based on Personalized Requirements?
Add: 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:
Print ("think bigger ...")

Count + = 1
If count = 3:
Countine_confirm = input ("do you want keeping guessing ...? ")
If countine_confirm! = "N": If you press n, the game is exited.
Count = 0 when you press the Enter key, it indicates that you want to play the game, and the program continues to run.

Task: Separate the code and clarify the ideas.

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...

Then add the breakpoint ??? What do you 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 the next loop)
Print ("hehe "..)
Q: Is there no hehe for the same code?


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.