Python 程式列印圖案“G”
如果需要使用“*”列印字母“G”的圖案,可以定義一個方法,然後使用巢狀迴圈來迭代數字並列印“*”,以形成“G”圖案。
以下是演示程式碼,作用相同-
示例
def display_pattern(my_line):
my_pattern=""
for i in range(0,my_line):
for j in range(0,my_line):
if ((j == 1 and I != 0 and I != my_line-1) or ((I == 0 or I == my_line-1) and j > 1 and j < my_line-2) or (I == ((my_line-1)/2) and j > my_line-5 and j < my_line-1) or (j == my_line-2 and I != 0 and I != my_line-1 and I >=((my_line-1)/2))):
my_pattern=my_pattern+"*"
else:
my_pattern=my_pattern+" "
my_pattern=my_pattern+"\n"
return my_pattern
num_line = 8
print("The pattern G has been shown below :")
print(display_pattern(num_line))輸出
The pattern G has been shown below : **** * * * * * * * * * ****
說明
定義了一個名為“display_pattern”的方法,該方法以行作為引數。
使用巢狀迴圈來迭代數字,
只要符合條件就列印“*”。
達到基本條件時,在控制檯上顯示輸出。
控制脫離迴圈。
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言
C++
C#
MongoDB
MySQL
Javascript
PHP