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

[Python training camp] Python daily practice -- day 18: snake length (three quotation marks usage)

編輯:Python


Hello, Hello, my name is Dream ah , An interesting Python Blogger , Please go easy on me
2021 Blog star of the year TOP100,2021 Blog star of the year TOP5,Python Quality creators in the field , Welcome to cooperate with me ( At the end of the article VX Want to join the learning exchange group or Learning materials welcome +++)
Introductory notes : This paradise never lacks genius , Hard work is your final ticket !
Last , May we all shine where we can't see , Let's make progress together
“ Ten thousand times sad , There will still be Dream, I've been waiting for you in the warmest place ”, It's me ! Ha ha ha ~

Preface :【Python Training camp 】 Is aimed at Python A war created by language learning Brush questions and revel party! If you don't grasp the solid foundation of knowledge , Welcome to this course :Python Public class It's best to use it together ~ If you like, hurry up and subscribe ! If you don't have self-control or motivation to learn and communicate together , Welcome to send a private letter or add my... At the end of the text VX, I will pull you into the learning exchange group , Let's exchange and study together , Group punch in

Python Practice every day :

  • Title Description
  • Their thinking
  • The source code to share
  • Learning summary
    • ** To summarize the three quotation mark method :**
  • The articles ---- Good article recommends

Title Description

Title Description
This question is to fill in the blanks , Just calculate the result , Use the output statement in the code to output the filled results .

+-------------------------------------------------+
| |
| H###### #### |
| # # # |
| # # # |
| # #### # # |
| # # # # # |
| ######@### # # |
| # #### # # |
| # # # # # |
| ####@#######@### # # |
| # # # # # |
| T ##### # # # ## |
| # # ### ### ## |
| ################ # # #### |
| # # # # |
| ############## #######@########## |
| # ### |
| ########################### |
+-------------------------------------------------+

Xiao Ming found an old computer in Grandpa's private collection . There's no graphical interface , Can only be programmed with the console . After Xiao Ming's groping , Magically designed the greedy Snake game on the console .
Above , It's a screenshot of the game .
among ,H It means snake head ,TT Indicates snake tail .# Represents the body of a snake ,@ Indicates where the body crosses and overlaps . Can you tell the length of the greedy snake now ?
Actually , Just count # The number of 1, Count out @ The number of counts counts count 2, Plus the head and tail , Plus the head and tail 1$ And we're done .
Count by hand ? Too tired eyes , Smart, why don't you let the computer help ?
The requirement of this question is : Please output the length of the greedy snake in the above figure ?

 Operation limit
Maximum operation time :1s
Maximum running memory : 128M

Their thinking

  • count Function directly calculates the number of times a symbol appears
  • The three quotation marks method represents the maze in the form of string

The source code to share

# Time : 2022/2/11 13:06
# File : snake .py
# Author : yes Dream ah !
# WeChat VX : Xu18300396393
# Ten thousand times sad , There will still be Dream, I've been waiting for you in the warmest place !
import os
import sys
# Please enter your code here 
a = ''' +-------------------------------------------------+ | | | H###### #### | | # # # | | # # # | | # #### # # | | # # # # # | | ######@### # # | | # #### # # | | # # # # # | | ####@#######@### # # | | # # # # # | | T ##### # # # ## | | # # ### ### ## | | ################ # # #### | | # # # # | | ############## #######@########## | | # ### | | ########################### | +-------------------------------------------------+'''
print(a.count('#')+2+2*a.count('@'))

Learning summary

To summarize the three quotation mark method :

actually 3 Two single quotes and 3 Double quotes are not often used , But it is useful in some special format strings . Usually, when we define a string in single quotation marks or double quotation marks, we can only write the strings together in one line , If you have to write multiple lines , You have to add a... After each line \ Represents a hyphen , such as :

str1 = "List of name:\
Hua Li\
Chao Deng"
print(str1)

But we can't get the answer we want , In fact, its output is :

List of name: Hua Li Chao Deng

So how do we get the desired output format of one name per line ? This is it. 3 The use of quotation marks :

str1 = '''List of name: Hua Li Chao Deng'''
print(str1)

Get the answer :

List of name:
Hua Li
Chao Deng

Although we can also add... To the string \n Realization :

str1 = "List of name:\nHua Li\nChao Deng"
print(str1)
#List of name:
#Hua Li
#Chao Deng

But it looks a lot messy when typing, doesn't it ? So try to use it in this case 3 Quotation marks , as for 3 Single quotation marks or double quotation marks are the same , Just note that if the string contains single quotation marks, you need to use double quotation marks to define it .

And use 3 Another great use of quotation marks is : add comments of explanations !

Today is my day Python The first of the training camp 18 God , I hope to see the best of you every day

The articles ---- Good article recommends

[Python Public class ] Zero base play Python The basic chapter ---- Section 1 :Python The introduction of

[Python Public class ] Zero base play Python Advanced ---- Section 1 :Python File operations in

Come on, let's have a problem swiping Carnival party Well !----【Python Training camp 】
All right. , That's all I want to share with you today
️️️ If you like , Don't be stingy with your one key triple connection ~


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