Nested Triangle in Python

2024/10/6 9:10:33

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*numnum -= 2check = 1
cont = 0
var = 1
for ii in range(6):tri2[ii] = ch2*checkcheck += 2if (ii >= 3):tri2[ii] = ch2*var + tri1[cont] + ch2*varcont += 1var += 2for i in tri1:print('{:^5}'.format(tri1[i]))for i in tri2:print('{:^11}'.format(tri2[i]))

Answer

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 +1tri_char = tri_chars[i%2]level_str[i] = level_str[i-1] + '\n' + tri_char * level_lengthreturn 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])
https://en.xdnf.cn/q/120190.html

Related Q&A

How to fix cannot open file Test.py: [Errno2] No such file or directory?

I tried to run a python script which does not exist in current folder, for exampleC:\>python Test.pypython:cant open file Test.py:[Errno2] No such file or directoryI have to specify the absolute pat…

Highlight cells in a column in google spreadsheet when the value above a threshold with python

Here is a simplified example of my codes and the screenshot of the results I want to get in google spreadsheet. I hope to either save the dataframe style to google spreadsheet as applying table style t…

cannot concatenate str and file objects : Python error

I have Following piece of code: for src_filename, src_code in src_dict.iteritems(): try: set.dependencies = subprocess.check_output(unifdef -s /home/c/maindir/folder/ +src_filename, shell=True) except…

Run python script from html button submit

i have a code input data to txt file :<form action="proses.php" method="post">Nomor Polisi : <br><input type="text" name="nopol"><br><…

Reading log files in python

I have a log file (it is named data.log) containing data that I would like to read and manipulate. The file is structured as follows:#Comment line 1 #Comment line 2 1.00000000,3.02502604,343260.6865…

Return more than one value in python function [duplicate]

This question already has answers here:How can I use `return` to get back multiple values from a loop? Can I put them in a list?(2 answers)Closed 1 year ago.I was trying to use *args with a for loop …

Python: Making a class to use complex numbers

I am currently trying to write a program that deals with complex numbers. I have to use classes and methods. I am trying to be able to add, subtract, multiply etc., complex numbers, as well as compare …

Return does not return anything in Spyder. It works fine with other IDE

I just moved to spyder for Python and the return function doesnt seem to work: def test():return 2 test()The IPython console is empty. If I use print instead of return it works fine. Any idea? I use p…

Error in goto module [Python]

Ok, let me start by saying I know that it is bad that I am using the goto module and I shouldnt be and blah blah blah. However, for this specific purpose I need it. Let me also say that I am new to Pyt…

How to scrape all product review from lazada in python

i currently working on web scraping of data from the lazada site using selenium in python: https://www.lazada.sg/products/loreal-paris-uv-perfect-even-complexion-sunscreen-spf50pa-30ml-i214861100-s325…