有会用PYTHON 语言的朋友,请帮个忙

今天

知名会员
注册
2003-11-08
消息
1,288
荣誉分数
471
声望点数
193
孩子在学PYTHON 卡在这道题上了,请指点一下

Instructions

Write a function upper_lower that consumes a string plainand produces a string with the same characters as in plainexcept that all letters in the first half of the string are in upper case and all the rest of the letters are in lower case. Non-letters should be the same.

If the length of the string is odd, the "first half" should contain the middle character.

Warning: Make sure to use the name of the function given in the instructions.
 
first split string in two halves, then
first half
$ tr a-z A-Z
2nd half
$ tr A-Z a-z
:evil:
 
def upper_lower(string):
=
孩子在学PYTHON 卡在这道题上了,请指点一下

Instructions

Write a function upper_lower that consumes a string plainand produces a string with the same characters as in plainexcept that all letters in the first half of the string are in upper case and all the rest of the letters are in lower case. Non-letters should be the same.

If the length of the string is odd, the "first half" should contain the middle character.

Warning: Make sure to use the name of the function given in the instructions.

代码:
def upper_lower(string):
    length = len(string)

    middle = length//2

    if length % 2:
        middle += 1

    return string[:middle].upper() + string[middle:].lower()
 
最后编辑:
first split string in two halves, then
first half
$ tr a-z A-Z
2nd half
$ tr A-Z a-z
:evil:
def upper_lower(string):
=


代码:
def upper_lower(string):
    length = len(string)

    middle = length//2

    if length % 2:
        middle += 1

    return string[:middle].upper() + string[middle:]
谢谢,我让他试试
 
后退
顶部
首页 论坛
消息
我的