程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Experiment 2: Structure and reuse of Python programming

編輯:Python

WeChat Official Account: Creative Diary
Send: Python Experiment
Get a complete and clear report, source code and other resources


A few days ago, I found a giant bull's artificial intelligence learning e-book. It is easy to understand, humorous, and has no ads. I can't help but share it with everyone.(click to jump to artificial intelligence learning materials)

One.Experiment purpose
1.Master the control structures such as branch and loop of the program;
2.Master the use of random library;
3.Understand the exception handling and usage of the program;
3.Master the definition and calling methods of functions.

Second.Knowledge points
1. Program control structure

2. Function and code reuse

3 Experiment content and steps
1. Guess the number game.Preset an integer between 0-9 in the program, and let the user input the guessed number through the keyboard. If it is greater than the preset number, it will display "The number you guessed is greater than the correct answer"; if it is less than the preset number, it will display "The number you guessed is less than the correct answer", and so on, until you guess the number correctly, displaying "You guessed it N times, you guessed it right, it's really good", where N is the number of times the user has entered a number.

2. Number guessing game continued.Adapt the number guessing game in 1, so that the computer can randomly generate a preset number in the range of 0-100, and other game rules remain unchanged.
3. Continue the number guessing game.Use the variable maxtimes to set the maximum number of guesses allowed (for example, only 6 guesses are allowed at most maxtimes=6), and prompts for a few more chances after guessing wrong.Rewrite the entire program with a for loop and submit.(Hint, you can use break to jump out of the loop after guessing correctly) (For source code, please refer to WeChat public account download)

4. The number guessing game continued.In order to increase the reusability of the code, the number guessing game is encapsulated as a function GuessSecret(maxtimes), and the maximum number of guessing numbers maxtimes is allowed as a parameter.When calling GuessSecret, users are allowed to set maxtimes themselves to beautify the output interface of the program.Submit program documents.(For details, please refer to WeChat official account download)

ExecuteResults as shown below.

5. Recursive method to draw Koch curve.Please input the "Koch Curve Drawing" program line by line according to the content in the textbook, modify and run the effect according to your own ideas, and experience recursive thinking.(For details, please refer to WeChat public account download)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved