Show pyramids in Python

Introduction

I decided to learn Python and studied for about 10 hours based on the reference books. As the output, I created a pyramid in Python.

environment

Execution result

> python pyramid.py
Please input number => 6
     *
    ***
   *****
  *******
 *********
***********

code

pyramid.py


num=int(input('Please input number => '))
spc=' '*max(0,num-1)

for i in range(1,2*num,2):
    ast='*'*max(1,i)
    out=spc+ast
    print(out)
    spc=spc.replace(' ','',1)

Code description

Use the input function to assign the value entered from the keyboard to the num variable. At this time, the acquired value will be str type, so leave it as int type for later.

Substitute spaces for spc variables.

We will display the pyramid using the for statement. Add 1 to 2 with the range function. As a result, * will increase to 1, 3, 5, and so on. Remove the whitespace one by one at the end of the loop.

(It may be possible to include what to do when something other than numbers is entered using try or expect.)

Summary

By creating this program

--input function

I understand.

Recommended Posts

Show pyramids in Python
Show decimal point in Python
[Python] Show multiple windows in Tkinter
Quadtree in Python --2
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Edit fonts in Python
Singleton pattern in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python
Key input in Python
Use config.ini in Python
Daily AtCoder # 33 in Python
Solve ABC168D in Python