[처음 시작하는 파이썬] ch5. 연습문제

2023. 1. 20. 01:29파이썬

#  5.1
#  m으로 시작하는 단어를 대문자로 만들어보자.

song = """When an eel grabs your arm,
And it causes great harm,
That's - a moray!"""

 

 

THINK1.

print(song.replace('m', 'M'))
=>When an eel grabs your arM,
And it causes great harM,
That's - a Moray!

 

아닌듯..

 

THINK2.

moray = (song[-6:])
moray = (moray.capitalize())

print(moray)
=> Moray!

 

모레이 슬라이스해서 어떻게 해보려다가 문득 생각이 나서..

 

THINK3.

split()을 써본다.

song_list = (song.split(' '))
print(song_list)

song_list[-1] = (song_list[-1].capitalize())
print(song_list)

print(' '.join(song_list))
['When', 'an', 'eel', 'grabs', 'your', 'arm,', 'And', 'it', 'causes', 'great', 'harm,', "That's", '-', 'a', 'moray!']
['When', 'an', 'eel', 'grabs', 'your', 'arm,', 'And', 'it', 'causes', 'great', 'harm,', "That's", '-', 'a', 'Moray!']

When an eel grabs your arm, And it causes great harm, That's - a Moray!

성공!.!

 

응용

 

#  5.2
# 다음과 같은 형식으로 각 리스트의 질문과 답을 차례로 출력해보자.

print('Q: question')
print('A: answer')

questions = [ "We don't serve strings around here. Are you a string?",
              "What is said on Father's Day in the forest?",
              "What makes the sound 'Sis! Boom! Bah!'?"]

answers = [ "An exploding sheep.",
            "No, I'm a frayed knot.",
            "'Pop!' goes the weasel."]

바로 알겠다!

print('Q: ', questions[0])
print('A: ', answers[0])
print('Q: ', questions[1])
print('A: ', answers[1])
print('Q: ', questions[2])
print('A: ', answers[2])
Q: We don't serve strings around here. Are you a string?
A: An exploding sheep.
Q: What is said on Father's Day in the forest?
A: No, I'm a frayed knot.
Q: What makes the sound 'Sis! Boom! Bah!'?
A: 'Pop!' goes the weasel.

 

THINK1.

print('''My kitty cat likes %s, 
My kitty cat likes %s, 
My kitty cat fell on his %s And now thinks he's a %s. ''' % (roast beef, ham, head, clam))
SyntaxError: invalid syntax. Perhaps you forgot a comma?

엥..? 왜 안돼..

 

THINK2.

print('''My kitty cat likes %s, 
My kitty cat likes %s, 
My kitty cat fell on his %s And now thinks he's a %s. ''' % ('roast beef', 'ham', 'head', 'clam'))
My kitty cat likes roast beef, My kitty cat likes ham, My kitty cat fell on his head And now thinks he's a clam.

바보구나 ㅎ.ㅎ '문자열'

 

'''
5.4
새 스타일의 포매팅을 사용하여 메일을 써보자.
다음 문자열을 letter 변수에 저장한다.

    Dear {salutation} {name},

    Thank you for your letter. We are sorry that our {product} {verbed} in your
{room}. Please note that it should never be used in a {room}, especially near any
{animals}.

    Send us your receipt and {amount} for shipping and handling. We will send you
another {product} that, in our tests, is {percent}% less likely to have {verbed}.

    Thank you for your support.
    Sincerely,
    {spokeman}
    {job_title}

'''
letter = ('''
    Dear {salutation} {name},

    Thank you for your letter. We are sorry that our {product} {verbed} in your
{room}. Please note that it should never be used in a {room}, especially near any 
{animals}.

    Send us your receipt and {amount} for shipping and handling. We will send you
another {product} that, in our tests, is {percent}% less likely to have {verbed}.

    Thank you for your support.
    Sincerely,
    {spokeman}
    {job_title}
      ''')

 

'''
5.5
문자열 변수 'salutation', 'name', 'product', 'verbed', 'room',
'animals', 'amount', 'percent', 'spokeman', 'job_title'에
값을 할당한다. 문자열 변수와 letter.format()를 사용하여
메일을 출력해보자.
'''
salutation = 'sal'
name = 'na'
product = 'prod'
verbed = 'ver'
room = 'ro'
animals = 'ani'
amount = 'amo'
percent = '100'
spokeman = 'spo'
job_title = 'job'

그냥 간단하게 이렇게 썼어요.

 

letter = ('''
    Dear {} {},

    Thank you for your letter. We are sorry that our {} {} in your
{}. Please note that it should never be used in a {}, especially near any 
{}.

    Send us your receipt and {} for shipping and handling. We will send you
another {} that, in our tests, is {}% less likely to have {}.

    Thank you for your support.
    Sincerely,
    {}
    {}
      ''')

letter {} 안 다 비워주고

 

print(letter.format(salutation, name, product, verbed, room, room, animals, amount, product, percent, verbed, spokeman, job_title))

순서에 맞게끔 format 안을 변수로 채워줍니다.

 

Dear sal na,
Thank you for your letter. We are sorry that our prod ver in your ro.
Please note that it should never be used in a ro, especially near any ani.

Send us your receipt and amo for shipping and handling.
We will send you another prod that, in our tests, is 100% less likely to have ver.
Thank you for your support. Sincerely, spo job

잘 출력됩니다.

 

'''
5. 6
어떤 물건에 이름을 짓는 여론조사를 실시한 후 다음과 같은 패턴이 나타났다.
- an English submarine 이름은 (Boaty McBoatface)로 지어졌다.
- an Austrailian racehorse 이름은 (Horsey McHorseface)로 지어졌다.
- a Swedish train 이름은 (Trainy McTrainface)로 지어졌다.

문자열 'duck', 'gourd', 'spitz'과 % 포매팅을 사용하여 위 괄호 안의 밑줄 문자열의
형식과 같이 출력해보자.

[출력]
Ducky McDuckface
Gourdy McGourdface
Spitzy McSpitzface
'''
duck = 'Ducky McDuckface'
gourd = 'Gourdy McGourdface'
spitz = 'Spitzy McSpitzface'

print('''an English submarine 이름은 %s로 지어졌다.
an Austrailian racehorse 이름은 %s로 지어졌다.
a Swedish train 이름은 %s로 지어졌다.
''' % (duck, gourd, spitz))
an English submarine 이름은 Ducky McDuckface로 지어졌다.
an Austrailian racehorse 이름은 Gourdy McGourdface로 지어졌다.
a Swedish train 이름은 Spitzy McSpitzface로 지어졌다.

 

'''
5.7
format() 메서드를 사용하여 문제 5.6을 풀어본다.
'''
print('''an English submarine 이름은 {}로 지어졌다.
an Austrailian racehorse 이름은 {}로 지어졌다.
a Swedish train 이름은 {}로 지어졌다.
'''.format(duck, gourd, spitz))
an English submarine 이름은 Ducky McDuckface로 지어졌다.
an Austrailian racehorse 이름은 Gourdy McGourdface로 지어졌다.
a Swedish train 이름은 Spitzy McSpitzface로 지어졌다.

 

'''
5.8
f-문자열을 사용하여 문제 5.6을 풀어본다.
'''
print(f'''an English submarine 이름은 {duck}로 지어졌다.
an Austrailian racehorse 이름은 {gourd}로 지어졌다.
a Swedish train 이름은 {spitz}로 지어졌다.
''')
an English submarine 이름은 Ducky McDuckface로 지어졌다.
an Austrailian racehorse 이름은 Gourdy McGourdface로 지어졌다.
a Swedish train 이름은 Spitzy McSpitzface로 지어졌다.