์ƒˆ์†Œ์‹

Languages/Python

[Python] ์•ผ๊ตฌ ๊ฒŒ์ž„ ์•Œ๊ณ ๋ฆฌ์ฆ˜/ ํ•™์  ๊ณ„์‚ฐ๊ธฐ

  • -
๋ฐ˜์‘ํ˜•

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

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

 


 

1. ์•ผ๊ตฌ ๊ฒŒ์ž„ 

import random
rand = []
you = "" #์‚ฌ์šฉ์ž ์ž…๋ ฅ ๊ฐ’์„ ๋ฌธ์žํ˜•ํƒœ๋กœ ๋ฐ›์•„์คŒ(z์™€ ๊ฐ™์€์—ญํ• )

#๋žœ๋ค ๊ฐ’ 3 ๊ฐœ ๋ฝ‘๊ณ  ์ค‘๋ณต ์ œ๊ฑฐ ํ•˜๊ธฐ
while len(rand) < 3:
    rand.append(random.randint(1,9)) #๋žœ๋ค
    rand = list(set(rand)) #์ค‘๋ณต์ œ๊ฑฐ

#๊ฒŒ์ž„ ๊ทœ์น™
computer = "".join(map(str,rand)) #computer๋Š” rand๋ผ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ถ™์—ฌ์„œ ๋ฌธ์žํ˜•ํƒœ๋กœ ๋‚˜ํƒ€๋‚ด์ฃผ๋Š” ์—ญํ• 
print("์ˆซ์ž ์•ผ๊ตฌ๊ฒŒ์ž„\n๊ทœ์น™ : 1~9 ์‚ฌ์ด์— ๊ฒน์น˜์ง€ ์•Š๋Š” ์ˆซ์ž 3๊ฐœ : %s" %computer,"\n")

#์‚ฌ์šฉ์ž ์ž…๋ ฅ ๊ตฌ๊ฐ„
num = 0
while you != computer:#์ •๋‹ต์ด๋ฉด ์ข…๋ฃŒ
    num = num +1
    you = input("3์ž๋ฆฌ ์ž…๋ ฅ : ")
    arr = 0; ball = 0; strike =0 # arr(๋žœ๋คํ•œ ๊ฐ’์˜ ์ธ๋ฑ์Šค)
    for i in you:
        if i == computer[arr]:
            strike = strike+1
        else:
            for j in computer:
                if i == j:
                    ball = ball+1
        arr = arr+1
    print("\t%d Strike, %d Ball"%(strike,ball))
print("-----------------------\n%d ๋ฒˆ๋งŒ์— ์ •๋‹ต !!!!"%num)






 

๊ฒฐ๊ณผ

 

 

2. ํ•™์  ๊ณ„์‚ฐ๊ธฐ

student = int(input("ํ•™์ƒ ์ˆ˜ ์ž…๋ ฅ : "))
subject = int(input("๊ณผ๋ชฉ ์ˆ˜ ์ž…๋ ฅ : "))
grade = []
a = 0; b = 0

for i in range(1,student+1):
    print("===== %d ๋ฒˆ ํ•™์ƒ =====" %i)
    for j in range(1,subject+1):
        s = int(input("%d ๋ฒˆ ๊ณผ๋ชฉ ์„ฑ์  ===> "%j))
        grade.append(s)
print("\n[๊ฒฐ๊ณผ]")
for i in range(1,student+1):
    sum = 0
    print("===== %d ๋ฒˆ ํ•™์ƒ =====" %i)
    for j in range(1,subject+1):
        print("๊ณผ๋ชฉ %d : %d " %(j,grade[a]))
        sum += grade[a]
        avg = sum/subject
        a = a+1
    print("ํ‰๊ท  : %.1f" %avg)
print("============ ๊ณผ๋ชฉ๋ณ„ ํ‰๊ท  ์„ฑ์  ============")
for i in range(1, subject+1):
    sum = 0
    for j in range(1, student+1):
        sum += grade[b]
        b = b + subject
    b=i
    avg = sum/student
    print("๊ณผ๋ชฉ %d ๋ฒˆ ํ‰๊ท  ์„ฑ์  : %.1f"%(i,avg))

๊ฒฐ๊ณผ

๋ฐ˜์‘ํ˜•
Contents

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

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