你的问题是你继续使用打印. print语句(以及Python 3中的函数)将在您打印的内容后添加换行符,除非您明确告诉它不要.你可以在Python 2中这样做:

print '*', # note the trailing comma

或者在Python 3中(使用print函数),如下所示:

print('*', end='')

我的解决方案

我对问题采取了自己的看法并提出了这个解决方案:

# The diamond size

l = 9

# Initialize first row; this will create a list with a

# single element, the first row containing a single star

rows = ['*']

# Add half of the rows; we loop over the odd numbers from

# 1 to l, and then append a star followed by `i` spaces and

# again a star. Note that range will not include `l` itself.

for i in range(1, l, 2):

rows.append('*' + ' ' * i + '*')

# Mirror the rows and append; we get all but the last row

# (the middle row) from the list, and inverse it (using

# `[::-1]`) and add that to the original list. Now we have

# all the rows we need. Print it to see what's inside.

rows += rows[:-1][::-1]

# center-align each row, and join them

# We first define a function that does nothing else than

# centering whatever it gets to `l` characters. This will

# add the spaces we need around the stars

align = lambda x: ('{:^%s}' % l).format(x)

# And then we apply that function to all rows using `map`

# and then join the rows by a line break.

diamond = '\n'.join(map(align, rows))

# and print

print(diamond)

利用python画钻石_Python空心钻石相关推荐

  1. 用python画动态樱花_利用python画一棵漂亮的樱花树,turtle画图代码大全,此处感谢知乎大佬小白...

    利用python画一棵漂亮的樱花树,turtle画图代码大全,此处感谢知乎大佬小白 此处感谢知乎大佬 小白练手 练习一下比较流行的turtle(海龟库) 画一棵漂亮的樱花树,效果如下: ps: 是动态 ...

  2. python画曲线-利用python画出AUC曲线的实例

    以load_breast_cancer数据集为例,模型细节不重要,重点是画AUC的代码. 直接上代码: from sklearn.datasets import load_breast_cancer ...

  3. python中词云图怎样变成特殊图案_如何利用python画出一个多变的词云图?(1)...

    问题描述: 如何利用python画出一个多变的词云图? 解决方法:import numpy as np import matplotlib import matplotlib.pyplot as pl ...

  4. python画一颗心_利用python画一颗心的方法示例

    前言 Python一般使用Matplotlib制作统计图形,用它自己的说法是'让简单的事情简单,让复杂的事情变得可能'.用它可以制作折线图,直方图,条形图,散点图,饼图,谱图等等你能想到的和想不到的统 ...

  5. python怎么画出圆润的曲线_利用python画出AUC曲线的实例

    以load_breast_cancer数据集为例,模型细节不重要,重点是画AUC的代码. 直接上代码: from sklearn.datasets import load_breast_cancer ...

  6. python画二维温度云图_利用python画出词云图

    本文将介绍如何利用python中相应的模块画出词云图.首先给出效果图: 其中词云图中的词汇是对手机短信中的垃圾短信的统计,字体越大表示在垃圾短信中出现的频次越高.下面给出具体的步骤. 读取" ...

  7. 如何利用python画一些简单的图形

    如何利用python画一些简单的图形 1 问题描述 在我们学习python的初期,就想拿一些实际问题来练手,好提高自己的技术.我们都了解可以用python画很多漂亮的图,但是作为新手的我们如何来画一些 ...

  8. 利用python画爱心表白

    *七夕来袭!是时候展现专属于程序员的浪漫了! 创意代码表白 简单分享一下如何利用python画爱心表白 以程序员的方式撒狗粮,专业浪漫,值得拥有! 代码演示 # 导入海龟包 import turtle ...

  9. 利用python画梯形图案例

    利用python画梯形图案例 import matplotlib.pyplot as plt import numpy as npdef mystep(x,y, ax=None, where='pre ...

最新文章

  1. 北电ERS1600,8300,8600交换机的基本技术-第六章 二层冗余技术(MLT,SMLT,IST)
  2. 源码时代php中级项目,PHP学科项目评比圆满结束
  3. c#与java之比较(转自Jack.Wang's home)
  4. Global.asax 文件
  5. 2019春第一课程设计报告
  6. LeetCode(1009)——十进制整数的反码(JavaScript)
  7. MySQL密码忘记,怎么办?
  8. 自动化用例设计原则+web自动化框架
  9. Co-Fusion: Real-time Segmentation, Tracking and Fusion of Multiple Objects
  10. linux tomcat catalina.out 乱码,Tomcat输出日志乱码解决
  11. 信息安全管理体系(ISMS)
  12. 安装Go语言开发工具
  13. 用matlab抽样定理验证,实验一 MATLAB验证抽样定理
  14. Git GUI中文乱码问题解决方法
  15. 爬虫:Iron WebScraper 2022 for NET
  16. 媒体直播——直播现场 一个信号源多平台同步
  17. [论文解读]微信看一看实时Look-alike推荐算法
  18. 深度学习-扩张卷积(dilated convolution)
  19. 无线路由登不上服务器怎么办,登录不了无线路由器的管理界面怎么办?
  20. 音乐对计算机专业的影响,计算机网络技术对音乐发展的影响探讨

热门文章

  1. mysql distinct field_mysql – 如何提高COUNT(DISTINCT field1)的性能… GROUP BY field2?
  2. CTF中关于pwn题如何加载目标libc的方法
  3. 一个简单易用的RatingBar
  4. 项目任务管理工具GoodTask for Mac
  5. 飞行堡垒FX50JX加固态
  6. 【QQ界面展示-设置消息正文的背景图 Objective-C语言】
  7. OC 技术 (不需要UniversalLink)第三方微信(登录,分享,支付)详解(手动集成)(视频教学)
  8. sml完整形式_DVD的完整形式是什么?
  9. 画小猪佩奇之python实现
  10. 使用AlarmManager实现定时提醒,解决锁屏后无法正常唤醒CPU和屏幕的问题