{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 5일차 \n", "5 명이 한 팀이 되어 문제를 해결한다.\n", "\n", "# 조건부\n", "많은 결정들을 내리기 전 다양한 조건들을 비교합니다. 컴퓨터에게 조건을 비교 후 명령의 실행 여부를 결정하도록 하기 위해서는 ```if``` 명령을 사용해야 합니다. 이 명령은 조건을 판단합니다. 조건이 참이 아니면 실행해야 하는 경우 ```else``` 명령으로 선택할 수 있습니다. 다음의 예를 봅시다." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "number = int(input(\"if you enter a number larger than 5, I will give you 5\"))\n", "if (number > 5):\n", " print(\"You entered a number larger than 5, I will give you 5\")\n", "else:\n", " print(\"You entered a number smaller than 5, I will give you none\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 문제 1\n", "3개의 주사위를 던져서 모두 같은 수가 나오면 triple, 2개만 같은 수가 나오면 pair, 같은 수가 나오지 않으면 none이라고 출력하는 순서도를 작성하기 (힌트: ```import random; random.randint(1,6)```). 순서도의 코드를 작성하시오" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1회차 4\n", "2회차 3\n", "3회차 1\n", "4회차 2\n", "5회차 4\n", "6회차 5\n" ] } ], "source": [ "# 참고 코드\n", "import random\n", "\n", "print(\"1회차\", random.randint(1, 6))\n", "print(\"2회차\", random.randint(1, 6))\n", "print(\"3회차\", random.randint(1, 6))\n", "print(\"4회차\", random.randint(1, 6))\n", "print(\"5회차\", random.randint(1, 6))\n", "print(\"6회차\", random.randint(1, 6))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 문제 2\n", "섭씨와 화씨를 계산하는 공식은 다음과 같다. ```C = (F-32) x 5/9, F =(C x 9/5) + 32``` 섭씨(옵션 1) 또는 화씨(옵션 2)를 사용자가 선택하고 온도를 제시하면 화씨 또는 섭씨를 출력하는 프로그램의 순서도와 코드를 작성하시오." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your code here\n", "\n", "print(\"Enter 1 for converting celcius\")\n", "print(\"Enter 2 for converting Farenheit\")\n", "choice = int(input(\"1 or 2:\"))\n", "# 옵션 1\n", "\n", "# 옵션 2\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 문제 3\n", "해리포터 1편에 나오는 sorting hat의 동작을 모사하는 프로그램의 순서도와 코드를 작성해보자. Gryfindor는 1, Hufflepuff는 2, Ravenclaw는 3, Slytherin는 4라고 가정하자. 사용자가 입력하는 값이 아닌 1부터 4까지의 임의의 숫자를 자동생성하도록하여 실행할 때마다 다른 결과가 나오게 하자(문제 1에서 사용한 명령 사용). 10명이 sorting hat으로부터 결정을 기다리고 있다고 가정하자." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your code here\n", "\n", "for person in range(10):\n", " print(\"The decision is\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 문제 4\n", "다음 두 프로그램을 실행하기 전 동작을 예상해보자. 한 줄 씩 해석해보면서, 결과를 예측해보고, 자신의 예측과 같은지 확인해보자. 처음 보는 함수의 경우 검색을 통해 이해해보자. \n", "\n", "아래의 코드 1번을 수정하여 20-04시까지는 night, 05-11시까지는 morning, 12-15시까지는 noon, 16-19시까지는 afternoon이라고 출력되도록 해보자." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 1번\n", "import random\n", "\n", "morningTime = random.randint(1,12)\n", "print(\"Good morning it is exactly ”, morningTime, \" o'clock.\")\n", "\n", "sunny = random.choice([True, False])\n", "if sunny==True:\n", " print (\"It is sunny outside.\")\n", "else:\n", " print (\"It is rather cloudy today.\")\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 2번\n", "thisYear = 2018 \n", "print(\"We are in \" + str(thisYear))\n", "age = int(input(\"How old are you?\"))\n", "print(\"You are \", age, \" years old.\")\n", " \n", "for i in range(0, 36):\n", " thisYear += 1\n", " age += 1\n", " print(\"In \", this year, \" you will be \", age, \" years old.\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 문제 5\n", "1 부터 100 사이의 값을 사용자가 찾는 프로그램을 작성하자. ```While```문 을 다룰 때도 이 프로그램을 사용할 예정이다. 동작하는 방식은 다음과 같다. 사용자가 입력한 값이 프로그램에서 정한 값보다 큰지 또는 작은지를 알려주고 다시 사용자에게 값을 입력하도록 요청한다. 정한 값과 같다면 축하메시지를 출력한다. \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random\n", "\n", "answer = random.randint(1, 100)\n", "# your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 숙제\n", "\n", "매장에서 판매하는 물건의 가격은 다음과 같다. \n", "* 커피 700원\n", "* 홍차 600원\n", "* 콜라 400원\n", "* 보리차 300원\n", "* 생수 200원\n", "사용자에게 음료의 종류와 가격을 보여주고, 구매할 음료의 개수와 음료의 이름을 물어보도록하자. 사용자가 입력한 값들을 기반으로 전체 금액을 표시하는 프로그램을 만들어 보자. \n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }