반응형
-
[파이썬]함수 - 매개변수 지정호출, args, kwargs, 초기값 설정, 전역 변수 이번 포스팅에서는 함수의 특성을 다룬다. 매개변수 지정 호출, 입력값이 몇 개인지 모를 때 다수의 입력값을 받는 *args, **kwargs, 변수의 초기값 설정 방법 등에 대해 알아보자. 매개변수 지정 호출 매개변수 지정 호출 파이썬의 함수에서는 매개변수를 지정하여 호출할 수 있는데, 이게 무슨 말인지 코드를 보면서 확인해보자. def fahr_or_cel(sort, temp): if sort == 'to_fahr': fahr = (temp * 9/5) + 32 print('섭씨 {}℃ 화씨로 변환: {}℉'.format(temp, fahr)) elif sort == 'to_cel': cel = (temp - 32) * 5/9 print('화씨 {}℉ 섭씨로 변환: {}℃'.format(temp, ce..
-
[파이썬]while, for 반복문 탈출과 계속하기 - break, continue 이번 포스팅에서는 while과 for 같은 반복문에서 조건문이 True인 경우에도 반복문을 탈출할 수 있게 해주는 break와 반복문의 시작 지점으로 돌아가게 해주는 continue에 대해 알아본다. 반복문 강제로 탈출하기 - break break로 반복문 강제로 탈출하기 while문의 조건문이 True일 경우에도 break로 while문을 빠져나올 수 있다. count = 0 while True: count += 1 print('현재 카운트 %d' %count) if count == 5: break print('while문 탈출') #결과 출력 현재 카운트 1 현재 카운트 2 현재 카운트 3 현재 카운트 4 현재 카운트 5 while문 탈출 위 예문을 보면 조건문은 항상 True이기 때문에 while문..
-
43. I was going to say 1. 예문 - A: Doesn't she kind of remind you of Cindy? B: Yeah, I was going to say. C: Oh, that's right! No wonder she looked so familiar. - A: Hmm. It looks like it's going to start raining any minute. B: Yeah, I was going to say. I didn't hear anything about any rain today. Did you? A: I hardly check the forecast anymore. B: My app says it's just going to be cloudy all day. A: Well, w..
-
[파이썬]문자열 함수 - 값 찾기, 개수 세기, 합치기, 나누기 파이썬은 문자열 자료형에 사용할 수 있는 다양한 내장 함수를 가지고 있다. 이번 글에서는 문자의 위치를 알려주는 함수 find, index와 문자열에서 특정 문자의 개수를 세는 count 함수, 문자열을 합쳐주고 구분하는 join 함수에 대해 포스팅한다. 함수는 문자열 변수에 '.'를 붙여 사용한다. 문자열 관련 함수들을 보려면 dir(str)을 터미널 창에 입력하면 된다. >>> dir(str) ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__',..
-
47. for all I know 1. 예문 - For all I know, I could be making it worse. - For all we know, they could already be gone. - For all they know, we could be brothers and sisters. - A: So, how much did you say you would pay for the laptop. B: Oh, that? I actually called off the deal. A: How come? B: I just didn't feel comfortable doing it over mail. He might not even have the laptop for all I know. A: Didn't yout..
-
8. keep up with(catch, fall) 1. 예문 - We can't keep up with all the changes. - I couldn't keep up with the rest of the class. - They're struggling to keep up with their competitors. - A: So, what's new in AI? B: I haven't really been keeping up with the latest, but not a whole lot I think. A: Maybe that's a good thing. I remember people saying that they need to slow down the development. B: They're still saying that, b..
-
1. for now 1. 예문 - Let's just use this for now. - That's it for now. - I know you want to help, but you're still new and it'll be a while before you can start doing any real work. So for now, just focus on learning as much as you can. 2. 응용 및 심화 - I think we've done enough for today. - It looks like they're closed for the day. Let's come back tomorrow. - Thanks, but I'm good. I think I've had enough to dri..
-
[파이썬]짝수와 홀수 정리하기 이번 포스팅에서는 일련의 숫자들을 받아 홀수와 짝수를 받아 정렬하여 출력하는 코드를 작성해보자. 홀수와 짝수 정렬하기 다만 홀수와 짝수의 개수가 다를 경우 숫자를 같게 맞추며 홀수는 오름차순, 짝수는 내림차순으로 번갈아가면서 출력을 해야 한다. 아래 예시를 보자 1 6 3 5 2 7 4 → 1 6 3 4 5 2 홀수는 1 3 5 7, 짝수는 6 2 4가 들어왔는데, 짝수와 홀수의 개수가 맞지 않아 짝수처럼 3개로 맞춘 후 홀수는 오름차순 짝수는 내림차순으로 번갈아가면서 출력이 돼야 한다. 입력값 받기 user_input = input("숫자들을 공백없이 입력하세요> ") nums_list = set([int(x) for x in '_'.join(user_input).split('_')]) print(nu..
-
[파이썬]숫자 맞추기 게임 만들기 이번 포스팅에선 파이썬을 활용하여 1부터 99까지의 랜덤한 숫자를 10번 안에 찾는 게임을 만들어보자. 숫자 맞추기 게임 만들기 랜덤한 수 만들기 1에서 99까지 랜덤한 수를 만들어 보자. random 라이브러리를 활용해보자. import random num_random = random.randint(1, 99) 반복문 만들기 그 다음 10번 동안 문제를 내는 반복문을 만들어보자. coin = 10 while coin: coin-=1 print("남은 코인 개수: {}".format(coin)) 조건문 만들기, 입력받기 coin = 10 num = 0 while coin: coin -= 1 num = int(input("1~99 사이의 숫자를 맞춰보세요")) if num == num_random: pri..
반응형