자판기
menus = ['콜라', '사이다', '생수', '커피']
prices = [1200, 1200, 600, 300]
위와 같이 메뉴와 가격이 있을 때, 사용자가 직접 돈과 메뉴를 입력해 만들어주는 프로젝트입니다.
아래 메뉴를 참고해주세요.
메뉴:
1 콜라 1000
2 사이다 1000
3 생수 600
4 커피 300
프로그램 동작 순서
menus = ['콜라', '사이다', '생수', '커피']
prices = [1200, 1200, 600, 300]
print('메뉴:')
for i, (# TODO) in enumerate(zip(# TODO)):
print(# TODO)
money = # TODO #돈을 투입하세요: 2000
choice = # TODO #메뉴를 선택하세요: 1
menu = #
price = #
if # TODO <= # TODO:
print('선택 메뉴:', menu) #선택 메뉴: 콜라
print('메뉴 가격:', price) #메뉴 가격: 1200
print('거스름 돈:', money - price) #거스름 돈: 800
else:
print('잔액이 부족합니다')
주사위 게임
주사위 게임 프로젝트입니다.
컴퓨터와 사용자의 이름을 설정 합니다.
주사위를 굴릴 수 있는 함수를 설정합니다.
while 문을 이용해서 연속으로 게임을 실행합니다.
import random
# 컴퓨터와 사용자 이름 설정
computer_name = "Computer"
user_name = input("사용자 이름을 입력하세요: ")
# 주사위 굴리기 함수
def roll_dice():
return random.randint(#TODO)
# 주사위 게임 실행
while True: # 무한 반복문
# 컴퓨터와 사용자 주사위 굴리기
computer_roll = #TODO
user_roll = #TODO
# 주사위 결과 출력
print(f"{computer_name}: {computer_roll}")
print(f"{user_name}: {user_roll}")
# 더 큰 수를 가진 사람 출력
if #TODO:
print(f"{computer_name} 승리!")
elif #TODO:
print(f"{user_name} 승리!")
else:
print("무승부!")
# 게임 종료 여부 확인
play_again = input("게임을 다시 시작하시겠습니까? (y/n): ")
if play_again.lower() != 'y':
break
단어 빈도수 계산기
문장을 입력하세요: 너랑 너랑 이리와봐
'너랑': 2회
'이리와봐': 1회
# 사용자로부터 텍스트 입력 받기
text = input("문장을 입력하세요: ")
# 단어 빈도수 계산
word_frequency = {}
words = #TODO
for word in words:
if word in #TODO:
#TODO += 1
else:
#TODO = 1
# 단어 빈도수 출력
for word, frequency in #TODO:
print(f"'{word}': {frequency}회")
자판기
solmenus = ['콜라', '사이다', '생수', '커피']
prices = [1200, 1200, 600, 300]
print('메뉴:')
for i, (menu, price) in enumerate(zip(menus, prices)):
print(i + 1, menu, price)
money = int(input('돈을 투입하세요: ')) #돈을 투입하세요: 2000
choice = int(input('메뉴를 선택하세요: ')) #메뉴를 선택하세요: 1
menu = menus[choice - 1]
price = prices[choice - 1]
if price <= money:
print('선택 메뉴:', menu) #선택 메뉴: 콜라
print('메뉴 가격:', price) #메뉴 가격: 1200
print('거스름 돈:', money - price) #거스름 돈: 800
else:
print('잔액이 부족합니다')
메뉴:
1 콜라 1200
2 사이다 1200
3 생수 600
4 커피 300
돈을 투입하세요: 2000
메뉴를 선택하세요: 2
선택 메뉴: 사이다
메뉴 가격: 1200
거스름 돈: 800
주사위 게임
solimport random
# 컴퓨터와 사용자 이름 설정
computer_name = "Computer"
user_name = input("사용자 이름을 입력하세요: ")
# 주사위 굴리기 함수
def roll_dice():
return random.randint(1, 6)
# 주사위 게임 실행
while True:
# 컴퓨터와 사용자 주사위 굴리기
computer_roll = roll_dice()
user_roll = roll_dice()
# 주사위 결과 출력
print(f"{computer_name}: {computer_roll}")
print(f"{user_name}: {user_roll}")
# 더 큰 수를 가진 사람 출력
if computer_roll > user_roll:
print(f"{computer_name} 승리!")
elif computer_roll < user_roll:
print(f"{user_name} 승리!")
else:
print("무승부!")
# 게임 종료 여부 확인
play_again = input("게임을 다시 시작하시겠습니까? (y/n): ")
if play_again.lower() != 'y':
break
사용자 이름을 입력하세요: ssoju
Computer: 4
ssoju: 2
Computer 승리!
게임을 다시 시작하시겠습니까? (y/n): y
Computer: 4
ssoju: 6
ssoju 승리!
게임을 다시 시작하시겠습니까? (y/n): y
Computer: 6
ssoju: 1
Computer 승리!
게임을 다시 시작하시겠습니까? (y/n): n
단어 빈도수 계산기
sol# 사용자로부터 텍스트 입력 받기
text = input("문장을 입력하세요: ")
# 단어 빈도수 계산
word_frequency = {}
words = text.split()
for word in words:
if word in word_frequency:
word_frequency[word] += 1
else:
word_frequency[word] = 1
# 단어 빈도수 출력
for word, frequency in word_frequency.items():
print(f"'{word}': {frequency}회")