Python中的嵌套三角形 [英] Nested Triangle in Python

查看:454
本文介绍了Python中的嵌套三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的奉献

在每个级别上,将前一个级别的完整三角形放置在一个额外的外部三角形中.应该要求用户输入要使用的两个字符和最里面的三角形的宽度,该宽度必须是奇数.除了测试否定输入之外,该功能还应该测试所提供的数字是否为奇数,如果不是,则显示适当的消息.

我需要打印3个三角形,但每个三角形都包含一个.它需要打印两个不同的字符(例如*-),并且用户必须指定最里面的三角形的长度,并且该长度必须为奇数.例子,

5个值的示例输出

好,让我解释一下我的方式,

每个三角形都应在字典中.

tri1 = {1:"*****", 2:"***", 3:"*"}
tri2 = {1:"..........", ...}

并且找不到我该如何处理用户输入?

如果输入5,

长度-5个单位,高度3个单位

长度-11个单位,高度6个单位

长度-23个单位,高度12个单位.

我怎么知道?逻辑是什么?

好吧,如果我愿意的话.我知道我应该将一个tringle放在另一个带有嵌套循环的三角形中,可以简单地迭代另一个字典,但是,我需要检查第二个字符的位置.

谢谢.

我的代码,

解决方案

可以使用以下简单函数创建字典:

def create_tri_dict(tri_chars, tri_height):
    level_str = {0:tri_chars[0]}

    for i in range(1,tri_height):
        level_length = i *2 +1
        tri_char = tri_chars[i%2]
        level_str[i] = level_str[i-1] + '\n' + tri_char * level_length
    return level_str

那么您程序的主要逻辑可能是:

tri_chars = input('Input triangle characters: ')
tri_length = int(input('Input triangle base length: '))
tri_height = (tri_length + 1)//2
if tri_length %2 == 0:
    raise Exception('Triangle base length not odd')
tri_dict = create_tri_dict(tri_chars, tri_length)

然后打印最后的3(?)三角形:

print(tri_dict[tri_height-2])
print(tri_dict[tri_height-1])
print(tri_dict[tri_height])

My assingment

At each level the complete triangle for the previous level is placed into an extra outer triangle. The user should be asked to input the two characters to be used and the width of the innermost triangle, which must be odd. In addition to the test for negative input the function should test whether the supplied number is odd and display an appropriate message if it is not.

I need to print 3 triangles but every one of them includes other. It needs to get printed with two different character(like *-) and the user have to specify the length of innermost triangle and which has to be an odd number. Example,

Example output for 5 value

Ok, let me explain my way,

Every triangle should be in dictionary.

tri1 = {1:"*****", 2:"***", 3:"*"}
tri2 = {1:"..........", ...}

And couldn't find how I can deal with user input?

If enter 5,

length - 5 unit, height 3 unit

length - 11 unit, height 6 unit

length - 23 unit, height 12 unit.

How can i know? What is the logic?

Ok lets say if I did. I understand I should put a tringle in another triangle with nested loop, can simply iterate it another dictionary but, I need to check second character's position.

Thanks in advance.

My code,

    ch1, ch2 = input("Please enter the characters you want to use: ")

num = int(input("Please specify the length of innermost triangle(only odd number): "))

if (num % 2 == 0) or (num < 3):
  print("Number can not be even, less then 3 and negative")

num2 = (2 * num) + 1
num3 = (2 * num2) +1
tri1 = {}
tri2 = {}
tri3 = {}

for i in range(3):
  tri1[i] = ch1*num
  num -= 2


check = 1
cont = 0
var = 1
for ii in range(6):
  tri2[ii] = ch2*check
  check += 2
  if (ii >= 3):
    tri2[ii] = ch2*var + tri1[cont] + ch2*var
    cont += 1
    var += 2

for i in tri1:
  print('{:^5}'.format(tri1[i]))

for i in tri2:
  print('{:^11}'.format(tri2[i]))

解决方案

The dictionary can be created using a simple function:

def create_tri_dict(tri_chars, tri_height):
    level_str = {0:tri_chars[0]}

    for i in range(1,tri_height):
        level_length = i *2 +1
        tri_char = tri_chars[i%2]
        level_str[i] = level_str[i-1] + '\n' + tri_char * level_length
    return level_str

Then the main logic of your program could be:

tri_chars = input('Input triangle characters: ')
tri_length = int(input('Input triangle base length: '))
tri_height = (tri_length + 1)//2
if tri_length %2 == 0:
    raise Exception('Triangle base length not odd')
tri_dict = create_tri_dict(tri_chars, tri_length)

Then to print the final 3(?) triangles:

print(tri_dict[tri_height-2])
print(tri_dict[tri_height-1])
print(tri_dict[tri_height])

这篇关于Python中的嵌套三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆