์ƒˆ์†Œ์‹

Languages/Python

[Python] up down๊ฒŒ์ž„, ๊ทธ์™ธ ์—ฐ์Šต ์ฝ”๋“œ

  • -
๋ฐ˜์‘ํ˜•

ํŒŒ์ด์ฌ์€ ๋‚˜์ค‘์— ์ฝ”๋“œ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์“ฐ๊ฒŒ ๊ฐ„๋‹จํ•œ ๋ฌธ๋ฒ•์„

๊ฐœ์ธ์ ์œผ๋กœ ๊ณต๋ถ€ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 


 

1. up down ๊ฒŒ์ž„

import random t = 0 #์‹œ๋„ ํšŸ์ˆ˜ guess = 0 #์ž…๋ ฅ๋ฐ›๋Š”๊ฐ’ answer = random.randint(1,100) #๋žœ๋คํ•œ ์ˆ˜ print(answer) while 1: print("๊ฐ’์„ ์ž…๋ ฅ ํ•˜์„ธ์š”.") guess = input() t = t+1 if int(guess) < int(answer): print("๋‹˜ ์ˆ˜๊ฐ€ ๋” ๋‚ฎ์Œ.") else: if int(guess) > int(answer): print("๋‹˜ ์ˆ˜๊ฐ€ ๋” ๋†’์Œ") else: print("์ •๋‹ต~ ์ง์ง์ง") print("์‹œ๋„ ํšŸ์ˆ˜๋Š” %d ๋ฒˆ ์ž…๋‹ˆ๋‹ค."%t) break

 

2. ๊ตฌ๊ตฌ๋‹จ ์˜ˆ์˜๊ฒŒ ์งœ๊ธฐ

for i in range(1,10): for j in range(2,10): print('%3d'%(i*j),end='') #3d๋Š” 3์ž๋ฆฌ๋ž€ ๋œป ์ฆ‰ ํ•œ์นธ์ด ๋„์–ด์ ธ์„œ ๋‚˜์˜ด(2์ž๋ฆฌ ์ˆ˜์ด๋ฏ€๋กœ) print() #ํ•œ์ค„๋„๊ธฐ

 

2-1. while ๋ฌธ๋งŒ ์‚ฌ์šฉํ•ด์„œ ๊ตฌ๊ตฌ๋‹จ์งœ๊ธฐ

num = 1 while num < 9: num = num+1 num2 = 0 while num2 < 9: num2 = num2+1 print("%d * %d = %d" %(num,num2,num*num2)) print("\n")

 

3. ์ผ์ • ์ด์ƒ์˜ ์ˆซ์ž๋งŒ ์ถœ๋ ฅ(continue)

marks=[90,25,67,80,45] num = 0 for mark in marks: num = num+1 if mark < 60: continue #์ด์–ดํ•˜๊ธฐ else: print("%d !your good"%num) sum =0 for i in range(0,11): sum += i print(sum)

 

4. dict

a=[(1,2),(3,4),(5,6)] for (o,t) in a: print(o+t)

 

๋ฐ˜์‘ํ˜•
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.