Triangle of T in Python

2024/11/15 13:23:59

EDIT ** I can't multiply strings by an integer. Its for a homework and those were the instructions **

I need to do a triangle in python using for loops or while loops(mandatory). The final output should look like this.

T
TT
TTT
TTTT
TTTTT
TTTTTT
TTTTT
TTTT
TTT
TT
T

I already figure the firs part, that prints from the first line to the middle line, but i cant figure how to reverse my function.

def triangle(base):for length in range(base+1):for b in range(length):print("T",end="")print() 

What I tried to do was sustracting length like this.

def triangle(base):
for length in range(base+1):for b in range(length):print("T",end="")print()
for length in range(base):for b in range(length-1):print("T",end="")
print()

But only print this and I cant find the way to keep printing the "T" until it gets done:

T
TT
TTT
TTTT
TTTTT
TTTTTT
TTTTT
Answer

try the following

def triangle(base):for i in range(1,base+1):print(''.join('T' for o in range(i)))for i in reversed(range(1,base)  # note the lack of the +1 on baseprint(''.join('T' for o in range(i)))

Example

from shapes import triangle
triangle(3)
T
TT
TTT
TT
T
https://en.xdnf.cn/q/119656.html

Related Q&A

Finding prime project euler

Find the 10,001st prime number.I am trying to do Project Euler problems without using copying and pasting code I dont understand. I wrote code that finds whether a number is prime or not and am trying …

Splitting a python string

I have a string in python that I want to split in a very particular manner. I want to split it into a list containing each separate word, except for the case when a group of words are bordered by a par…

file modifiaction and manipulation

How would you scan a dir for a text file and read the text file by date modified, print it to screen having the script scan the directory every 5 seconds for a newer file creadted and prints it. Is it …

Get quantitative value for color on two-color scale

I have run a chemical test that produces a color depending on how much of a given chemical is in the sample. It is green if there is no chemical, and yellow if there is a saturating amount of chemical.…

how to save python session input and output [duplicate]

This question already has answers here:How to save a Python session, including input and output, as a text?(4 answers)Closed 2 years ago.All of the ways which discussed this question save the history …

flask_mysqldb Delete FROM variable table [duplicate]

This question already has answers here:Python sqlite3 parameterized drop table(1 answer)How do I use SQL parameters with python?(1 answer)Closed 6 years ago.So i use flask_mysqldb in a Flask(Python we…

Syntax Error at the end of a while loop

EDIT: This question was ask at the start of my learning process for python. The Syntax Error was produced by pythons IDLE with no trackback to speak of. This was the main cause of the problem and confu…

Creating an adjacency list class in Python

I was wondering how to create an adjacency list class Here is what I have so far:class AdjNode:def __init__(self, value):self.vertex = valueself.next = Noneclass Graph:def __init__(self):# Add edgesdef…

How can I separate a rust library and the pyo3 exported python extensions which wrap it

I have a rust library which provides useful functionality for use in other rust programs. Additionally I would like to provide this functionality as a python extension (using pyo3 and setuptools-rust, …

how do I count unique words of text files in specific directory with Python? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…