星号三角形Python(带输入)(Asterisk Triangle Python (with input))

我正在做一个关于python 3的初学者课程,并且必须形成一个星号三角形,输出如下。 星号三角形格式

到目前为止,我的尝试如下:

def printRow(c, length) :

line = c * length

print(line)

myLen = 0

stars ="*"

n = myLen-1

spaces = (' '*n)

myLen = int(input("Enter number to make triangle: "))

if myLen<=0 :

print("The value you entered is too small to display a triangle")

elif myLen>=40 :

print("the value you entered is too big to display on a shell window")

while myLen>0 :

print(spaces, stars, myLen)

myLen = myLen-1

从这一刻起我很失落,所以任何帮助都会受到赞赏。

I'm doing a beginners course on python 3 and have to form a asterisk triangle that outputs like the following.Asterisk triangle format

My attempt so far looks as follows:

def printRow(c, length) :

line = c * length

print(line)

myLen = 0

stars ="*"

n = myLen-1

spaces = (' '*n)

myLen = int(input("Enter number to make triangle: "))

if myLen<=0 :

print("The value you entered is too small to display a triangle")

elif myLen>=40 :

print("the value you entered is too big to display on a shell window")

while myLen>0 :

print(spaces, stars, myLen)

myLen = myLen-1

From this point I was quite lost, so any help would be appreciated.

原文:https://stackoverflow.com/questions/40308584

更新时间:2019-11-08 11:11

最满意答案

正如Jeff L.提到的那样,你没有调用你的函数,所以你确实打印了一个空格,一个星,然后是myLen的新值。

关于实际问题,让我们尝试从右到左逐行绘制。 首先计算一个空格的数量,以及一行的星数。 打印出来,转到下一行。

见下面的代码:

space = ' ';

star = '*';

size = int(input("Enter number to make triangle: \n"))

def printRow(current_row, max_row) :

line = space * (max_row - current_row) + star * current_row;

print(line)

if size<=0 :

print("The value you entered is too small to display a triangle")

elif size>=40 :

print("the value you entered is too big to display on a shell window")

for i in range(1, size + 1) :

printRow(i, size);

As Jeff L. mentionned you are not calling your function, so you are indeed printing one space, one star and then the new value of myLen.

Regarding the actual problem, lets try to draw line by line, from right to left. First compute the number of spaces, and the number of stars for a row. Print it, go to next row.

See code bellow :

space = ' ';

star = '*';

size = int(input("Enter number to make triangle: \n"))

def printRow(current_row, max_row) :

line = space * (max_row - current_row) + star * current_row;

print(line)

if size<=0 :

print("The value you entered is too small to display a triangle")

elif size>=40 :

print("the value you entered is too big to display on a shell window")

for i in range(1, size + 1) :

printRow(i, size);

2016-10-28

相关问答

acos以弧度为单位返回一边,所以你必须将它转换为六十度,为此你必须乘以180/π 。 我们也知道内角的总和是180,所以第三个角是180-AB 。 另一个问题是必须通过绘制的角度,默认情况下从右向左绘制前进,然后你必须旋转180-A,前进c,旋转180-B并前进b a = 70

b = 60

c_pwr = a**2 + b**2

c = math.sqrt(c_pwr)

print("Langste zijde is: ", c)

#Angles

A = math.acos((b**2

...

您正在将输入就地转换为int,然后将其丢弃。 您应该将这些行更改为: length1 = int(raw_input('enter side 1\n'))

length2 = int(raw_input('enter side 2\n'))

length3 = int(raw_input('enter side 3\n'))

您的代码中还有一些其他语法错误。 更正如下: def triangle_check(l1,l2,l3):

if (l1>l2+l3) or (l2>l1+l3) o

...

def print_triangle(length):

for i in range(1, length+1):

print('{0:>{length}}'.format('*'*i, length=length))

用法: >>> print_triangle(5)

*

**

***

****

*****

为了分解它,这是使用字符串格式化迷你语言 : '{0:>{length}}'

第一个开始括号索引到第一个参数,而:表示格式规范将遵循它。

...

每次浏览一次:虽然通配符导入通常被忽略,但它是更常用的语法 from turtle import *

from math import *

import math

你说你想输入实数; 在Python中,这些数字是类float ,所以这段代码应该读取 a = float(input("enter the value for a: "))

b = float(input("enter the value for b: "))

c = float(input("enter the value for

...

调用turtle.fillcolor()时,您没有使用颜色: 将color_triangle函数更新为: def color_triangle(side_length, color):

turtle.fillcolor(color) # See here! Actually set the fill color!

turtle.begin_fill()

draw_triangle(size)

turtle.end_fill()

请注意fillcolor()的文档

...

我确定那里有非常好的解决方案,但如果你正在寻找一些初级水平的东西,请查看这个 n = 5 #depth of the pascal tree

pascal = []

for x in range(n):

if x == 0:

help_list = [1]

pascal.append(help_list)

continue

if x==1:

help_list = [1,1]

...

首先你的循环都关闭了; 您正在为i和j分配值而不使用它们。 其次,第一个循环是无用的。 如果您输入3个字符,它将重复该块3次,但变量triangle_height在第一次通过时减少为0,因此在下一次迭代时不会打印任何内容。 只需删除此行即可 第三:你说你需要反转三角形,所以,不要减少triangle_height ,而是在for循环中使用你为j的值而忘记减少变量。 由于范围从0开始计数,因此需要在print语句中为其添加1: triangle_char = input('Enter a chara

...

关键问题是你对三角不等式的检验: if (side1 + side2 > side3) or (side1 + side3 > side2) or (side1 + side3 > side2):

你的逻辑是错误的:你需要所有这三个都是真的,形成一个三角形,而不仅仅是一个。 if (side1 + side2 > side3) and (side1 + side3 > side2) and (side2 + side3 > side1):

# Note the change in

...

正如Jeff L.提到的那样,你没有调用你的函数,所以你确实打印了一个空格,一个星,然后是myLen的新值。 关于实际问题,让我们尝试从右到左逐行绘制。 首先计算一个空格的数量,以及一行的星数。 打印出来,转到下一行。 见下面的代码: space = ' ';

star = '*';

size = int(input("Enter number to make triangle: \n"))

def printRow(current_row, max_row) :

line = spa

...

我早些时候已经回答了同样的问题。 请记住,您提到的所有函数都不是recursive函数。 您可以在此处阅读有关递归的更多信息。 这是我在这里提到的例子。 Python 3.X def asterix_triangle(i, t=0):

if i == 0:

return 0

else:

print(' ' * ( i + 1 ) + '*' * ( t * 2 + 1 ))

return asterix_triangle( i - 1

...

python输出星号等腰三角形_星号三角形Python(带输入)(Asterisk Triangle Python (with input))...相关推荐

  1. python 输出一个 5*5的 三角形_GitHub标星3W+,80个Python案例,带你轻松玩转Python学习!...

    在Python学习过程中,案例是我们绝对绕不开的一部分.它不光能够帮助我们加深对基础知识的理解,也能进一步提升我们的编程能力. 今天给大家整理的这份80份Python精选案例,来自GitHub热门项目 ...

  2. python 输出纯音频_提取视频中的音频python三行程序搞定

    写在开头 身处数据爆炸增长的时代,各种各样的数据都飞速增长,视频数据也不例外.我们可以使用 python 来提取视频中的音频,而这仅仅需要安装一个体量很小的python包,然后执行三行程序! 语音数据 ...

  3. python输出文本居中_#python PIL ImageDraw text 文本居中#

    python pip pil有什么东西 你所问的问题实是属1.先参考[教程]Python中的内置的和方的模块搞懂PIL是属于第三方Python模块2.再参考:[待完善][总结]Python安装第三方的 ...

  4. 怎么利用python输出星座符号_怎么利用python输出星座

    怎么利用python输出星座?下面给大家带来具体方法: 思路: 1.定义一个get_constellation(month,date)函数,来获取出生日期. 2.创建一个dates和constella ...

  5. python输出列表元素_在Python中分别打印列表中的每一个元素方法

    在Python中分别打印列表中的每一个元素方法 更新时间:2018年11月07日 15:12:03 作者:wintersshi 今天小编就为大家分享一篇在Python中分别打印列表中的每一个元素方法, ...

  6. python输出最大值教程_实例讲解Python中整数的最大值输出

    在Python中可以存储很大的值,如下面的Python示例程序: x = 10000000000000000000000000000000000000000000; x = x + 1 print ( ...

  7. python输出word内容_使用python-docx生成Word文档

    学会来使用python操作数据表和PDF,今天我们尝试下使用python操作Word文档. 首先是安装python-docx:(centos环境) pip install python-docx 基本 ...

  8. python 进程生命周期_计算客户生命周期价值的python解决方案

    python 进程生命周期 By Lisa Cohen, Zhining Deng, Shijing Fang, and Ron Sielinski 由丽莎·科恩,志宁邓,石井方和罗恩Sielinsk ...

  9. python批量命名教程_《自拍教程69》Python 批量重命名音频文件,AV专家必备!

    本篇主要学习如何Python自定义模块并调用该模块,并重点介绍Python正则表达式的强大的文本处理能力. 案例故事:任何一款终端产品只要涉及音频输出,就肯定涉及音频的解码, 作为一名专业的AV (A ...

最新文章

  1. mysql内置的变量,MySQL服务器模式及相关内置变量
  2. jQuery入门[2]-选择器[转]
  3. 大学c语言编程模板,c语言编程模板
  4. Kafka启动报错:Timed out waiting for connection while in state: CONNECTING
  5. Hadoop完全分布式部署
  6. 2012浙大878计算机专业基础综合大题答案解析
  7. jQuery中click事件多次触发解决方案
  8. Bailian4117 简单的整数划分问题【整数划分+记忆化递归】
  9. matlab的三维数组(三维矩阵)
  10. i3 7100黑苹果_教你用2000多块装一台黑苹果主机 玩吃鸡剪视频毫无压力
  11. easydarwin php,EasyDarwin返回401 Unauthorized解决方法
  12. 10.14、驱动开发 -- input子系统
  13. 技巧:如何提高git下载速度
  14. 国庆回家记之2017
  15. sql date_format用法
  16. [计算机网络]计算机网络发展历程,osi7层模型,报文交换
  17. VUE使用docxtemplater导出word(带图片)
  18. 软件项目延期,怎么办?
  19. 【Unity入门】24.碰撞检测
  20. python足球大数据分析_使用Python抓取欧洲足球联赛数据进行大数据分析

热门文章

  1. 使用ScriptX.cab控件
  2. android简历!剖析Android开发未来的出路在哪里,Android篇
  3. Delphi菜单栏背景色-转载收藏学习
  4. git 更新密码之后出现异常(雾)
  5. 【目标检测】1、基础内容
  6. Opencv Python:图片与视频互转
  7. adb shell bugreport分析
  8. 图形学进阶——移动端TB(D)R架构基础
  9. 系统分析师易错题整理
  10. 《Python数据分析基础教程:NumPy学习指南(第2版)》笔记1:第一章 NumPy快速入门