์์ฃผ ๊ฐ๋ฅ ๋์ด ๊ณ์ฐ๊ธฐ ๋ง๋ค๊ธฐ
- input์ ์ค์ง 1๊ฐ์ argument(์ธ์)๋ง ๋ฐ์ต๋๋ค.
- ์๋ฐ์ดํ๋ฅผ ๋ถ์ด๊ณ ์์ ๋ด์ฉ์ ์์ฑํ๋ฉด ์ฌ์ฉ์์๊ฒ ๋ฌป๋ ์ง๋ฌธ์ด ๋ฉ๋๋ค.
- ๋จ์ํ print๊ฐ ์๋๋ผ, value๋ฅผ printํ๊ฐ๋ํ์ง๋ง, ์ฌ์ฉ์์ ์๋ต์ ๊ธฐ๋ค๋ฆฝ๋๋ค.
- ์๋ต ๋๊ธฐ ์ํ์ด๋ฉฐ, ํ๋ก๊ทธ๋จ์ด ์์ง ๋๋์ง ์์ ์ํ์
๋๋ค.
- ์๋ต์ ๋ฐ์์ผ ๊ทธ๋ ํ๋ก๊ทธ๋จ์ด ๋๋ฉ๋๋ค.
- ๊ทธ ์๋ต๊ฐ์ด ์ด ํจ์์ return๊ฐ์
๋๋ค.
- print์ ๋ง์ฐฌ๊ฐ์ง๋ก, input์ ๋นํธ ์ธ ํจ์์
๋๋ค.
- typeํจ์๋ ๋ณ์์ type์ ์๋ ค์ฃผ๋ ํจ์์
๋๋ค.
- intํจ์๋ ๊ฐ์ ์ซ์ ํ์
์ผ๋ก ๋ณ๊ฒฝํด์ฃผ๋ ํจ์์
๋๋ค.
- user๋ ์์ ์ซ์๊ฐ ๋ค์ด์๋ string๊ฐ์ผ๋ก ์๋ตํ๊ณ , ๊ทธ ๊ฐ์ int๊ฐ ์ซ์ ํ์
์ผ๋ก ๋ณ๊ฒฝํฉ๋๋ค.
age = int(input("How old are you?"))
print("user answer", age)
print(type(age))
# How old are you? 12
# user anser 12
# <class 'int'>
- ๋ ๊ฐ์ง์ ์กฐ๊ฑด์ ๋์์ ๋ง์กฑํฉ๋๋ค.
- ๋ ๊ฐ์ง์ ์กฐ๊ฑด์ด ๋ชจ๋ ์ฐธ(True)์ฌ์ผ ์กฐ๊ฑด ์คํ๋ฌธ์ ์ดํํฉ๋๋ค.
- ๋ ๊ฐ์ง ์กฐ๊ฑด ์ค์ ํ๋๋ง ๋ง์กฑํด๋ ์ฐธ(True)์
๋๋ค.
- ๋ ์ค์ ํ ๊ฐ์ง ์กฐ๊ฑด๋ง ๋ง์กฑํ๋ฉด ์กฐ๊ฑด๋ฌธ์ ์คํํฉ๋๋ค.
age = int(input("๋์ด๊ฐ ์ด๋ป๊ฒ ๋์ธ์?"))
if age < 18:
print("์์ง ์ ์ ๋ง์๋ฉด ์๋ฉ๋๋ค.")
elif age > 18 and age < 35:
print("์ด๋ฅธ์
๋๋ค.")
elif age == 60 or age == 70:
print("๊ฑด๊ฐํ์ธ์!")
else:
print("๋งฅ์ฃผ๐บ ํ์, ํ์ธ์!")
- ํ๋ฆ์ ์ด(Control Flow)
- while์ if์ ๊ฐ์ ๋ ๋ค๋ฅธ ์ข
๋ฅ์ ์กฐ๊ฑด๋ฌธ์
๋๋ค.
- ์กฐ๊ฑด์ ๋ง์กฑํ๋ ๋์ ๊ณ์ ์คํ๋ฌธ์ ๋ฐ๋ณตํฉ๋๋ค.
distance = 0
while distance < 5:
print("I'm running:", distance, "km")
distance = distance + 1
"""
I'm running: 0km
I'm running: 1km
I'm running: 2km
I'm running: 3km
I'm running: 4km
"""
- pc_choice๋ฅผ randomํ๊ฒ ์ ํฉ๋๋ค.
- Python Standard Library ์ฐธ๊ณ
- ํด๋น ์ธ์ด์ ๊ธฐ๋ณธ์ ์ผ๋ก ํฌํจ๋ ํจ์(๋นํธ์ธ)ํจ์์ ๋ํ ๋ฌธ์๋ฅผ ๋งํฉ๋๋ค.
- random.randint ํจ์๋ฅผ ์ฌ์ฉํฉ๋๋ค.
- random์ ๋ชจ๋ ์ด๋ฆ์ผ๋ก, ํด๋น ํ์ด์ง๋ก import๊ฐ ํ์ํฉ๋๋ค.
- ์คํ ๋ค๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์ฌ, function์ ๋ชจ๋์์ ๊ฐ์ ธ์ต๋๋ค.
- random๋ชจ๋์ randint(a,b)ํจ์๋ a๋ N๋ณด๋ค ์๊ฑฐ๋ ๊ฐ๊ณ , b๋ N๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ต๋๋ค.
- if/elif/while์ ์ฌ์ฉํ์ฌ ์์ฑํฉ๋๋ค.
- ๊ฒ์์ด ์ข
๋ฃ๋๋ฉด ๋ค์ ์์ํ ์ง ์กฐ๊ฑด์ ์์ฑํฉ๋๋ค.
from random import randint
print("์, ๊ฒ์์ ์์ํ์ง! ๐คก ๐ซ")
playing = True
while playing:
pc_choice = randint(1,20)
user_choice = int(input("์ซ์๋ฅผ ์ ํํ์ธ์ ๐"))
while user_choice != pc_choice:
if user_choice > pc_choice:
print("๋ค์ ๊ณ ๋ฅด์ธ์, ๋ ์์๊ฐ์ผ๋ก ๐")
elif user_choice < pc_choice:
print("๋ค์ ๊ณ ๋ฅด์ธ์, ๋ ํฐ ๊ฐ์ผ๋ก ๐")
user_choice = int(input("์ซ์๋ฅผ ์ ํํ์ธ์ ๐"))
if user_choice == pc_choice:
print("๋น์ ์ด ์ด๊ฒผ์ต๋๋ค. ๐๏ธ ์ ๋ต์", pc_choice)
restart = input("๋ค์ ํ ๊น์? ๋ค/์๋์ค ")
if restart == "๋ค":
playing = True
elif restart == "์๋์ค":
playing = False
print("๊ณ์ฐ์ ํด๋ด
์๋ค ๐ค")
calculation = True
while calculation:
value_01 = int(input("Choose a number : "))
print(value_01)
value_02 = int(input("Choose another one : "))
print(value_02)
operation = input("Options are: +,-,* or / Wirte 'exit' to finish!")
if operation == "+":
print("Result : ", value_01 + value_02)
elif operation == "-":
print("Result : ", value_01 - value_02)
elif operation == "*":
print("Result : ", value_01 * value_02)
elif operation == "/":
print("Result : ", value_01 / value_02)
elif "exit":
calculation = False
print("๊ณ์ฐ์ ์ข
๋ฃํฉ๋๋ค ๐")