전체 글
-
[file] copy the file🐍 파이썬/파이썬 연습문제 2022. 7. 17. 07:42
문제 : Implement a function that copies the contents of a file to a new file. The function should add a header (one line of arbitrary text) to the top def copy_file(src, dest, header): with open (src, 'r') as f_in , open (dest, 'w') as f_out: f_out.write(header) for line in f_in.readlines: f_out.write(line)
-
working with file🐍 파이썬/파이썬 기본 문법 2022. 7. 17. 07:39
create file # opening a file in write mode will either create a new file (if the file does not exist) # or overwrite an existing one f = open('test.txt', 'w') read file f = open('test.txt', 'r') contents = f.read() # read the entire file content print(contents) contents = f.read(10) # read only the first 10 characters of the file print(contents) line = f.readline() # read first line of the file ..
-
[random/inf loop] guessing game🐍 파이썬/파이썬 연습문제 2022. 7. 17. 07:26
문제 : Assign a random value to a variable and ask the user to guess the value. If the guess is lower than the chosen value, print ‘too low’, if it is greater, print ‘too high’. In case the number is correct, ‘correct’ and terminate the program. take home: random library와 while True / break 구문을 활용한 infinite loop 작성하기 import random rnd = random.randint (0, 100) while True: guess = int input("guess ..
-
[input/string interpolation] 유저에게 인풋을 받아서 프린트하기🐍 파이썬/파이썬 연습문제 2022. 7. 17. 06:58
생각보다 쓰일일이없어서 검색하지않고서는 기억이 안날수있는 문법중에하나인 input function과 헷갈리는 string interpolation ! 문제 : Ask from the user the following information: name, age, occupation and print the following message using the provided values: Hi NAME! I see you are AGE years old and work as a OCCUPATION. # ask and store what the user has typed in a variable name = input('What is your name?') age = input('How old are you?')..
-
[modulo/divisor] Vending machine change 거스름돈 문제🐍 파이썬/파이썬 연습문제 2022. 7. 17. 06:52
처음에는 생각해내기 어렵지만 사실 엄청 많이 쓰이는 모듈로와 디비전 연산 ! 문제 : 주어진 금액을 최소한의 갯수의 동전으로 거슬러주는 프로그래밍을 작성하시오 동전의 종류는 다음과 같다; quarters –25 cents, dime –10 cents, nickel –5 cents, pennies –1 cent def change(amount): quarters = amount // 25 rest_amount = amount % 25 dimes = rest_amount // 10 rest_amount %= 10 nickels = rest_amount // 5 rest_amount %= 5 pennies = rest_amount print('%d quarters %d dimes %d nickels %d pe..
-
[string] 문자열 자주 쓰이는 문법 정리🐍 파이썬/파이썬 기본 문법 2022. 7. 17. 06:42
String indexing String formatting String method 1.대소문자 변환 .upper(), .lower() 메소드 (비파괴적 함수) a = "hello" a.upper() 2. 공백제거 .strip(), lstrip(), rstrip() a = " hello " a.strip() 3.구성 # moethods result boolean s = "some string..." s.startswith("some") # Bool: True s.endswith("!") # Bool: False "xxx" in s # Bool: False "1".isdigit() # Bool: True "e".isupper () # Bool: False "r".islower () # Bool: True..