How to make a triangle of xs in python?

2024/7/4 15:37:42

How would I write a function that produces a triangle like this:

    xxxxxxxxxx
xxxxx

Let's say the function is def triangle(n), the bottom row would have n amount of x's

All I know how to do is make a box:

n = 5
for k in range(n):for j in range(n):print('x', end='')print()
Answer

Dude It's super easy:

def triangle(n):for i in range(1, n +1):print ' ' * (n - i) + 'x' * i

Or even:

def triangle(n):for i in range(1, n +1):print ('x' * i).rjust(n, ' ')

output for triangle(5):

xxxxxxxxxx
xxxxx

Dont just copy this code without comprehending it, try and learn how it works. Usually good ways to practice learning a programming language is trying different problems and seeing how you can solve it. I recommend this site, because i used it a lot when i first started programming.

And also, dont just post your homework or stuff like that if you dont know how to do it, only if you get stuck. First try thinking of lots of ways you think you can figure something out, and if you dont know how to do a specific task just look it up and learn from it.

https://en.xdnf.cn/q/120467.html

Related Q&A

Pip freeze --local

I am following a video tutorial and that guy did this:$ pip freeze --local > requirement.txt $ cat requirement.txtthis is to export all these packages with their versions in another project, but how…

How to optimize this Pandas code to run faster

I have this code to create a swarmplot from data from a DataFrame:df = pd.DataFrame({"Refined__Some_ID":some_id_list,"Refined_Age":age_list,"Name":name_list …

i was creating a REST api using flask and while i was about to test it on postman I saw that error

File "c:\Users\kally\rest\code\app.py", line 3, in <module>from flask_jwt import JWTFile "C:\Users\kally\AppData\Roaming\Python\Python310\site-packages\flask_jwt\__init__.py",…

Web scrape get drop-down menu data python

I am trying to get a list of all countries in the webpage https://www.nexmo.com/products/sms. I see the list is displayed in the drop-down. After inspecting the page, I tried the following code but I m…

TypeError(unsupported operand type(s) for ** or pow(): str and int,)

import mathA = input("Enter Wright in KG PLease :") B = input("Enter Height in Meters Please :")while (any(x.isalpha() for x in A)):print("No Letters Please")A = input(&qu…

How can I do assignment in a List Comprehension? [duplicate]

This question already has answers here:How can I do assignments in a list comprehension?(8 answers)Closed 1 year ago.Generally, whenever I do a for loop in python, I try to convert it into a list comp…

KeyError: column_name

I am writing a python code, it should read the values of columns but I am getting the KeyError: column_name error. Can anyone please tell me how to fix this issue. import numpy as np from sklearn.clust…

Find starting and ending indices of list chunks satisfying given condition

I am trying to find the start and stop indices of chunks of positive numbers in a list.cross = [7,5,8,0,0,0,0,2,5,8,0,0,0,0,8,7,9,3,0,0,0,3,2,1,4,5,0,0,0,7,5] For the given example input, the desired o…

How can I Scrape Business Email Contact with python?

this morning I wanted to create a little Software/Script in Python, it was 6am when I started and now Im about to become crazy because its 22pm and I have nothing that works.So basically, I want to do …

Python class that works as list of lists

Im trying to create a python class that can work as a list of lists. However, all Ive managed to develop so far is,class MyNestedList(list): ...Im aware that the above code will work as,my = MyNestedLi…