The writing is a bit messy, but it's just such a thing, referring to the implementation of C language, the logic inside has not changed, just copied it. It's just that the language is different, and the code is different when it is implemented.
n =int(input("请输入菱形的宽度:"))# 打印上三角for i inrange(1,int((n +1)/2)+1):print(" "*int(((n +1)/2- i)),"*"*(2*(i-1)+1))for i inrange(1,int((n +1)/2)):print(" "*i ,"*"*((int((n+1)/2)- i)*2-1))# 1. 利用python字符串可以用 乘法运算符,省去两个for循环# 2. 得到的结果是 浮点类型~,需要转换为int类型print(type(5/3))# 3. 另外需要注意的是,连个字符串用 , 连接,不能使用 + , 加号会使 第一个 空格没有