Numerical patterns in Python3 [duplicate]

2024/10/15 6:18:16

I'm fairly new to programming, i have to start learning it for Uni.

I have to make a pattern as follow:

5 4 3 2 1
4 3 2 1
3 2 1
2 1
1

I have found ample examples of code for these patterns, just not for mine.

I cannot seem to get the numbers to line up vertically, only underneath each other:

5
4
3
2
14
3
2
1

what am i Missing ? I can't seem to find the exact function that other people are using to make their code act like this.


# Question 4.
import randomnum1 = random.choice([5, 6, 7, 8, 9, 10])def print_triangle():for row in range(num1, 0, -1):for space in range(num1 - row):print ('')for col in range(row, 0, -1):print (col)print_triangle()

Answer

Edit by Ducky:

import randomnum1 = random.choice([5, 6, 7, 8, 9, 10])def print_triangle():
for row in range(num1, 0, -1):for num in range(row, 0, -1):print(str(num) + " ", end="")print()print_triangle()

My answer:

def print_triangle():for row in range(10, 4, -1):for num in range(row, 0, -1):print(str(num) + " ", end="")print()print_triangle()

Output:

10 9 8 7 6 5 4 3 2 1 
9 8 7 6 5 4 3 2 1 
8 7 6 5 4 3 2 1 
7 6 5 4 3 2 1 
6 5 4 3 2 1 
5 4 3 2 1 

Or use:

def print_triangle(max_val, min_val):for row in range(max_val, min_val - 1, -1):for num in range(row, 0, -1):print(str(num) + " ", end="")print()print_triangle(10, 5)
https://en.xdnf.cn/q/117864.html

Related Q&A

setsockopt before connect for reactor.connectTCP

I have a small python client which needs a setsockopt after create_socket, but before connect. The non-twisted python code is as follows. How can this be expressed in a twisted environment?create_sock…

Manage quotation marks in XPath (lxml)

I want to extract web elements from the table MANUFACTURING AT A GLANCE in the given website. But the name of the row has (single quote). This is interfering with my syntax. How do I overcome this iss…

exception capture in threads and parent

How do you nicely capture exceptions in Python threads?If you have a threaded python app sys.excepthook does not capture errors in children.When a child raises an exception the parent will continue to…

Writing a program that compares 2 numbers in 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…

Run Python3 without activating the virtual environment

My objective is to run Python 3 code on the AWS Lambda Service, which currently only supports Python 2.7. These are the steps I have done.Since I work on a Mac, setup a docker image similar to the AWS …

Matplotlib functions in tkinter

This is my first python project so I understand that this problem may seem a bit stupid. I am trying to create a Mandelbrot renderer. I am piecing code together from tutorials and code that I understan…

sudo su user -c with arguments not working

I am trying to execute command from python as another "user" with:command = "sudo su user -c player --standard=1 -o 2" subprocess.Popen(command.split(), shell=False, stdin=None, std…

Grouping data on column value

Hi I have data (in excel and text file as well) like C1 C2 C31 p a1 q b2 r c2 s dAnd I want the output like:C1 C2 C31 p,q a,b2 r,s c,dHow can I group the data…

Memory Error Python Processing Large File Line by Line

I am trying to concatenate model output files, the model run was broken up in 5 and each output corresponds to one of those partial run, due to the way the software outputs to file it start relabelling…

python assign literal value of a dictionary to key of another dictionary

I am trying to form a web payload for a particular request body but unable to get it right. What I need is to pass my body data as below data={file-data:{"key1": "3","key2&quo…