MapleStory Finger Point

๐Ÿ’Ž Python/๐Ÿ“šํŒŒ์ด์ฌ for Beginner (2ํŒ)

ch 5 ์—ฐ์Šต๋ฌธ์ œ ํ’€์ด - ํŒŒ์ด์ฌ for Beginner (2ํŒ)

HYEJU01 2024. 3. 20. 04:12

1) ๋นˆ์นธ์„ ์ฑ„์šฐ์‹œ์˜ค

1. ์‹œ์ž‘๊ฐ’

2. ๋๊ฐ’ + 1

3. ์ฆ๊ฐ๊ฐ’

 

2) ๋ช‡๋ฒˆ ์ถœ๋ ฅ๋˜๋Š”๊ฐ€?

for i in range(1,100,1) : 
	print("~~")

1๋ถ€ํ„ฐ 99๋ฒˆ๊นŒ์ง€ ํ•œ๋ฒˆ์”ฉ ์ถœ๋ ฅ๋˜๋‹ˆ,

์ด 99๋ฒˆ ์ถœ๋ ฅ๋œ๋‹ค.

 

3) 100๋ถ€ํ„ฐ 200๊นŒ์ง€ 2์˜ ๋ฐฐ์ˆ˜๋งŒ ๋”ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ ?

for i in range(100,201,2)

=> 2์”ฉ ์ฆ๊ฐํ•˜์—ฌ 2์˜๋ฐฐ์ˆ˜๋งŒ 100๋ถ€ํ„ฐ 200๊นŒ์ง€ ๋”ํ•˜๋Š” for ๋ฌธ

 

4) ๋ช‡๋ฒˆ ์ถœ๋ ฅ๋˜๋Š”๊ฐ€?

i๊ฐ€ 1์ผ๋•Œ k ๊ฐ€ 9๋ฒˆ ์ถœ๋ ฅ๋œ๋‹ค.

i ๋Š” 1~9๊นŒ์ง€์ด๋ฏ€๋กœ 9x9 ๋กœ 81๋ฒˆ ์ถœ๋ ฅ๋œ๋‹ค.

 

5) ๊ตฌ๊ตฌ๋‹จ ๋‹จ์„ ์ž…๋ ฅํ•˜๋ฉด ๊ฑฐ๊พธ๋กœ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ

i,sum = 0, 0
dan = 0
dan = int(input("๋‹จ์„ ์ž…๋ ฅํ•˜์„ธ์š” : "))
for i in range(9, 0, -1):
    sum = dan * i
    print ("๊ตฌ๊ตฌ๋‹จ %d  x %d  = %d" % (dan,i,sum))

 

 

 

6) for ๋ฌธ 0~100์‚ฌ์ด์— ์žˆ๋Š” ์ง์ˆ˜์˜ ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ while ๋ฌธ์œผ๋กœ ๊ณ ์น˜์‹œ์˜ค

 

i,sum = 0, 0

while i < 101 :
    sum += i
    i += 2
print ("0~100 ์ง์ˆ˜์˜ ํ•ฉ : %d" % (sum))

 

 

7) 1๊ณผ 1000 ์‚ฌ์ด์— ์žˆ๋Š” ํ™€์ˆ˜์˜ ํ•ฉ ์ค‘์— ์ตœ์ดˆ๋กœ ํ•ฉ์ด 1000์ด ๋„˜์–ด๊ฐ€๋Š” ์ˆซ์ž ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ

ํ™€์ˆ˜๋ฉด 0๋ถ€ํ„ฐ 2์”ฉ ๋”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

i,sum = 1, 0

for i in range (1, 1001, 2) :
    sum += i
    if (sum >= 1000) :
        break
    
print ("์ •๋‹ต : %d" % (i))

 

8) ์ž…๋ ฅํ•œ ์ˆซ์ž์˜ 2๋ฐฐ๋งŒํผ ๋ณ„์„ ์ถœ๋ ฅํ•˜๋„๋ก ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ

๊ธฐ์กด์ฝ”๋“œ์—์„œ ๋ณ„ ์ฐ๊ธฐ๋ฅผ ๋‹ด๋‹นํ•˜๋˜ ์ฝ”๋“œ๋Š” while ๋ฌธ ์•ˆ ์ฒซ for๋ฌธ์ธ๋ฐ

์ด๋•Œ hName ๊ฐœ์ˆ˜๋งŒํผ ๋ณ„์„ ์ฐ๊ณ  ์žˆ๋‹ค. ์ฆ‰ 

numStr[0] ์— 1์ด ๋“ค์–ด์žˆ์—ˆ๋‹ค๋ฉด

hName= 1 ๋งŒํผ ๋ณ„์„ ์ฐ๋Š” ๊ฒƒ์ด๋‹ค.

int(ch) ์—์„œ * 2๋ฅผ ํ•ด์ฃผ๋ฉด ๋‘๋ฐฐ๋งŒํผ ๋ณ„์ด ์ฐ์–ด์ง„๋‹ค.

i,k,hName = 0,0,0
numStr, ch, hStr = "", "", ""



numStr = input("์ˆซ์ž๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ์ž…๋ ฅํ•˜์„ธ์š”")
print('')

i=0
ch=numStr[i]

while True :
    hName = int(ch) *2 ## hName = int(ch)##
    hStr = ""
    for k in range (0,hName) :
        hStr += "\u2605"
        k += 1

    print(hStr)

    i += 1
    if (i > len(numStr) - 1) :
        break


    ch = numStr[i]