01_ํจ์๋
๊ณ์ฐ๊ธฐ ๋ง๋ค๊ธฐ
02_ํจ์ ์ ์ธ๊ณผ ํธ์ถ
๏ ์ค๋ ๋ ์จ๋ฅผ ์ถ๋ ฅํ๋ ํจ์๋ฅผ ์ ์ธํ๊ณ 3๋ฒ ํธ์ถํด๋ณด์.
๏ ์ ์ ๋ ๊ฐ๋ฅผ ์
๋ ฅํ๋ฉด ๊ณฑ์
๊ณผ ๋๋์
์ฐ์ฐ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ ํจ์๋ฅผ ๋ง๋ค๊ณ ํธ์ถํด๋ณด์.
03_ํจ์ ๋ด์์ ๋ ๋ค๋ฅธ ํจ์ ํธ์ถ
๏ ๋ค์๊ณผ ๊ฐ์ด ๊ตฌ๊ตฌ๋จ ์ถ๋ ฅ ํจ์๊ฐ ์ฐ์์ ์ผ๋ก ํธ์ถ๋๋๋ก ํจ์๋ฅผ ์ ์ธํด๋ณด์.
04_์ธ์์ ๋งค๊ฐ๋ณ์
๏ ๊ตญ์ด, ์์ด, ์ํ ์ ์๋ฅผ ์
๋ ฅ๋ฐ๊ณ , ์
๋ ฅ๋ฐ์ ์ ์๋ฅผ ์ด์ฉํด์ ์ด์ ๊ณผ ํ๊ท ์ ์ถ๋ ฅํ๋ ํจ์๋ฅผ ๋ง๋ค์ด ๋ณด์.
05_๋ฐ์ดํฐ ๋ฐํ
๏ ์ฌ์ฉ์๊ฐ ๊ธธ์ด(cm)๋ฅผ ์
๋ ฅํ๋ฉด mm๋ก ํ์ฐํ ๊ฐ์ ๋ฐํํ๋ ํจ์๋ฅผ ๋ง๋ค์ด๋ณด์.
06_์ง์ญ๋ณ์์ ์ ผ์ญ๋ณ์
๏ ์ฌ์ฉ์๊ฐ ๊ฐ๋ก, ์ธ๋ก ๊ธธ์ด๋ฅผ ์
๋ ฅํ๋ฉด ์ผ๊ฐํ๊ณผ ์ฌ๊ฐํ์ ๋์ด๋ฅผ ์ถ๋ ฅํ๋ ํจ์๋ฅผ ๋ง๋ค์ด๋ณด์.
๏ ๋ฐฉ๋ฌธ๊ฐ ์๋ฅผ ์นด์ดํธํ๋ ํจ์๋ฅผ ๋ง๋ค์ด๋ณด์.
07_์ค์ฒฉํจ์
๏ calculator()ํจ์๋ฅผ ์ ์ธํ๊ณ calculator() ์์ ๋ง์
, ๋บ์
, ๊ณฑ์
, ๋๋์
ํจ์๋ฅผ ์ ์ธํ์.
08_lambda ํจ์
๏ ์ผ๊ฐํ, ์ฌ๊ฐํ, ์์ ๋์ด๋ฅผ ๋ฐํํ๋ lambdaํจ์๋ฅผ ๋ง๋ค์ด๋ณด์.
09_๋ชจ๋
๏ ramdom ๋ชจ๋์ ์ด์ฉํด์ 1๋ถํฐ 10๊น์ง์ ์ ์ ์ค ๋์ 1๊ฐ๋ฅผ ๋ฐ์์์ผ ๋ณด์.
๏ ramdom ๋ชจ๋์ ์ด์ฉํด์ 0๋ถํฐ 100์ฌ์ด์ ๋์ 10๊ฐ๋ฅผ ๋ฐ์์์ผ ๋ณด์.
10_๋ชจ๋์ ์
๏ ๋ก๋ ๋ฒํธ(6๊ฐ)๋ฅผ ์ถ๋ ฅํ๋ ๋ชจ๋์ ๋ง๋ค์ด ๋ณด์.
๏ ๋ฌธ์์ด์ ๊ฑฐ๊พธ๋ก ๋ฐํํ๋ ๋ชจ๋์ ๋ง๋ค์ด๋ณด์.
11_๋ชจ๋์ฌ์ฉ
๏ ๊ตญ์ด, ์์ด, ์ํ ์ ์๋ฅผ ์
๋ ฅํ๋ฉด ์ด์ , ํ๊ท ์ ์ถ๋ ฅํ๋ ๋ชจ๋์ ๋ง๋ค์ด๋ณด์.
12_์คํ(๋ฉ์ธ) ํ์ผ (01)
13_์คํ(๋ฉ์ธ) ํ์ผ (02)
๏ ๋จ์ ํ์ฐ ๋ชจ๋์ ๋ง๋ค๊ณ cm๋ฅผ mm, inch, m, ft๋ก ๋ณํํด๋ณด์.
14_ํจํค์ง
15_site-packages
๏ site-packages์ ์ฝ์์ ์์๋ฅผ ๋ฆฌ์คํธ๋ก ๋ฐํํ๋ ๋ชจ๋์ ๋ง๋ค์ด๋ณด์.
16_์์ฃผ ์ฌ์ฉํ๋ ์ธ๋ถ ๋ชจ๋
17_๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ
๊ณ์ฐ๊ธฐ ํด๋์ค ๋ง๋ค๊ธฐ
์๋์ฐจ ํด๋์ค ๋ง๋ค๊ธฐ
18_ํด๋์ค์ ๊ฐ์ฒด ์์ฑ
๏ ๋นํ๊ธฐ ํด๋์ค๋ฅผ ๋ง๋ค๊ณ ๋นํ๊ธฐ ๊ฐ์ฒด 5๊ฐ๋ฅผ ์์ฑํด ๋ณด์.
19_๊ฐ์ฒด ์์ฑ ๋ณ๊ฒฝ
์์ฑ ๋ณ๊ฒฝ์ ํตํ PC์
๊ทธ๋ ์ด๋ ํด๋ณด๊ธฐ
๏ ๊ณ์ฐ๊ธฐ ํด๋์ค๋ฅผ ๋ง๋ค๊ณ ์ฌ์น์ฐ์ฐ์ ์คํํด ๋ณด์.
20_๊ฐ์ฒด์ ๋ฉ๋ชจ๋ฆฌ
๏ ๊ตญ์ด, ์์ด, ์ํ ์ ์๋ฅผ ์
๋ ฅ๋ฐ์ ๋ฆฌ์คํธ์ ์ ์ฅํ๊ณ ์๋ณธ์ ์ ์งํ ์ํ๋ก, ๋ณต์ฌ๋ณธ์ ๋ง๋ค์ด ๊ณผ๋ชฉ๋ณ ์ ์๋ฅผ 10% ์ฌ๋ ธ์ ๊ฒฝ์ฐ์ ํ๊ท ์ ์ถ๋ ฅํด ๋ณด์.
21_์์๋ณต์ฌ์ ๊น์๋ณต์ฌ
๏ ์ ์์ ์๋ณธ ์ ์๋ฅผ ์ด์ฉํด์ ํ๊ท ์ ์ถ๋ ฅํ๊ณ , ์ต๊ณ ๊ฐ๊ณผ ์ต์ ๊ฐ์ ์ ์ธํ ํ๊ท ์ ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ์ ๋ง๋ค์ด๋ณด์.
22_ํด๋์ค ์์
๏ ๋ง์
, ๋บ์
๊ธฐ๋ฅ์ด ์๋ ํด๋์ค๋ฅผ ๋ง๋ค๊ณ , ์ด๋ฅผ ์์ํ๋ ํด๋์ค๋ฅผ ๋ง๋ค์ด์ ๊ณฑ์
๊ณผ ๋๋์
๊ธฐ๋ฅ์ ์ถ๊ฐํด ๋ณด์.
23_์์ฑ์ (01)
24_์์ฑ์ (02)
๏ ์ค๊ฐ๊ณ ์ฌ ํด๋์ค์ ๊ธฐ๋ง๊ณ ์ฌ ํด๋์ค๋ฅผ ์์๊ด๊ณ๋ก ๋ง๋ค๊ณ ๊ฐ๊ฐ์ ์ ์๋ฅผ ์ด๊ธฐํํ์. ๋ํ ์ด์ ๋ฐ ํ๊ท ์ ๋ฐํํ๋ ๊ธฐ๋ฅ๋ ๋ง๋ค์ด๋ณด์.
25_๋ค์ค ์์
๏ BasicCalculator์ DeveloperCalculator ํด๋์ค๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ๋ง๋ค๊ณ ์ด๋ค ํด๋์ค๋ฅผ ์์ํด์ Calculator ํด๋์ค๋ฅผ ๋ง๋ค๊ณ ์ฌ์ฉํด ๋ณด์.
26_์ค๋ฒ๋ผ์ด๋ฉ
27_์ถ์ํด๋์ค
๏ ๊ณ์ฐ๊ธฐ ์ถ์ ํด๋์ค๋ฅผ ๋ง๋ค๊ณ ์ด๋ฅผ ์ด์ฉํด์ ์๋ก์ด ๊ณ์ฐ๊ธฐ ํด๋์ค๋ฅผ ๋ง๋ค์ด ๋ณด์. ์ถ์ ํด๋์ค์๋ ๋ง์
, ๋บ์
, ๊ณฑ์
, ๋๋์
๊ธฐ๋ฅ์ด ์ ์ธ๋์ด ์์ด์ผ ํ๋ค.
28_์์ธ๋
29_์์ธ์ฒ๋ฆฌ
๏ ์ฌ์ฉ์๋ก๋ถํฐ ์ซ์ 5๊ฐ๋ฅผ ์
๋ ฅ๋ฐ์ ๋ ์ซ์๊ฐ ์๋ ์๋ฃํ์ด ์
๋ ฅ๋๋ฉด ์์ธ ์ฒ๋ฆฌํ๋ ํ๋ก๊ทธ๋จ์ ๋ง๋ค์ด๋ณด์.
30_try ~ except ~ else
๏ ์ฌ์ฉ์๋ก๋ถํฐ ์ซ์ 5๊ฐ๋ฅผ ์
๋ ฅ๋ฐ์ ์ง์, ํ์, ์ค์๋ก ๊ตฌ๋ถํด์ ๊ฐ๊ฐ์ ๋ฆฌ์คํธ์ ์ ์ฅํ๋ ํ๋ก๊ทธ๋จ์ ๋ง๋ค์ด๋ณด์.
31_finally
๏ ์ฌ์ฉ์๋ก๋ถํฐ ์ซ์ 5๊ฐ๋ฅผ ์
๋ ฅ๋ฐ์ ์ง์, ํ์, ์ค์์ ์
๋ ฅํ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ๊ฐ ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ์ ๋ง๋ค์ด๋ณด์.
32_Exception ํด๋์ค
๏ raise ํค์๋๋ฅผ ์ด์ฉํ๋ฉด ์์ธ๋ฅผ ๋ฐ์์ํฌ ์ ์๋ค.
๏ ์ฌ์ฉ์๊ฐ ๋ฌธ์ ๋ฉ์์ง๋ฅผ ๋ณด๋ผ๋ 10๊ธ์ ์ดํ๋ฉด SMS๋ก ๋ฐ์กํ๊ณ , 10๊ธ์๋ฅผ ์ด๊ณผํ๋ฉด MMS๋ก ๋ฐ์กํ๋ ํ๋ก๊ทธ๋จ์ ์์ธ์ฒ๋ฆฌ๋ฅผ ์ด์ฉํด์ ๋ง๋ค์ด๋ณด์.
33_์ฌ์ฉ์ Exception ํด๋์ค
๏ ๊ด๋ฆฌ์ ์ํธ๋ฅผ ์
๋ ฅํ๊ณ ๋ค์ ์ํ์ ๋ฐ๋ผ ์์ธ ์ฒ๋ฆฌํ๋ ์์ธ ํด๋์ค๋ฅผ ๋ง๋ค์ด๋ณด์.
โข ์ํธ ๊ธธ์ด๊ฐ 5๋ฏธ๋ง์ธ ๊ฒฝ์ฐ: PasswordLengthShortException
โข ์ํธ ๊ธธ์ด๊ฐ 10์ ์ด๊ณผํ๋ ๊ฒฝ์ฐ: PasswordLengthLongException
โข ์ํธ๊ฐ ์๋ชป๋ ๊ฒฝ์ฐ: PasswordWrongException
34_ํ
์คํธ ํ์ผ ์ฐ๊ธฐ
๏ ๋ค์๊ณผ ๊ฐ์ด ์์คํ
์๊ฐ๊ณผ ์ผ์ ์ ํ
์คํธ ํ์ผ์ ์์ฑํด ๋ณด์.
35_ํ
์คํธ ํ์ผ ์ฝ๊ธฐ
๏ ๋ค์ ํ
์คํธ ํ์ผ์์ โPythonโ์ โํ์ด์ฌโ์ผ๋ก ๋ณ๊ฒฝํด์ ํ์ผ์ ๋ค์ ์ ์ฅํด๋ณด์.
36_ํ
์คํธ ํ์ผ ์ด๊ธฐ
๏ ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ์ซ์์ ๋ํ ์์๋ฅผ ๊ตฌํ๊ณ ์ด๋ฅผ ํ์ผ์ ์์ฑํด๋ณด์.
37_with ~ as๋ฌธ
๏ ๋ก๋ ๋ฒํธ ์์ฑ๊ธฐ ํ๋ก๊ทธ๋จ์ ๋ง๋ค๊ณ ํ์ผ์ ๋ฒํธ๋ฅผ ์ถ๋ ฅํด ๋ณด์.
38_writelines()
๏ ๋์
๋๋ฆฌ์ ์ ์ฅ๋ ๊ณผ๋ชฉ๋ณ ์ ์๋ฅผ ํ์ผ์ ์ ์ฅํ๋ ์ฝ๋๋ฅผ ์์ฑํ์.
39_readlines(), readline()
๏ ํ์ผ์ ์ ์ฅ๋ ๊ณผ๋ชฉ๋ณ ์ ์๋ฅผ ํ์ด์ฌ์์ ์ฝ์ด, ๋์
๋๋ฆฌ์ ์ ์ฅํ๋ ์ฝ๋๋ฅผ ๋ง๋ค์ด๋ณด์.
01 ์ฐ์ ์ฐ์ฐ ๊ณ์ฐ๊ธฐ๋ฅผ ํจ์ ๋ง๋ค๊ธฐ
02 ์ด๋๊ฑฐ๋ฆฌ, ์ด๋์๊ฐ ๋ฐํํ๋ ํจ์ ๋ง๋ค๊ธฐ
03 ๋นํ๊ธฐ ํฐ์ผ ์์์ฆ ์ถ๋ ฅ ํจ์ ๋ง๋ค๊ธฐ
04 ์ฌ๊ทํจ์๋ฅผ ์ด์ฉํด ํฉํ ๋ฆฌ์ผ ํจ์ ๋ง๋ค๊ธฐ + ๋จ๋ฆฌ/์๋ณต๋ฆฌ ๊ณ์ฐ๊ธฐ ํจ์ ๋ง๋ค๊ธฐ
05 ๋ฑ์ฐจ์์ด์ n๋ฒ์งธ ๊ฐ๊ณผ ํฉ์ ์ถ๋ ฅํ๋ ํจ์ ๋ง๋ค๊ธฐ
06 ๋ฑ๋น์์ด์ n๋ฒ์งธ ๊ฐ๊ณผ ํฉ์ ์ถ๋ ฅํ๋ ํจ์ ๋ง๋ค๊ธฐ
01 ๊ณผ๋ชฉ๋ณ ์ ์๋ฅผ ์
๋ ฅํด ํฉ๊ฒฉ ์ฌ๋ถ๋ฅผ ์ถ๋ ฅํ๋ ๋ชจ๋ ๋ง๋ค๊ธฐ
02 ์ํ ๊ตฌ๋งค ๊ฐ์์ ๋ฐ๋ผ ํ ์ธ์จ์ด ๊ฒฐ์ ๋๋ ๋ชจ๋ ๋ง๋ค๊ธฐ
03 ๋ก๋ ๋ชจ๋ ๋ง๋ค์ด ๋ก๋ ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋ ์ ์๋ ํ๋ก๊ทธ๋จ ๋ง๋ค๊ธฐ
04 ์์ด ๊ณ์ฐ ๋ชจ๋ ๋ง๋ค๊ธฐ
05 ์กฐํฉ ๊ณ์ฐ ๋ชจ๋ ๋ง๋ค๊ธฐ
06 ์์
๊ณผ ๊ณต๊ณผ๊ธ์ ์
๋ ฅํด ๊ณต๊ณผ๊ธ ์ด์ก๊ณผ ์์
๋๋น ๊ณต๊ณผ๊ธ ๋น์จ์ ๊ณ์ฐํ๋ ๋ชจ๋ ๋ง๋ค๊ธฐ
07 ํจํค์ง์ ๋ชจ๋์ ๋ง๋ค์ด ์ฐ์ฐ๊ฒฐ๊ณผ ์ถ๋ ฅํ๊ธฐ
01 ํ์๊ฐ์
ํด๋์ค์ ํ์์ ๋ณด๋ฅผ ๊ด๋ฆฌํ๋ ํด๋์ค๋ฅผ ๋ง๋ค๊ณ ํ์๊ฐ์
๋ก๊ทธ์ธ ๊ตฌํํ๊ธฐ
02 TVํด๋์ค๋ฅผ ์์๊ตฌ์กฐ๋ก ๋ง๋ค์ด ๊ฐ์ฒด ์์ฑํ๊ธฐ
03 ๋ช
์ธ์๋ฅผ ์ฐธ๊ณ ํด ๋์ ๊ด๋ฆฌ ํ๋ก๊ทธ๋จ ๋ง๋ค๊ธฐ
04 ํ์, ํ์ผ ์ฌ์ ํด๋์ค ๋ง๋ค๊ธฐ (# ์ถ์ํด๋์ค #@)
05 ์ฃผ์ฌ์ ๊ฒ์ ํด๋์ค๋ฅผ ๋ง๋ค๊ณ ์ปดํจํฐ์ ์ฌ์ฉ์์ ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๊ธฐ
06 ์๋์ฐจ ๊ฒฝ์ฃผ ๊ฒ์ ํด๋์ค ๋ง๋ค๊ธฐ
07 mp3 ํ๋ ์ด์ด ํด๋์ค๋ฅผ ๋ง๋ค๊ณ ๋
ธ๋ ๋ฑ๋ก ํ ์ฌ์ํ๊ธฐ
01 ์ฐ์ ์ฐ์ฐ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๊ณ ์์๋๋ ์์ธ์ฒ๋ฆฌ ์ฝ๋ ์์ฑํ๊ธฐ
02 1๋ถํฐ 1000๊น์ง์ ์์์ธ ๋์๋ฅผ 10๊ฐ ์์ฑํ๋ ์๊ตฌ๊ฐ ์๋๋ฉด ์ฌ์ฉ์ ์์ธ๊ฐ ๋ฐ์ํ๋๋ก ํ๋ ํ๋ก๊ทธ๋จ ๋ง๋ค๊ธฐ
03 ์ด๊ตฌ๋งค๊ธ์ก์ ์ถ๋ ฅํ๋ ๊ฐ์๊ฐ ์๋ชป ์
๋ ฅ๋ ๊ฒฝ์ฐ ๋ณ๋๋ก ์ถ๋ ฅํ๋๋ก ํ๋ ํ๋ก๊ทธ๋จ ๋ง๋ค๊ธฐ
04 ํ์ ๊ฐ์
ํ๋ก๊ทธ๋จ์ ๋ง๋ค๋ ์
๋ ฅํ์ง ์์ ํญ๋ชฉ์ด ์๋ ๊ฒฝ์ฐ ์๋ฌ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ ๋ง๋ค๊ธฐ
05 ์ํ ๊ณ์ข ๊ฐ์ค ๋ฐ ์
/์ถ๊ธ ํ๋ก๊ทธ๋จ ๋ง๋ค๊ธฐ
01 ํ์ ๊ณ์ ๋ณ ํ
์คํธ ํ์ผ์ ์์ฑํ ํ ํ์ ๋ณธ์ธ ํ์ผ์ ํ์ค ์ผ๊ธฐ๋ฅผ ์ฐ๊ณ ์ฝ๋ ํ๋ก๊ทธ๋จ ๋ง๋ค๊ธฐ
02 ์์
๊ณผ ์ง์ถ์ ๊ธฐ๋กํ๋ ๊ฐ๊ณ๋ถ ๋ง๋ค๊ธฐ
03 ์ซ์์ ์ฝ์์ ์์๋ฅผ ํ
์คํธ ํ์ผ์ ๊ธฐ๋กํ๊ธฐ
04 ๋๊ฐ์ ์๋ฅผ ์
๋ ฅํ๋ฉด ๊ณต์ฝ์, ์ต๋๊ณต์ฝ์๋ฅผ ํ
์คํธ ํ์ผ์ ์์ฑํ๊ธฐ
05 ๋ฐฐ(๊ณผ์ผ)๊ฐ ์
ํญํ๋ ๋ ์ง๋ฅผ ํ
์คํธ ํ์ผ์ ๊ธฐ๋กํด๋ณด๊ธฐ
01 ์ฐ์ ์ฐ์ฐ ๊ณ์ฐ๊ธฐ๋ฅผ ํจ์ ๋ง๋ค๊ธฐ
def add(n1, n2):
return n1 + n2
def sub(n1, n2):
return n1 - n2
def mul(n1, n2):
return n1 * n2
def div(n1, n2):
return n1 / n2
def mod(n1, n2):
return n1 % n2
def flo(n1, n2):
return n1 // n2
def exp(n1, n2):
return n1 ** n2
while True:
print('-' * 60)
selectNum = int(input('1.๋ง์
, 2.๋บ์
, 3.๊ณฑ์
, 4.๋๋์
, 5.๋๋จธ์ง, 6.๋ชซ, 7.์ ๊ณฑ์น, 8.์ข
๋ฃ '))
if selectNum == 8:
print('Bye~')
break
num1 = float(input('์ฒซ ๋ฒ์งธ ์ซ์ ์
๋ ฅ: '))
num2 = float(input('๋ ๋ฒ์งธ ์ซ์ ์
๋ ฅ: '))
if selectNum == 1:
print(f'{num1} + {num2} = {add(num1, num2)}')
elif selectNum == 2:
print(f'{num1} - {num2} = {sub(num1, num2)}')
elif selectNum == 3:
print(f'{num1} * {num2} = {mul(num1, num2)}')
elif selectNum == 4:
print(f'{num1} / {num2} = {div(num1, num2)}')
elif selectNum == 5:
print(f'{num1} % {num2} = {mod(num1, num2)}')
elif selectNum == 6:
print(f'{num1} // {num2} = {flo(num1, num2)}')
elif selectNum == 7:
print(f'{num1} ** {num2} = {exp(num1, num2)}')
else:
print('์๋ชป ์
๋ ฅํ์ต๋๋ค. ๋ค์ ์
๋ ฅํ์ธ์.')
print('-' * 60)
02 ์ด๋๊ฑฐ๋ฆฌ, ์ด๋์๊ฐ ๋ฐํํ๋ ํจ์ ๋ง๋ค๊ธฐ (๋ฌธ2)
# ๊ฑฐ๋ฆฌ(km) = ์๋(km/h) * ์๊ฐ(h)
def getDistance(speed, hour, minute):
distance = speed * (hour + (minute / 60))
return distance
def getTime(speed, distance):
# 100 : 75 = 60 : x --> x = 75 * 60 / 100
time = distance / speed
print(f'time: {time}')
h = int(time)
m = int((time - h) * 100 * 60 / 100)
# m = convertFloatToMinute(time - h)
return [h, m]
def convertFloatToMinute(f):
return int(f * 100 * 60 / 100)
print('-' * 60)
s = float(input('์๋(km/h) ์
๋ ฅ: '))
h = float(input('์๊ฐ(h) ์
๋ ฅ: '))
m = float(input('์๊ฐ(m) ์
๋ ฅ: '))
d = getDistance(s, h, m)
print(f'{s}(km/h)์๋๋ก {h}(h)์๊ฐ {m}(m)๋ถ ๋์ ์ด๋ํ ๊ฑฐ๋ฆฌ: {d}(km)')
print('-' * 60)
print('-' * 60)
s = float(input('์๋(km/h) ์
๋ ฅ: '))
d = float(input('๊ฑฐ๋ฆฌ(km) ์
๋ ฅ: '))
t = getTime(s, d)
print(f'{s}(km/h) ์๋๋ก {d}(km) ์ด๋ํ ์๊ฐ: {t[0]}(h)์๊ฐ {t[1]}(m)๋ถ')
print('-' * 60)
03 ๋นํ๊ธฐ ํฐ์ผ ์์์ฆ ์ถ๋ ฅ ํจ์ ๋ง๋ค๊ธฐ
childPrice = 18000
infantPrice = 25000
adultPrice = 50000
specialDC = 50
def formatedNumber(n):
return format(n, ',')
def printAriPlaneReceipt(c1, c2, i1, i2, a1, a2):
print('=' * 40)
cp = c1 * childPrice
cp_dc = int(c2 * childPrice * 0.5)
print(f'์ ์ {c1}๋ช
์๊ธ: {formatedNumber(cp)}์')
print(f'์ ์ ํ ์ธ ๋์ {c2}๋ช
์๊ธ: {formatedNumber(cp_dc)}์')
ip = i1 * infantPrice
ip_dc = int(i2 * infantPrice * 0.5)
print(f'์์ {i1}๋ช
์๊ธ: {formatedNumber(ip)}์')
print(f'์์ ํ ์ธ ๋์ {i2}๋ช
์๊ธ: {formatedNumber(ip_dc)}์')
ap = a1 * adultPrice
ap_dc = int(a2 * adultPrice * 0.5)
print(f'์ฑ์ธ {a1}๋ช
์๊ธ: {formatedNumber(ap)}์')
print(f'์ฑ์ธ ํ ์ธ ๋์ {a2}๋ช
์๊ธ: {formatedNumber(ap_dc)}์')
print('=' * 40)
print(f'Total: {formatedNumber(c1 + c2 + i1 + i2 + a1 + a2)}๋ช
')
print(f'TotalPrice : {formatedNumber(cp + cp_dc + ip + ip_dc + ap + ap_dc)}์')
print('=' * 40)
print(f'childPrice(24๊ฐ์ ๋ฏธ๋ง)\t\t: {formatedNumber(childPrice)}์')
print(f'infantPrice(๋ง12์ธ ๋ฏธ๋ง)\t\t: {formatedNumber(infantPrice)}์')
print(f'adultPrice(๋ง12์ธ ์ดํ)\t\t: {formatedNumber(adultPrice)}์')
print(f'๊ตญ๊ฐ ์ ๊ณต์ ๋ฐ ์ฅ์ ์ฐ ํ ์ธ\t\t: {specialDC}%')
print()
childCnt = int(input('์ ์ ์
๋ ฅ: '))
specialDCChildCnt = int(input(f'ํ ์ธ๋์ ์ ์ ์
๋ ฅ: '))
infantCnt = int(input('์์ ์
๋ ฅ: '))
specialDCInfantCnt = int(input(f'ํ ์ธ๋์ ์์ ์
๋ ฅ: '))
adultCnt = int(input('์ฑ์ธ ์
๋ ฅ: '))
specialDCAdultCnt = int(input(f'ํ ์ธ๋์ ์ฑ์ธ ์
๋ ฅ: '))
printAriPlaneReceipt(childCnt, specialDCChildCnt,
infantCnt, specialDCInfantCnt,
adultCnt, specialDCAdultCnt)
04 ์ฌ๊ทํจ์๋ฅผ ์ด์ฉํด ํฉํ ๋ฆฌ์ผ ํจ์ ๋ง๋ค๊ธฐ + ๋จ๋ฆฌ/์๋ณต๋ฆฌ ๊ณ์ฐ๊ธฐ ํจ์ ๋ง๋ค๊ธฐ
def formatedNumber(n):
return format(n, ',')
def recursionFun(n):
if n == 1:
return n
return n * recursionFun(n - 1)
inputNumber = int(input('input number: '))
inputNumberFormated = formatedNumber(recursionFun(inputNumber))
print(inputNumberFormated)
# ๋จ๋ฆฌ ๊ณ์ฐ๊ธฐ
def singleRateCalculator(m, t, r):
totalMoney = 0
totalRateMoney = 0
for i in range(t):
totalRateMoney += m * (r * 0.01)
totalMoney = m + totalRateMoney
return int(totalMoney)
# ์๋ณต๋ฆฌ ๊ณ์ฐ๊ธฐ
def multiRateCalculator(m, t, r):
t = t * 12
rpm = (r / 12) * 0.01
totalMoney = m
for i in range(t):
totalMoney += totalMoney * rpm
return int(totalMoney)
money = int(input('์์น๊ธ(์): '))
term = int(input('๊ธฐ๊ฐ(๋
): '))
rate = int(input('์ฐ ์ด์จ(%): '))
print()
print('[๋จ๋ฆฌ ๊ณ์ฐ๊ธฐ]')
print('=' * 30)
print(f'์์น๊ธ\t: {formatedNumber(money)}์')
print(f'์์น๊ธฐ๊ฐ\t: {term}๋
')
print(f'์ฐ ์ด์จ\t: {rate}%')
print('-' * 30)
amount = formatedNumber(singleRateCalculator(money, term, rate))
print(f'{term}๋
ํ ์ด ์๋ น์ก: {amount}์')
print('=' * 30)
print()
print('[์๋ณต๋ฆฌ ๊ณ์ฐ๊ธฐ]')
print('=' * 30)
print(f'์์น๊ธ\t: {formatedNumber(money)}์')
print(f'์์น๊ธฐ๊ฐ\t: {term}๋
')
print(f'์ฐ ์ด์จ\t: {rate}%')
print('-' * 30)
amount = formatedNumber(multiRateCalculator(money, term, rate))
print(f'{term}๋
ํ ์ด ์๋ น์ก: {amount}์')
print('=' * 30)
05 ๋ฑ์ฐจ์์ด์ n๋ฒ์งธ ๊ฐ๊ณผ ํฉ์ ์ถ๋ ฅํ๋ ํจ์ ๋ง๋ค๊ธฐ
def sequenceCal(n1, d, n):
valueN = 0
sumN = 0
i = 1
while i <= n:
if i == 1:
valueN = n1
sumN += valueN
print('{}๋ฒ์งธ ํญ์ ๊ฐ: {}'.format(i, valueN))
print('{}๋ฒ์งธ ํญ๊น์ง์ ํฉ: {}'.format(i, sumN))
i += 1
continue
valueN += d
sumN += valueN
print('{}๋ฒ์งธ ํญ์ ๊ฐ: {}'.format(i, valueN))
print('{}๋ฒ์งธ ํญ๊น์ง์ ํฉ: {}'.format(i, sumN))
i += 1
def sequenceCal01(n1, d, n):
# ๋ฑ์ฐจ ์์ด(์ผ๋ฐํญ) ๊ณต์: an = a1 + (n-1) * d
valueN = n1 + (n-1) * d
print('{}๋ฒ์งธ ํญ์ ๊ฐ: {}'.format(n, valueN))
# ๋ฑ์ฐจ ์์ด(ํฉ) ๊ณต์: sn = n(a1 + an) / 2
sumN = n * (n1 + valueN) / 2
print('{}๋ฒ์งธ ํญ๊น์ง์ ํฉ: {}'.format(n, int(sumN)))
inputN1 = int(input('a1 ์
๋ ฅ: '))
inputD = int(input('๊ณต์ฐจ ์
๋ ฅ: '))
inputN = int(input('n ์
๋ ฅ: '))
sequenceCal(inputN1, inputD, inputN)
print('-' * 50)
sequenceCal01(inputN1, inputD, inputN)
06 ๋ฑ๋น์์ด์ n๋ฒ์งธ ๊ฐ๊ณผ ํฉ์ ์ถ๋ ฅํ๋ ํจ์ ๋ง๋ค๊ธฐ
def sequenceCal(n1, r, n):
valueN = 0
sumN = 0
i = 1
while i <= inputN:
if i == 1:
valueN = n1
sumN += valueN
print('{}๋ฒ์งธ ํญ์ ๊ฐ: {}'.format(i, valueN))
print('{}๋ฒ์งธ ํญ๊น์ง์ ํฉ: {}'.format(i, sumN))
i += 1
continue
valueN *= r
sumN += valueN
print('{}๋ฒ์งธ ํญ์ ๊ฐ: {}'.format(i, valueN))
print('{}๋ฒ์งธ ํญ๊น์ง์ ํฉ: {}'.format(i, sumN))
i += 1
def sequenceCal01(n1, r, n):
# ๋ฑ๋น ์์ด(์ผ๋ฐํญ) ๊ณต์: an = a1 * r^(n-1)
valueN = n1 * (r ** (n - 1))
print('{}๋ฒ์งธ ํญ์ ๊ฐ: {}'.format(n, valueN))
# ๋ฑ๋น ์์ด(ํฉ) ๊ณต์: sn = a1 * (1 - r^n) / (1-r)
sumN = n1 * (1 - (r ** n)) / (1 - r)
print('{}๋ฒ์งธ ํญ๊น์ง์ ํฉ: {}'.format(n, int(sumN)))
inputN1 = int(input('a1 ์
๋ ฅ: '))
inputR = int(input('๊ณต๋น ์
๋ ฅ: '))
inputN = int(input('n ์
๋ ฅ: '))
sequenceCal(inputN1, inputR, inputN)
print('-' * 50)
sequenceCal01(inputN1, inputR, inputN)
01 ๊ณผ๋ชฉ๋ณ ์ ์๋ฅผ ์
๋ ฅํด ํฉ๊ฒฉ ์ฌ๋ถ๋ฅผ ์ถ๋ ฅํ๋ ๋ชจ๋ ๋ง๋ค๊ธฐ
# passOrFail.py
def exampleResult(s1, s2, s3, s4, s5):
passAvgScore = 60
limitScore = 40
def getTotal():
totalScore = s1 + s2 + s3 + s4 + s5
print(f'์ด์ : {totalScore}')
return totalScore
def getAverage():
avg = getTotal() / 5
print(f'ํ๊ท : {avg}')
return avg
def printPassOrFail():
print(f'{s1}: Pass') if s1 >= limitScore else print(f'{s1}: Fail')
print(f'{s2}: Pass') if s2 >= limitScore else print(f'{s2}: Fail')
print(f'{s3}: Pass') if s3 >= limitScore else print(f'{s3}: Fail')
print(f'{s4}: Pass') if s4 >= limitScore else print(f'{s4}: Fail')
print(f'{s5}: Pass') if s5 >= limitScore else print(f'{s5}: Fail')
def printFinalPassOrFail():
if getAverage() >= passAvgScore:
if s1 >= limitScore and s2 >= limitScore and s3 >= limitScore and s4 >= limitScore and s5 >= limitScore:
print('Final Pass!!')
else:
print('Final Fail!!')
else:
print('Final Fail!!')
getAverage()
printPassOrFail()
printFinalPassOrFail()
if __name__ == '__main__':
sub1 = int(input('๊ณผ๋ชฉ1 ์ ์ ์
๋ ฅ: '))
sub2 = int(input('๊ณผ๋ชฉ2 ์ ์ ์
๋ ฅ: '))
sub3 = int(input('๊ณผ๋ชฉ3 ์ ์ ์
๋ ฅ: '))
sub4 = int(input('๊ณผ๋ชฉ4 ์ ์ ์
๋ ฅ: '))
sub5 = int(input('๊ณผ๋ชฉ5 ์ ์ ์
๋ ฅ: '))
exampleResult(sub1, sub2, sub3, sub4, sub5)
def exampleResult(*s):
passAvgScore = 60
limitScore = 40
def getTotal():
totalScore = sum(s)
print(f'์ด์ : {totalScore}')
return totalScore
def getAverage():
avg = getTotal() / len(s)
print(f'ํ๊ท : {avg}')
return avg
def printPassOrFail():
for idx, score in enumerate(s):
print(f'๊ณผ๋ชฉ{idx+1}: Pass') if score >= limitScore else print(f'๊ณผ๋ชฉ{idx+1}: Fail')
def printFinalPassOrFail():
result = 'Final Pass!!'
if getAverage() >= passAvgScore:
for idx, score in enumerate(s):
if score < limitScore:
result = 'Final Fail!!'
break
else:
result = 'Final Fail!!'
print(result)
getAverage()
printPassOrFail()
printFinalPassOrFail()
if __name__ == '__main__':
sub1 = int(input('๊ณผ๋ชฉ1 ์ ์ ์
๋ ฅ: '))
sub2 = int(input('๊ณผ๋ชฉ2 ์ ์ ์
๋ ฅ: '))
sub3 = int(input('๊ณผ๋ชฉ3 ์ ์ ์
๋ ฅ: '))
sub4 = int(input('๊ณผ๋ชฉ4 ์ ์ ์
๋ ฅ: '))
sub5 = int(input('๊ณผ๋ชฉ5 ์ ์ ์
๋ ฅ: '))
exampleResult(sub1, sub2, sub3, sub4, sub5)
# ex.py
import passOrFail as pf
if __name__ == '__main__':
sub1 = int(input('๊ณผ๋ชฉ1 ์ ์ ์
๋ ฅ: '))
sub2 = int(input('๊ณผ๋ชฉ2 ์ ์ ์
๋ ฅ: '))
sub3 = int(input('๊ณผ๋ชฉ3 ์ ์ ์
๋ ฅ: '))
sub4 = int(input('๊ณผ๋ชฉ4 ์ ์ ์
๋ ฅ: '))
sub5 = int(input('๊ณผ๋ชฉ5 ์ ์ ์
๋ ฅ: '))
pf.exampleResult(sub1, sub2, sub3, sub4, sub5)
02 ์ํ ๊ตฌ๋งค ๊ฐ์์ ๋ฐ๋ผ ํ ์ธ์จ์ด ๊ฒฐ์ ๋๋ ๋ชจ๋ ๋ง๋ค๊ธฐ
# discount.py
def calculatorTotalPrice(gs):
if len(gs) <= 0:
print('๊ตฌ๋งค ์ํ์ด ์์ต๋๋ค.')
return
rate = 25
totalPrice = 0
rates = {1:5, 2:10, 3:15, 4:20}
if len(gs) in rates:
rate = rates[len(gs)]
for g in gs:
totalPrice += g * (1 - rate * 0.01)
return [rate, int(totalPrice)]
def formatedNumber(n):
return format(n, ',')
# ex.py
import discount as dc
if __name__ == '__main__':
flag = True
gs = []
while flag:
selectNumber = int(input('์ํ์ ๊ตฌ๋งค ํ์๊ฒ ์ด์? 1.๊ตฌ๋งค 2.์ข
๋ฃ '))
if selectNumber == 1:
goods_price = int(input('์ํ ๊ฐ๊ฒฉ ์
๋ ฅ: '))
gs.append(goods_price)
elif selectNumber == 2:
result = dc.calculatorTotalPrice(gs)
flag = False
print(f'ํ ์ธ์จ: {result[0]}')
print(f'ํฉ๊ณ: {dc.formatedNumber(result[1])}์')
03 ๋ก๋ ๋ชจ๋ ๋ง๋ค์ด ๋ก๋ ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋ ์ ์๋ ํ๋ก๊ทธ๋จ ๋ง๋ค๊ธฐ
# lotto.py
import random
userNums = []; randNums = []; collNums = []
randBonuNum = 0
def setUserNums(ns):
global userNums
userNums = ns
def getUserNums():
return userNums
def setRandNums():
global randNums
randNums = random.sample(range(1, 46), 6)
def getRandNums():
return randNums
def setBonuNum():
global randBonuNum
while True:
randBonuNum = random.randint(1, 45)
if randBonuNum not in randNums:
break
def getBonuNum():
return randBonuNum
def lottoResult():
global userNums
global randNums
global collNums
collNums = []
for un in userNums:
if un in randNums:
collNums.append(un)
if len(collNums) == 6:
print(f'1๋ฑ ๋ด์ฒจ!!')
print(f'๋ฒํธ: {collNums}')
elif (len(collNums) == 5) and (randBonuNum in userNums):
print(f'2๋ฑ ๋ด์ฒจ!!')
print(f'๋ฒํธ: {collNums}, ๋ณด๋์ค ๋ฒํธ: {randBonuNum}')
elif len(collNums) == 5:
print(f'3๋ฑ ๋ด์ฒจ!!')
print(f'๋ฒํธ: {collNums}')
elif len(collNums) == 4:
print(f'4๋ฑ ๋ด์ฒจ!!')
print(f'๋ฒํธ: {collNums}')
if len(collNums) == 3:
print(f'5๋ฑ ๋ด์ฒจ!!')
print(f'๋ฒํธ: {collNums}')
else:
print('์์ฝ์ต๋๋ค. ๋ค์ ๊ธฐํ์~')
print(f'๊ธฐ๊ณ ๋ฒํธ: {randNums}')
print(f'๋ณด๋์ค ๋ฒํธ: {randBonuNum}')
print(f'์ ํ ๋ฒํธ: {userNums}')
print(f'์ผ์น ๋ฒํธ: {collNums}')
def startLotto():
n1 = int(input('๋ฒํธ(1~45) ์
๋ ฅ: '))
n2 = int(input('๋ฒํธ(1~45) ์
๋ ฅ: '))
n3 = int(input('๋ฒํธ(1~45) ์
๋ ฅ: '))
n4 = int(input('๋ฒํธ(1~45) ์
๋ ฅ: '))
n5 = int(input('๋ฒํธ(1~45) ์
๋ ฅ: '))
n6 = int(input('๋ฒํธ(1~45) ์
๋ ฅ: '))
selectNums = [n1, n2, n3, n4, n5, n6]
setUserNums(selectNums)
setRandNums()
setBonuNum()
lottoResult()
# ex.py
import lotto as lt
lt.startLotto()
04 ์์ด ๊ณ์ฐ ๋ชจ๋ ๋ง๋ค๊ธฐ
# permutation.py
def getPermutaionCnt(n, r, logPrint = True):
result = 1
for n in range(n, (n-r), -1):
if logPrint: print('n : {}'.format(n))
result = result * n
return result
from itertools import permutations
def getPermutaions(ns, r):
pList = list(permutations(ns, r))
print(f'{len(ns)}P{r} ๊ฐ์: {len(pList)}')
for n in permutations(ns, r):
print(n, end=', ')
if __name__ == '__main__':
numN = int(input('numN ์
๋ ฅ: '))
numR = int(input('numR ์
๋ ฅ: '))
print(f'{numN}P{numR}: {getPermutaionCnt(numN, numR, logPrint=False)}')
ns = [1, 2, 3, 4, 5, 6, 7, 8]
getPermutaions(ns, 3)
# ex.py
import permutation as pt
numN = int(input('numN ์
๋ ฅ: '))
numR = int(input('numR ์
๋ ฅ: '))
# print(f'{numN}P{numR}: {pt.getPermutaionCnt(numN, numR)}')
print(f'{numN}P{numR}: {pt.getPermutaionCnt(numN, numR, logPrint=False)}')
listVar = [1, 2, 3, 4, 5, 6, 7, 8]
rVar = 3
pt.getPermutaions(listVar, rVar)
05 ์กฐํฉ ๊ณ์ฐ ๋ชจ๋ ๋ง๋ค๊ธฐ
# combination.py
def getCombinationCnt(n, r, logPrint = True):
resultP = 1
resultR = 1
resultC = 1
for n in range(n, (n - r), -1):
resultP = resultP * n
if logPrint: print('resultP : {}'.format(resultP))
for n in range(r, 0, -1):
resultR = resultR * n
if logPrint: print('resultR: {}'.format(resultR))
resultC = int(resultP / resultR)
if logPrint: print('resultC: {}'.format(resultC))
return resultC
from itertools import combinations
def getCombinations(ns, r):
cList = list(combinations(ns, r))
print(f'{len(ns)}C{r} ๊ฐ์: {len(cList)}')
for n in combinations(ns, r):
print(n, end=', ')
if __name__ == '__main__':
numN = int(input('numN ์
๋ ฅ: '))
numR = int(input('numR ์
๋ ฅ: '))
print(f'{numN}C{numR}: {getCombinationCnt(numN, numR, logPrint=False)}')
ns = [1, 2, 3, 4, 5, 6, 7, 8]
getCombinations(ns, 3)
# ex.py
import combination as ct
numN = int(input('numN ์
๋ ฅ: '))
numR = int(input('numR ์
๋ ฅ: '))
# print(f'{numN}C{numR}: {ct.getCombinationCnt(numN, numR)}')
print(f'{numN}C{numR}: {ct.getCombinationCnt(numN, numR, logPrint=False)}')
listVar = [1, 2, 3, 4, 5, 6, 7, 8]
rVar = 3
ct.getCombinations(listVar, rVar)
06 ์์
๊ณผ ๊ณต๊ณผ๊ธ์ ์
๋ ฅํด ๊ณต๊ณผ๊ธ ์ด์ก๊ณผ ์์
๋๋น ๊ณต๊ณผ๊ธ ๋น์จ์ ๊ณ์ฐํ๋ ๋ชจ๋ ๋ง๋ค๊ธฐ
# utilityBill.py
income = 0
waterPrice = 0; electricPrice = 0; gasPrice = 0
def setIncome(ic):
global income
income = ic
def getIncome():
return income
def setWaterPrice(wp):
global waterPrice
waterPrice = wp
def setElectricPrice(ep):
global electricPrice
electricPrice = ep
def setGasPrice(gp):
global gasPrice
gasPrice = gp
def getUtilityBill():
result = waterPrice + electricPrice + gasPrice
return result
def getUtilityBillRate():
result = getUtilityBill() / getIncome() * 100
return round(result, 2)
def formatedNumber(n):
return format(n, ',')
# ex.py
import utilityBill as ub
inputIncome = int(input('์์
์
๋ ฅ: '))
ub.setIncome(inputIncome)
inputWaterPrice = int(input('์๋์๊ธ ์
๋ ฅ: '))
ub.setWaterPrice(inputWaterPrice)
inputElectricPrice = int(input('์ ๊ธฐ์๊ธ ์
๋ ฅ: '))
ub.setElectricPrice(inputElectricPrice)
inputGasPrice = int(input('๊ฐ์ค์๊ธ ์
๋ ฅ: '))
ub.setGasPrice(inputGasPrice)
print(f'๊ณต๊ณผ๊ธ: {ub.formatedNumber(ub.getUtilityBill())}์')
print(f'์์
๋๋น ๊ณต๊ณผ๊ธ ๋น์จ: {ub.getUtilityBillRate()}%')
07 ํจํค์ง์ ๋ชจ๋์ ๋ง๋ค์ด ์ฐ์ฐ๊ฒฐ๊ณผ ์ถ๋ ฅํ๊ธฐ
# 1. arithmetic ํด๋ ์์ฑ ํ ์์ basic_operator.py, developer_oerator.py ๋ชจ๋ ์์ฑ
# basic_operator.py
def add(n1, n2):
return round(n1 + n2, 2)
def sub(n1, n2):
return round(n1 - n2, 2)
def mul(n1, n2):
return round(n1 * n2, 2)
def div(n1, n2):
return round(n1 / n2, 2)
# developer_oerator.py
def mod(n1, n2):
return round(n1 % n2, 2)
def flo(n1, n2):
return round(n1 // n2, 2)
def exp(n1, n2):
return round(n1 ** n2, 2)
# 2. shape ํด๋ ์์ฑ ํ ์์ circle_area.py, triangle_square_area.py ๋ชจ๋ ์์ฑ
# circle_area.py
def calCircleArea(r):
return round(r ** 2 * 3.14, 2)
# triangle_square_area.py
def calTriangleArea(w, h):
return round(w * h / 2, 2)
def calSquareArea(w, h):
return round(w * h, 2)
# 3. arithmetic ํด๋์ shape ํด๋ ๊ฒฝ๋ก์ ์คํํ์ผ ๋ง๋ค๊ธฐ
# ex.py
from arithmetic import basic_operator as bo
from arithmetic import developer_oerator as do
from shape import triangle_square_area as tsa
from shape import circle_area as ca
inputNumber1 = float(input('์ซ์1 ์
๋ ฅ: '))
inputNumber2 = float(input('์ซ์2 ์
๋ ฅ: '))
print(f'{inputNumber1} + {inputNumber2} = {bo.add(inputNumber1, inputNumber2)}')
print(f'{inputNumber1} - {inputNumber2} = {bo.sub(inputNumber1, inputNumber2)}')
print(f'{inputNumber1} * {inputNumber2} = {bo.mul(inputNumber1, inputNumber2)}')
print(f'{inputNumber1} / {inputNumber2} = {bo.div(inputNumber1, inputNumber2)}')
print(f'{inputNumber1} % {inputNumber2} = {do.mod(inputNumber1, inputNumber2)}')
print(f'{inputNumber1} // {inputNumber2} = {do.flo(inputNumber1, inputNumber2)}')
print(f'{inputNumber1} ** {inputNumber2} = {do.exp(inputNumber1, inputNumber2)}')
inputWidth = float(input('๊ฐ๋ก ๊ธธ์ด ์
๋ ฅ: '))
inputHeight = float(input('์ธ๋ก ๊ธธ์ด ์
๋ ฅ: '))
print(f'์ผ๊ฐํ ๋์ด: {tsa.calTriangleArea(inputWidth, inputHeight)}')
print(f'์ฌ๊ฐํ ๋์ด: {tsa.calSquareArea(inputWidth, inputHeight)}')
inputRadius = float(input('๋ฐ์ง๋ฆ ์
๋ ฅ: '))
print(f'์ฌ๊ฐํ ๋์ด: {ca.calCircleArea(inputRadius)}')
01 ํ์๊ฐ์
ํด๋์ค์ ํ์์ ๋ณด๋ฅผ ๊ด๋ฆฌํ๋ ํด๋์ค๋ฅผ ๋ง๋ค๊ณ ํ์๊ฐ์
๋ก๊ทธ์ธ ๊ตฌํํ๊ธฐ
# member.py
class Member:
def __init__(self, i, p):
self.id = i
self.pw = p
class MemberRepository:
def __init__(self):
self.members = {}
def addMember(self, m):
self.members[m.id] = m.pw
def loginMember(self, i, p):
isMember = i in self.members
if isMember and self.members[i] == p:
print(f'{i}: Log-in success!!')
else:
print(f'{i}: Log-in fail!!')
def removeMember(self, i, p):
del self.members[i]
def printMembers(self):
for mk in self.members.keys():
print(f'ID: {mk}')
print(f'PW: {self.members[mk]}')
# ex.py
import member as mb
mems = mb.MemberRepository()
for i in range(3):
mId = input('์์ด๋ ์
๋ ฅ: ')
mPw = input('๋น๋ฐ๋ฒํธ ์
๋ ฅ: ')
mem = mb.Member(mId, mPw)
mems.addMember(mem)
mems.printMembers()
mems.loginMember('abc@gmail.com', '1234')
mems.loginMember('def@gmail.com', '5678')
mems.loginMember('ghi@gmail.com', '9012')
mems.removeMember('abc@gmail.com', '1234')
mems.printMembers()
02 TVํด๋์ค๋ฅผ ์์๊ตฌ์กฐ๋ก ๋ง๋ค์ด ๊ฐ์ฒด ์์ฑํ๊ธฐ
# smartTv.py
class NormalTv:
def __init__(self, i=32, c='black', r='full-HD'):
self.inch = i
self.color = c
self.resolution = r
self.smartTv = 'off'
self.aiTv = 'off'
def turnOn(self):
print('TV power on!!')
def turnOff(self):
print('TV power off!!')
def printTvInfo(self):
print(f'inch: {self.inch}inch')
print(f'color: {self.color}')
print(f'resolution: {self.resolution}')
print(f'smartTv: {self.smartTv}')
print(f'aiTv: {self.aiTv}')
class Tv4k(NormalTv):
def __init__(self, i, c, r='4k'):
super().__init__(i, c, r)
def setSmartTv(self, s):
self.smartTv = s
class Tv8k(NormalTv):
def __init__(self, i, c, r='8k'):
super().__init__(i, c, r)
def setSmartTv(self, s):
self.smartTv = s
def setAiTv(self, a):
self.aiTv = a
if __name__ == '__main__':
my4KTv = Tv4k('65', 'silver', '4K')
my4KTv.setSmartTv('on')
my4KTv.turnOn()
my4KTv.printTvInfo()
my4KTv.turnOff()
my8KTv = Tv8k('75', 'black', '8K')
my8KTv.setSmartTv('on')
my8KTv.setAiTv('on')
my8KTv.turnOn()
my8KTv.printTvInfo()
my8KTv.turnOff()
# ex.py
import smartTv as st
my4KTv = st.Tv4k('65', 'silver', '4K')
my4KTv.setSmartTv('on')
my4KTv.turnOn()
my4KTv.printTvInfo()
my4KTv.turnOff()
friend4KTv = st.Tv4k('55', 'white', '4K')
friend4KTv.setSmartTv('off')
friend4KTv.turnOn()
friend4KTv.printTvInfo()
friend4KTv.turnOff()
my8KTv = st.Tv8k('75', 'black', '8K')
my8KTv.setSmartTv('on')
my8KTv.setAiTv('on')
my8KTv.turnOn()
my8KTv.printTvInfo()
my8KTv.turnOff()
friend8KTv = st.Tv8k('86', 'red', '8K')
friend8KTv.setSmartTv('on')
friend8KTv.setAiTv('off')
friend8KTv.turnOn()
friend8KTv.printTvInfo()
friend8KTv.turnOff()
03 ๋ช
์ธ์๋ฅผ ์ฐธ๊ณ ํด ๋์ ๊ด๋ฆฌ ํ๋ก๊ทธ๋จ ๋ง๋ค๊ธฐ
# book.py
class Book:
def __init__(self, name, price, isbn):
self.bName = name
self.bPrice = price
self.bIsbn = isbn
class BookRepository:
def __init__(self):
self.bDic = {}
def registBook(self, b):
self.bDic[b.bIsbn] = b
def removeBook(self, isbn):
del self.bDic[isbn]
def printBooksInfo(self):
for isbn in self.bDic.keys():
b = self.bDic[isbn]
print(f'{b.bName}, {b.bPrice}, {b.bIsbn}')
def printBookInfo(self, isbn):
if isbn in self.bDic:
b = self.bDic[isbn]
print(f'{b.bName}, {b.bPrice}, {b.bIsbn}')
else:
print('Lookup result does not exist')
if __name__ == '__main__':
br = BookRepository()
br.registBook(Book('python', 20000, '1234567890'))
br.registBook(Book('java', 25000, '852147963'))
br.registBook(Book('c/c++', 27000, '951378624'))
br.registBook(Book('javascript', 15000, '9874563254'))
br.printBooksInfo()
br.printBookInfo('1234567890')
br.removeBook('1234567890')
br.printBooksInfo()
# ex.py
import book as bk
myBReposit = bk.BookRepository()
myBReposit.registBook(bk.Book('python', 20000, '1234567890'))
myBReposit.registBook(bk.Book('java', 25000, '852147963'))
myBReposit.registBook(bk.Book('c/c++', 27000, '951378624'))
myBReposit.registBook(bk.Book('javascript', 15000, '9874563254'))
myBReposit.printBooksInfo()
myBReposit.printBookInfo('1234567890')
myBReposit.removeBook('1234567890')
myBReposit.printBooksInfo()
friendBReposit = bk.BookRepository()
friendBReposit.registBook(bk.Book('python', 10000, '1234567890'))
friendBReposit.registBook(bk.Book('java', 15000, '852147963'))
friendBReposit.registBook(bk.Book('c/c++', 17000, '951378624'))
friendBReposit.registBook(bk.Book('javascript', 5000, '9874563254'))
friendBReposit.printBooksInfo()
friendBReposit.printBookInfo('1234567890')
friendBReposit.removeBook('1234567890')
friendBReposit.printBooksInfo()
04 ํ์, ํ์ผ ์ฌ์ ํด๋์ค ๋ง๋ค๊ธฐ (# ์ถ์ํด๋์ค #@)
# ADictionary.py
from abc import ABCMeta
from abc import abstractmethod
class AbsDictionary(metaclass=ABCMeta):
def __init__(self):
self.wordDic = {}
@abstractmethod
def registWord(self, w1, w2):
pass
@abstractmethod
def removeWord(self, w1):
pass
@abstractmethod
def updateWord(self, w1, w2):
pass
@abstractmethod
def searchWord(self, w1):
pass
class KorToEng(AbsDictionary):
def __init__(self):
super().__init__()
def registWord(self, w1, w2):
print(f'[KorToEng] registWord() : {w1} to {w2}')
self.wordDic[w1] = w2
def removeWord(self, w1):
print(f'[KorToEng] removeWord() : {w1}')
del self.wordDic[w1]
def updateWord(self, w1, w2):
print(f'[KorToEng] updateWord() : {w1} to {w2}')
self.wordDic[w1] = w2
def searchWord(self, w1):
print(f'[KorToEng] searchWord() : {w1}')
return self.wordDic[w1]
def printWords(self):
for k in self.wordDic:
print(f'{k} : {self.wordDic[k]}')
class KorToJpa(AbsDictionary):
def __init__(self):
super().__init__()
def registWord(self, w1, w2):
print(f'[KorToJpa] registWord() : {w1} to {w2}')
self.wordDic[w1] = w2
def removeWord(self, w1):
print(f'[KorToJpa] removeWord() : {w1}')
del self.wordDic[w1]
def updateWord(self, w1, w2):
print(f'[KorToJpa] updateWord() : {w1} to {w2}')
self.wordDic[w1] = w2
def searchWord(self, w1):
print(f'[KorToJpa] searchWord() : {w1}')
return self.wordDic[w1]
def printWords(self):
for k in self.wordDic:
print(f'{k} : {self.wordDic[k]}')
if __name__ == '__main__':
kTe = KorToEng()
# ๋จ์ด ๋ฑ๋ก
kTe.registWord('์ฑ
', 'bok')
kTe.registWord('๋๋น', 'butterfly')
kTe.registWord('์ฐํ', 'pencil')
kTe.registWord('ํ์', 'studen')
kTe.registWord('์ ์๋', 'teacher')
# ๋จ์ด ์์
kTe.updateWord('์ฑ
', 'book')
kTe.updateWord('ํ์', 'student')
# ๋จ์ด ๊ฒ์
print(f'์ฑ
: {kTe.searchWord("์ฑ
")}')
print(f'๋๋น : {kTe.searchWord("๋๋น")}')
print(f'์ฐํ : {kTe.searchWord("์ฐํ")}')
print(f'ํ์ : {kTe.searchWord("ํ์")}')
print(f'์ ์๋ : {kTe.searchWord("์ ์๋")}')
# ๋จ์ด ์ญ์
kTe.removeWord('์ฑ
')
# ์ฌ์ ์ถ๋ ฅ
kTe.printWords()
# ex.py
import ADictionary as dic
kTe = dic.KorToEng()
# ๋จ์ด ๋ฑ๋ก
kTe.registWord('์ฑ
', 'bok')
kTe.registWord('๋๋น', 'butterfly')
kTe.registWord('์ฐํ', 'pencil')
kTe.registWord('ํ์', 'studen')
kTe.registWord('์ ์๋', 'teacher')
# ๋จ์ด ์์
kTe.updateWord('์ฑ
', 'book')
kTe.updateWord('ํ์', 'student')
# ๋จ์ด ๊ฒ์
print(f'์ฑ
: {kTe.searchWord("์ฑ
")}')
print(f'๋๋น : {kTe.searchWord("๋๋น")}')
print(f'์ฐํ : {kTe.searchWord("์ฐํ")}')
print(f'ํ์ : {kTe.searchWord("ํ์")}')
print(f'์ ์๋ : {kTe.searchWord("์ ์๋")}')
# ๋จ์ด ์ญ์
kTe.removeWord('์ฑ
')
# ์ฌ์ ์ถ๋ ฅ
kTe.printWords()
kTj = dic.KorToJpa()
# ๋จ์ด ๋ฑ๋ก
kTj.registWord('์ฑ
', 'ๆฌ')
kTj.registWord('๋๋น', '่ถ')
kTj.registWord('์ฐํ', '้็ญ')
kTj.registWord('ํ์', 'ๅญฆ็')
kTj.registWord('์ ์๋', 'ๅ
็')
# ๋จ์ด ์์
kTj.updateWord('์ฑ
', '่ถ')
kTj.updateWord('ํ์', 'ๅญฆ็')
# ๋จ์ด ๊ฒ์
print(f'์ฑ
: {kTj.searchWord("์ฑ
")}')
print(f'๋๋น : {kTj.searchWord("๋๋น")}')
print(f'์ฐํ : {kTj.searchWord("์ฐํ")}')
print(f'ํ์ : {kTj.searchWord("ํ์")}')
print(f'์ ์๋ : {kTj.searchWord("์ ์๋")}')
# ๋จ์ด ์ญ์
kTj.removeWord('์ฑ
')
# ์ฌ์ ์ถ๋ ฅ
kTj.printWords()
05 ์ฃผ์ฌ์ ๊ฒ์ ํด๋์ค๋ฅผ ๋ง๋ค๊ณ ์ปดํจํฐ์ ์ฌ์ฉ์์ ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๊ธฐ
# dice.py
import random as rd
class Dice:
def __init__(self):
self.cNum = 0
self.uNum = 0
def setCnum(self):
print('[Dice] setCnum()')
self.cNum = rd.randint(1, 6)
def setUnum(self):
print('[Dice] setUnum()')
self.uNum = rd.randint(1, 6)
def startGame(self):
print('[Dice] startGame()')
self.setCnum()
self.setUnum()
def printResult(self):
print('[Dice] printResult()')
if self.cNum == 0 or self.uNum == 0:
print('์ฃผ์ฌ์ ์ซ์ ์ค์ ์ ์
๋๋ค.')
else:
if self.cNum > self.uNum:
print(f'์ปดํจํฐ vs ์ ์ : {self.cNum} vs {self.uNum} >> ์ปดํจํฐ ์น!!')
elif self.cNum < self.uNum:
print(f'์ปดํจํฐ vs ์ ์ : {self.cNum} vs {self.uNum} >> ์ ์ ์น!!')
elif self.cNum == self.uNum:
print(f'์ปดํจํฐ vs ์ ์ : {self.cNum} vs {self.uNum} >> ๋ฌด์น๋ถ!!')
if __name__ == '__main__':
dc = Dice()
dc.startGame()
dc.printResult()
# ex.py
import dice
dc = dice.Dice()
dc.startGame()
dc.printResult()
06 ์๋์ฐจ ๊ฒฝ์ฃผ ๊ฒ์ ํด๋์ค ๋ง๋ค๊ธฐ
# 1. car_game ํด๋ ๋ง๋ ํ car.py, racing.py ์์ฑ
# car.py
import random
class Car:
def __init__(self, n='fire car', c='red', s=200):
self.name = n
self.color = c
self.max_speed = s
self.distance = 0
def printCarInfo(self):
print(f'name: {self.name}, '
f'color: {self.color}, '
f'max_speed: {self.max_speed}, '
f'current_speed: {self.cur_speed}')
def controlSpeed(self):
return random.randint(0, self.max_speed)
def getDistanceForHour(self):
return self.controlSpeed() * 1
if __name__ == '__main__':
tempCar = Car('myCar', 'black', 250)
tempCar.printCarInfo()
print(tempCar.controlSpeed())
# racing.py
from time import sleep
class CarRacing:
def __init__(self):
self.cars = []
self.rankings = []
def startRacing(self):
for i in range(10):
print(f'Racing: {i+1}๋ฐํด')
for car in self.cars:
car.distance += car.getDistanceForHour()
sleep(1)
self.printCurrentCarDistance()
def printCurrentCarDistance(self):
for car in self.cars:
print(f'{car.name}: {car.distance}\t\t', end='')
print()
def addCar(self, c):
self.cars.append(c)
# 2. car_game ํด๋ ๊ฒฝ๋ก์ ex.py ํ์ผ ์์ฑ
from car_game import racing as rc
from car_game import car
myCarGame = rc.CarRacing()
car01 = car.Car('Car01', 'white', 250)
car02 = car.Car('Car02', 'black', 200)
car03 = car.Car('Car03', 'yellow', 220)
car04 = car.Car('Car04', 'red', 280)
car05 = car.Car('Car05', 'blue', 150)
myCarGame.addCar(car01)
myCarGame.addCar(car02)
myCarGame.addCar(car03)
myCarGame.addCar(car04)
myCarGame.addCar(car05)
myCarGame.startRacing()
07 mp3 ํ๋ ์ด์ด ํด๋์ค๋ฅผ ๋ง๋ค๊ณ ๋
ธ๋ ๋ฑ๋ก ํ ์ฌ์ํ๊ธฐ
# mp3player.py
import random
from time import sleep
class Song:
def __init__(self, t, s, pt):
self.title = t
self.singer = s
self.play_time = pt
def printSongInfo(self):
print(f'Title: {self.title}, Singer: {self.singer}, Play time: {self.play_time}')
class Player:
def __init__(self):
self.songList = []
self.isLoop = False
def addSong(self, s):
self.songList.append(s)
def play(self):
if self.isLoop:
while self.isLoop:
for s in self.songList:
print(f'Title: {s.title}, Singer: {s.singer}, Play time: {s.play_time}sec')
sleep(s.play_time)
else:
for s in self.songList:
print(f'Title: {s.title}, Singer: {s.singer}, Play time: {s.play_time}sec')
sleep(s.play_time)
def suffle(self):
random.shuffle(self.songList)
def setIsLoop(self, flag):
self.isLoop = flag
# ex.py
import mp3player as mp3
s1 = mp3.Song('์ ํธ๋ฑ', '์ด๋ฌด์ง', 3)
s2 = mp3.Song('Permission to Dance', '๋ฐฉํ์๋
๋จ', 4)
s3 = mp3.Song('Butter', '๋ฐฉํ์๋
๋จ', 2)
s4 = mp3.Song('Weekend', 'TAEYEON', 5)
s5 = mp3.Song('์ข์์ข์', '์กฐ์ ์', 4)
player = mp3.Player()
player.addSong(s1)
player.addSong(s2)
player.addSong(s3)
player.addSong(s4)
player.addSong(s5)
player.setIsLoop(False)
player.suffle()
player.play()
01 ์ฐ์ ์ฐ์ฐ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๊ณ ์์๋๋ ์์ธ์ฒ๋ฆฌ ์ฝ๋ ์์ฑํ๊ธฐ
# calculator.py
def add(n1, n2):
print('๋ง์
์ฐ์ฐ')
try:
n1 = float(n1)
except:
print('์ฒซ ๋ฒ์งธ ํผ์ฐ์ฐ์๋ ์ซ์๊ฐ ์๋๋๋ค.')
return
try:
n2 = float(n2)
except:
print('๋ ๋ฒ์งธ ํผ์ฐ์ฐ์๋ ์ซ์๊ฐ ์๋๋๋ค.')
return
print(f'{n1} + {n2} = {n1 + n2}')
def sub(n1, n2):
print('๋บ์
์ฐ์ฐ')
try:
n1 = float(n1)
except:
print('์ฒซ ๋ฒ์งธ ํผ์ฐ์ฐ์๋ ์ซ์๊ฐ ์๋๋๋ค.')
return
try:
n2 = float(n2)
except:
print('๋ ๋ฒ์งธ ํผ์ฐ์ฐ์๋ ์ซ์๊ฐ ์๋๋๋ค.')
return
print(f'{n1} - {n2} = {n1 - n2}')
def mul(n1, n2):
print('๊ณฑ์
์ฐ์ฐ')
try:
n1 = float(n1)
except:
print('์ฒซ ๋ฒ์งธ ํผ์ฐ์ฐ์๋ ์ซ์๊ฐ ์๋๋๋ค.')
return
try:
n2 = float(n2)
except:
print('๋ ๋ฒ์งธ ํผ์ฐ์ฐ์๋ ์ซ์๊ฐ ์๋๋๋ค.')
return
print(f'{n1} * {n2} = {n1 * n2}')
def div(n1, n2):
print('๋๋์
์ฐ์ฐ')
try:
n1 = float(n1)
except:
print('์ฒซ ๋ฒ์งธ ํผ์ฐ์ฐ์๋ ์ซ์๊ฐ ์๋๋๋ค.')
return
try:
n2 = float(n2)
except:
print('๋ ๋ฒ์งธ ํผ์ฐ์ฐ์๋ ์ซ์๊ฐ ์๋๋๋ค.')
return
if n2 == 0:
print('0์ผ๋ก ๋๋ ์ ์์ต๋๋ค.')
return
print(f'{n1} / {n2} = {n1 / n2}')
# try:
# print(f'{n1} / {n2} = {n1 / n2}')
# except ZeroDivisionError as e:
# print(e)
# print('0์ผ๋ก ๋๋ ์ ์์ต๋๋ค.')
def mod(n1, n2):
print('๋๋จธ์ง ์ฐ์ฐ')
try:
n1 = float(n1)
except:
print('์ฒซ ๋ฒ์งธ ํผ์ฐ์ฐ์๋ ์ซ์๊ฐ ์๋๋๋ค.')
return
try:
n2 = float(n2)
except:
print('๋ ๋ฒ์งธ ํผ์ฐ์ฐ์๋ ์ซ์๊ฐ ์๋๋๋ค.')
return
if n2 == 0:
print('0์ผ๋ก ๋๋ ์ ์์ต๋๋ค.')
return
print(f'{n1} % {n2} = {n1 % n2}')
def flo(n1, n2):
print('๋ชซ ์ฐ์ฐ')
try:
n1 = float(n1)
except:
print('์ฒซ ๋ฒ์งธ ํผ์ฐ์ฐ์๋ ์ซ์๊ฐ ์๋๋๋ค.')
return
try:
n2 = float(n2)
except:
print('๋ ๋ฒ์งธ ํผ์ฐ์ฐ์๋ ์ซ์๊ฐ ์๋๋๋ค.')
return
if n2 == 0:
print('0์ผ๋ก ๋๋ ์ ์์ต๋๋ค.')
return
print(f'{n1} // {n2} = {int(n1 // n2)}')
def exp(n1, n2):
print('๊ฑฐ๋ญ์ ๊ณฑ ์ฐ์ฐ')
try:
n1 = float(n1)
except:
print('์ฒซ ๋ฒ์งธ ํผ์ฐ์ฐ์๋ ์ซ์๊ฐ ์๋๋๋ค.')
return
try:
n2 = float(n2)
except:
print('๋ ๋ฒ์งธ ํผ์ฐ์ฐ์๋ ์ซ์๊ฐ ์๋๋๋ค.')
return
print(f'{n1} ** {n2} = {n1 ** n2}')
# ex.py
import calculator as cc
num1 = input('์ฒซ ๋ฒ์งธ ํผ์ฐ์ฐ์ ์
๋ ฅ: ')
num2 = input('๋ ๋ฒ์งธ ํผ์ฐ์ฐ์ ์
๋ ฅ: ')
cc.add(num1, num2)
cc.sub(num1, num2)
cc.mul(num1, num2)
cc.div(num1, num2)
cc.mod(num1, num2)
cc.flo(num1, num2)
cc.exp(num1, num2)
02 1๋ถํฐ 1000๊น์ง์ ์์์ธ ๋์๋ฅผ 10๊ฐ ์์ฑํ๋ ์๊ตฌ๊ฐ ์๋๋ฉด ์ฌ์ฉ์ ์์ธ๊ฐ ๋ฐ์ํ๋๋ก ํ๋ ํ๋ก๊ทธ๋จ ๋ง๋ค๊ธฐ
# prime_module.py
class NotPrimeException(Exception):
def __init__(self, n):
super().__init__(f'{n} is not prime number.')
class PrimeException(Exception):
def __init__(self, n):
super().__init__(f'{n} is prime number.')
def isPrime(number):
flag = True
for n in range(2, number):
if number % n == 0:
flag = False
break
if flag == False:
raise NotPrimeException(number)
else:
raise PrimeException(number)
# ex.py
import random
import prime_module as pm
primeNumbers = []
n = 0
while n < 10:
rn = random.randint(2, 1000)
if rn not in primeNumbers:
try:
pm.isPrime(rn)
except pm.NotPrimeException as e:
print(e)
continue
except pm.PrimeException as e:
print(e)
primeNumbers.append(rn)
else:
print(f'{rn} is overlap number.')
continue
n += 1
print(f'PrimeNumbers: {primeNumbers}')
03 ์ด๊ตฌ๋งค๊ธ์ก์ ์ถ๋ ฅํ๋ ๊ฐ์๊ฐ ์๋ชป ์
๋ ฅ๋ ๊ฒฝ์ฐ ๋ณ๋๋ก ์ถ๋ ฅํ๋๋ก ํ๋ ํ๋ก๊ทธ๋จ ๋ง๋ค๊ธฐ
# calculatePurchase.py
g1price = 1200; g2price = 1000; g3price = 800
g4price = 2000; g5price = 900
def formatedNumber(n):
return format(n, ',')
def calculate(*gcs):
gcsDic = {}
againCntInput = {}
for idx, gc in enumerate(gcs):
try:
gcsDic[f'g{idx+1}'] = int(gc)
except Exception as e:
againCntInput[f'g{idx+1}'] = gc
print(e)
totalPrice = 0
for g in gcsDic.keys():
totalPrice += globals()[f'{g}price'] * gcsDic[g]
print('---------------------------')
print(f'์ด ๊ตฌ๋งค ๊ธ์ก: {formatedNumber(totalPrice)}์')
print('-------- ๋ฏธ๊ฒฐ์ ํญ๋ชฉ --------')
for g in againCntInput.keys():
print(f'์ํ: {g},\t ๊ตฌ๋งค ๊ฐ์: {againCntInput[g]}')
print('---------------------------')
# ex.py
import calculatePurchase as cp
g1Cnt = input('goods1 ๊ตฌ๋งค ๊ฐ์: ')
g2Cnt = input('goods2 ๊ตฌ๋งค ๊ฐ์: ')
g3Cnt = input('goods3 ๊ตฌ๋งค ๊ฐ์: ')
g4Cnt = input('goods4 ๊ตฌ๋งค ๊ฐ์: ')
g5Cnt = input('goods5 ๊ตฌ๋งค ๊ฐ์: ')
cp.calculate(g1Cnt, g2Cnt, g3Cnt, g4Cnt, g5Cnt)
04 ํ์ ๊ฐ์
ํ๋ก๊ทธ๋จ์ ๋ง๋ค๋ ์
๋ ฅํ์ง ์์ ํญ๋ชฉ์ด ์๋ ๊ฒฝ์ฐ ์๋ฌ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ ๋ง๋ค๊ธฐ
# mem.py
class EmptyDataException(Exception):
def __init__(self, i):
super().__init__(f'{i} is empty!')
def checkInputData(n, m, p, a, ph):
if n == '':
raise EmptyDataException('name')
elif m == '':
raise EmptyDataException('mail')
elif p == '':
raise EmptyDataException('password')
elif a == '':
raise EmptyDataException('address')
elif ph == '':
raise EmptyDataException('phone')
else:
return True
class RegisteMember():
def __init__(self, n, m, p, a, ph):
self.m_name = n
self.m_mail = m
self.m_pw = p
self.m_addr = a
self.m_phone = ph
print('Membership completed!!')
def printMemberInfo(self):
print(f'm_name: {self.m_name}')
print(f'm_mail: {self.m_mail}')
print(f'm_pw: {self.m_pw}')
print(f'm_addr: {self.m_addr}')
print(f'm_phone: {self.m_phone}')
# ex.py
import mem
m_name = input('์ด๋ฆ ์
๋ ฅ: ')
m_mail = input('๋ฉ์ผ ์ฃผ์ ์
๋ ฅ: ')
m_pw = input('๋น๋ฐ๋ฒํธ ์
๋ ฅ: ')
m_addr = input('์ฃผ์ ์
๋ ฅ: ')
m_phone = input('์ฐ๋ฝ์ฒ ์
๋ ฅ: ')
try:
mem.checkInputData(m_name, m_mail, m_pw, m_addr, m_phone)
newMember = mem.RegisteMember(m_name, m_mail, m_pw, m_addr, m_phone)
newMember.printMemberInfo()
except mem.EmptyDataException as e:
print(e)
05 ์ํ ๊ณ์ข ๊ฐ์ค ๋ฐ ์
/์ถ๊ธ ํ๋ก๊ทธ๋จ ๋ง๋ค๊ธฐ
# bank.py
import random
class PrivateBank:
def __init__(self, bank, account_name):
self.bank = bank
self.account_name = account_name
while True:
newAccountNo = random.randint(10000, 99999)
if bank.isAccount(newAccountNo):
continue
else:
self.account_no = newAccountNo
break
self.totalMoney = 0
bank.addAccount(self)
def printBankInfo(self):
print('-' * 40)
print(f'account_name: {self.account_name}')
print(f'account_no: {self.account_no}')
print(f'totalMoney: {self.totalMoney}์')
print('-' * 40)
class Bank:
def __init__(self):
self.accounts = {}
def addAccount(self, privateBank):
self.accounts[privateBank.account_no] = privateBank
def isAccount(self, ano):
return ano in self.accounts
def doDeposit(self, ano, m):
pb = self.accounts[ano]
pb.totalMoney = pb.totalMoney + m
def doWithdraw(self, ano, m):
pb = self.accounts[ano]
if pb.totalMoney - m < 0:
raise LackException(pb.totalMoney, m)
pb.totalMoney = pb.totalMoney - m
class LackException(Exception):
def __init__(self, m1, m2):
super().__init__(f'์๊ณ ๋ถ์กฑ!!, ์์ก: {m1}, ์ถ๊ธ์ก: {m2}')
# ex.py
import bank
koreaBank = bank.Bank()
new_account_name = input('ํต์ฅ ๊ณ์ค์ ์ํ ์๊ธ์ฃผ ์
๋ ฅ: ')
myAccount = bank.PrivateBank(koreaBank, new_account_name)
myAccount.printBankInfo()
while True:
selectNumber = int(input('1.์
๊ธ, \t2.์ถ๊ธ, \t3.์ข
๋ฃ '))
if selectNumber == 1:
m = int(input('์
๊ธ์ก ์
๋ ฅ: '))
koreaBank.doDeposit(myAccount.account_no, m)
myAccount.printBankInfo()
elif selectNumber == 2:
m = int(input('์ถ๊ธ์ก ์
๋ ฅ: '))
try:
koreaBank.doWithdraw(myAccount.account_no, m)
except bank.LackException as e:
print(e)
finally:
myAccount.printBankInfo()
elif selectNumber == 3:
print('Bye~')
break
else:
print('์๋ชป ์
๋ ฅํ์ต๋๋ค. ๋ค์ ์ ํํ์ธ์. ')
01 ํ์ ๊ณ์ ๋ณ ํ
์คํธ ํ์ผ์ ์์ฑํ ํ ํ์ ๋ณธ์ธ ํ์ผ์ ํ์ค ์ผ๊ธฐ๋ฅผ ์ฐ๊ณ ์ฝ๋ ํ๋ก๊ทธ๋จ ๋ง๋ค๊ธฐ
# diary.py
import time
def writeDiary(u, f, d):
lt = time.localtime()
timeStr = time.strftime('%Y-%m-%d %I:%M:%S %p', lt)
filePath = u + f
with open(filePath, 'a') as f:
f.write(f'[{timeStr}] {d}\n')
def readDiary(u, f):
lt = time.localtime()
timeStr = time.strftime('%Y-%m-%d %I:%M:%S %p', lt)
filePath = u + f
datas = []
with open(filePath, 'r') as f:
datas = f.readlines()
return datas
# ex.py
import diary
members = {}
uri = 'C:/pythonTxt/'
def printMembers():
for m in members.keys():
print(f'ID: {m} \t PW: {members[m]}')
while True:
selectNum = int(input('1.ํ์๊ฐ์
,\t 2.ํ์ค์ผ๊ธฐ์ฐ๊ธฐ,\t 3.ํ์ค์ผ๊ธฐ๋ณด๊ธฐ,\t 4.์ข
๋ฃ '))
if(selectNum == 1):
mId = input('input ID: ')
mPw = input('input PW: ')
members[mId] = mPw
printMembers()
elif(selectNum == 2):
mId = input('input ID: ')
mPw = input('input PW: ')
if mId in members and members[mId] == mPw:
print('login success!!')
fileName = 'myDaiary_' + mId + '.txt'
data = input('์ค๋ ํ๋ฃจ ์ธ์ ๊น์ ์ผ์ ๊ธฐ๋กํ์ธ์. ')
diary.writeDiary(uri, fileName, data)
else:
print('login fail!!')
printMembers()
elif (selectNum == 3):
mId = input('input ID: ')
mPw = input('input PW: ')
if mId in members and members[mId] == mPw:
print('login success!!')
fileName = 'myDaiary_' + mId + '.txt'
datas = diary.readDiary(uri, fileName)
for d in datas:
print(d, end='')
else:
print('login fail!!')
printMembers()
elif (selectNum == 4):
print('Bye~')
break
02 ์์
๊ณผ ์ง์ถ์ ๊ธฐ๋กํ๋ ๊ฐ๊ณ๋ถ ๋ง๋ค๊ธฐ
import time
limitSpendMoney = 2000
def getTime():
lt = time.localtime()
st = time.strftime('%Y-%m-%d %H:%M:%S')
return st
while True:
selectNumber = int(input('1.์
๊ธ \t 2.์ถ๊ธ \t 3.์ข
๋ฃ '))
if selectNumber == 1:
money = int(input('์
๊ธ์ก ์
๋ ฅ: '))
with open('C:/pythonTxt/bank/money.txt', 'r') as f:
m = f.read()
with open('C:/pythonTxt/bank/money.txt', 'w') as f:
f.write(str(int(m) + money))
memo = input('์
๊ธ ๋ด์ญ ์
๋ ฅ: ')
with open('C:/pythonTxt/bank/pocketMoneyRegister.txt', 'a') as f:
f.write('-------------------------------------------\n')
f.write(f'{getTime()} \n')
f.write(f'[์
๊ธ] {memo} : {str(money)}์ \n')
f.write(f'[์์ก] {str(int(m) + money)}์ \n')
print('์
๊ธ ์๋ฃ!!')
print(f'๊ธฐ์กด ์์ก : {m}')
print(f'์
๊ธ ํ ์์ก : {int(m) + money}')
elif selectNumber == 2:
money = int(input('์ถ๊ธ์ก ์
๋ ฅ: '))
with open('C:/pythonTxt/bank/money.txt', 'r') as f:
m = f.read()
with open('C:/pythonTxt/bank/money.txt', 'w') as f:
f.write(str(int(m) - money))
memo = input('์ถ๊ธ ๋ด์ญ ์
๋ ฅ: ')
with open('C:/pythonTxt/bank/pocketMoneyRegister.txt', 'a') as f:
f.write('-------------------------------------------\n')
f.write(f'{getTime()} \n')
f.write(f'[์ถ๊ธ] memo : {str(money)}์ \n')
f.write(f'[์์ก] {str(int(m) - money)}์ \n')
print('์ถ๊ธ ์๋ฃ!!')
print(f'๊ธฐ์กด ์์ก : {m}')
print(f'์ถ๊ธ ํ ์์ก : {int(m) - money}')
elif selectNumber == 3:
print('Bye~')
break
else:
print('์๋ชป ์
๋ ฅํ์
จ์ต๋๋ค.')
03 ์ซ์์ ์ฝ์์ ์์๋ฅผ ํ
์คํธ ํ์ผ์ ๊ธฐ๋กํ๊ธฐ (2๋ฌธ)
# ์ฝ์
inputNumber = int(input("0๋ณด๋ค ํฐ ์ ์ ์
๋ ฅ: "))
divisor = []
for number in range(1, (inputNumber + 1)):
if inputNumber % number == 0:
divisor.append(number)
if len(divisor) > 0:
try:
with open('C:/pythonTxt/divisor.txt', 'a') as f:
f.write(f'{inputNumber}์ ์ฝ์: ')
f.write(f'{divisor}\n')
except Exception as e:
print(e)
else:
print('divisor write complete!')
#์์
inputNumber = int(input("0๋ณด๋ค ํฐ ์ ์ ์
๋ ฅ: "))
prime = []
for number in range(2, (inputNumber + 1)):
flag = True
for n in range(2, number):
if number % n == 0:
flag = False
break
if (flag):
prime.append(number)
if len(prime) > 0:
try:
with open('C:/pythonTxt/prime.txt', 'a') as f:
f.write(f'{inputNumber}๊น์ง์ ์์: ')
f.write(f'{prime}\n')
except Exception as e:
print(e)
else:
print('prime write complete!')
04 ๋๊ฐ์ ์๋ฅผ ์
๋ ฅํ๋ฉด ๊ณต์ฝ์, ์ต๋๊ณต์ฝ์๋ฅผ ํ
์คํธ ํ์ผ์ ์์ฑํ๊ธฐ (3๋ฌธ)
# ์ ๋๊ฐ์ ๊ณต์ฝ์
num1 = int(input('1๋ณด๋ค ํฐ ์ ์ ์
๋ ฅ: '))
num2 = int(input('1๋ณด๋ค ํฐ ์ ์ ์
๋ ฅ: '))
common = []
for i in range(1, (num1 + 1)):
if num1 % i == 0 and num2 % i == 0:
common.append(i)
if len(common) > 0:
try:
with open('C:/pythonTxt/common.txt', 'a') as f:
f.write(f'{num1}์ {num2}์ ๊ณต์ฝ์: ')
f.write(f'{common}\n')
except Exception as e:
print(e)
else:
print('common factor write complete!')
# ์ ๋๊ฐ์ ์ต๋๊ณต์ฝ์
num1 = int(input('1๋ณด๋ค ํฐ ์ ์ ์
๋ ฅ: '))
num2 = int(input('1๋ณด๋ค ํฐ ์ ์ ์
๋ ฅ: '))
maxComNum = 0
for i in range(1, (num1 + 1)):
if num1 % i == 0 and num2 % i == 0:
maxNum = i
try:
with open('C:/pythonTxt/maxComNum.txt', 'a') as f:
f.write(f'{num1}์ {num2}์ ์ต๋๊ณต์ฝ์: {maxNum}\n')
except Exception as e:
print(e)
else:
print('max common factor write complete!')
# ์ ์ธ๊ฐ์ ๊ณต์ฝ์
num1 = int(input('1๋ณด๋ค ํฐ ์ ์ ์
๋ ฅ: '))
num2 = int(input('1๋ณด๋ค ํฐ ์ ์ ์
๋ ฅ: '))
num3 = int(input('1๋ณด๋ค ํฐ ์ ์ ์
๋ ฅ: '))
common = []
for i in range(1, (num1 + 1)):
if num1 % i == 0 and num2 % i == 0 and num3 % i == 0:
common.append(i)
if len(common) > 0:
try:
with open('C:/pythonTxt/common.txt', 'a') as f:
f.write(f'{num1}, {num2}, {num3}์ ๊ณต์ฝ์: ')
f.write(f'{common}\n')
except Exception as e:
print(e)
else:
print('common factor write complete!')
#์ ์ธ๊ฐ์ ์ต๋๊ณต์ฝ์
num1 = int(input('1๋ณด๋ค ํฐ ์ ์ ์
๋ ฅ: '))
num2 = int(input('1๋ณด๋ค ํฐ ์ ์ ์
๋ ฅ: '))
num3 = int(input('1๋ณด๋ค ํฐ ์ ์ ์
๋ ฅ: '))
maxComNum = 0
for i in range(1, (num1 + 1)):
if num1 % i == 0 and num2 % i == 0 and num3 % i == 0:
maxComNum = i
try:
with open('C:/pythonTxt/maxComNum.txt', 'a') as f:
f.write(f'{num1}, {num2}, {num3}์ ์ต๋๊ณต์ฝ์: {maxComNum}\n')
except Exception as e:
print(e)
else:
print('max common factor write complete!')
05 ๋ฐฐ(๊ณผ์ผ)๊ฐ ์
ํญํ๋ ๋ ์ง๋ฅผ ํ
์คํธ ํ์ผ์ ๊ธฐ๋กํด๋ณด๊ธฐ
ship1 = 3; ship2 = 4; ship3 = 5
maxDay = 0
for i in range(1, (ship1 + 1)):
if ship1 % i == 0 and ship2 % i == 0:
maxDay = i
# print('์ต๋๊ณต์ฝ์: {}'.format(maxDay))
minDay = (ship1 * ship2) // maxDay
# print('{}, {}์ ์ต์๊ณต๋ฐฐ์: {}'.format(ship1, ship2, minDay))
newDay = minDay
for i in range(1, (newDay + 1)):
if newDay % i == 0 and ship3 % i == 0:
maxDay = i
# print('์ต๋๊ณต์ฝ์: {}'.format(maxDay))
minDay = (newDay * ship3) // maxDay
# print('{}, {}, {}์ ์ต์๊ณต๋ฐฐ์: {}'.format(ship1, ship2, ship3, minDay))
# print('{}์ผ๋ง๋ค ๋ชจ๋ ์ ๋ฐ์ด ์
ํญํฉ๋๋ค.'.format(minDay))
from datetime import datetime
from datetime import timedelta
n = 1
baseTime = datetime(2021, 1, 1, 10, 0, 0)
# baseTime = datetime.now()
# print(f'2021๋
๋ชจ๋ ์ ๋ฐ ์
ํญ์ผ: {baseTime}')
with open('C:/pythonTxt/arrive.txt', 'a') as f:
f.write(f'2021๋
๋ชจ๋ ์ ๋ฐ ์
ํญ์ผ\n')
f.write(f'{baseTime}\n')
nextTime = baseTime + timedelta(days=minDay)
while True:
# print(f'2021๋
๋ชจ๋ ์ ๋ฐ ์
ํญ์ผ: {nextTime}')
with open('C:/pythonTxt/arrive.txt', 'a') as f:
f.write(f'{nextTime}\n')
nextTime = nextTime + timedelta(days=minDay)
if nextTime.year > 2021:
break