Python exercise 1-diamond printing

Python group's first homework

printXinXin.py

# 边长为n:用户端输入
n = input('菱形边长为:n = ')
# input的两个功能:打印传入参数 + 以str字符型输入作为函数返回值

n = int(n)
# 使用input语句返回值是str字符型,为方便下面对n进行运算,使用强制转换符int将n转化为数据类型
# 如果要求输入时数据,通常input后接强制转换语句

# while循环判断
m = temp = 2*n- 1  # 确定打印行数,   重复赋值语句python特有。
while m>0:
    if(m == temp or m == 1):# 第一行和最后一行做特殊处理
        print(' '*(n-1) + '*' + ' '*4*(n-1))
    elif(m >= n):# 打印上半部分
        print(' '*(m-n) + '*' + ' '*(2*(temp-m)-1) + '*')
    else: # 打印下半部分
        print(' '*(n-m) + '*' + ' '*(2*m-3) + '*')
    m-=1 # 继续下一行

Important knowledge points

  • Pay attention to the forced type conversion of input
  • If-elif-else loop judgment statement [Python does not have a switch statement]
  • Python has repeated assignment statements

result

Insert picture description here

Guess you like

Origin blog.csdn.net/Chaoyuan_Jam/article/details/82891316