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.
> python pyramid.py
Please input number => 6
*
***
*****
*******
*********
***********
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)
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.)
By creating this program
--input function
I understand.
Recommended Posts