3-1/Python 과제&실습
9주차-실습&과제
Donghun Kang
2024. 5. 21. 17:06

def arithmetic(x,y):
print(x,"+",y,"=",x+y)
print(x,"-",y,"=",x-y)
print(x,"*",y,"=",x*y)
print(x,"/",y,"=",x/y)
arithmetic(5,3)
print()

def show_star(x):
for i in range(x):
print("*",end='')
n = int(input("입력: "))
show_star(n)
print()

def show_gugudan(dan):
for i in range(1,10):
print(n,"*",i,"=",dan*i)
n = int(input("단: "))
show_gugudan(n)
print()

def draw_rect(x):
for i in range(x):
for j in range(x):
print("*",end='')
print()
n = int(input("입력: "))
draw_rect(n)
print()

def square(n):
x = n**2
return x
ans = square(3)
print(ans)
print()

def get_max(x,y):
if x>y:
max = x
else:
max = y
return max
ans = get_max(3,5)
print(ans)
print()

def get_sum(start, end):
s = 0
for i in range(start,end+1):
s+=i
return s
print(get_sum(1,10))
print()

def factorial(n):
s = 1
for i in range(1,n+1):
s *= i
return s
print(factorial(5))
print()

def sum(x,y):
plus = x+y
return plus
def diff(x,y):
if x>y:
return x-y
else:
return y-x
n1 = int(input("정수n1:"))
n2 = int(input("정수n2:"))
print("두수의 합:",sum(n1,n2))
print("두수의 차:",diff(n1,n2))
print()

def big_small(x,y):
if x>y:
return x,y
else:
return y,x
n1 = int(input("정수n1:"))
n2 = int(input("정수n2:"))
big,small = big_small(n1,n2)
print("큰 수:",big)
print("작은 수:",small)
print()

def cal_circle(r):
a = r*r*3.14
c = 2*r*3.14
return (a,c)
radius = float(input("원의 반지름:"))
(area,circum) = cal_circle(radius)
print("원의 넓이:",area)
print("원의 둘레:",circum)
print()

def count_words(text):
words = text.split()
return len(words)
def count_sentences(text):
sentence_end = ['.', '!', '?']
count = 0
for char in text:
if char in sentence_end:
count += 1
return count
def most_common_letter(text):
text = text.lower()
common_letter = {}
for char in text:
if char.isalpha():
if char in common_letter:
common_letter[char] += 1
else:
common_letter[char] = 1
max_count = max(common_letter.values())
most_common = [char for char, count in common_letter.items() if count == max_count]
return most_common
def average_word_length(text):
clean_text = ''.join(char for char in text if char.isalnum() or char.isspace())
words = clean_text.split()
total_length = sum(len(word) for word in words)
return round(total_length / len(words), 2)
text = input("텍스트를 입력하세요:")
print("텍스트 분석결과!!")
print("단어 수:",count_words(text))
print("문장 수:",count_sentences(text))
print("가장 많이 사용된 알파벳:",most_common_letter(text))
print("평균 단어 길이:",average_word_length(text))
#"Hello! Welcome to the world of Python. Are you ready to explore?"
#가장 많이 사용된 알파벳이 동률이면 이에 해당하는 알파벳을 모두 출력
#특수문자는 제외하고 계산한다
#소수점 둘째자리까지