Python:每日一题 75(答题领鱼币)
本帖最后由 新手·ing 于 2017-8-10 08:49 编辑&名字问题&
My friend wants a new band name for her band. She like bands that use the formula: 'The' + a noun with first letter capitalized.
dolphin -> The Dolphin
However, when a noun STARTS and ENDS with the same letter, she likes to repeat the noun twice and connect them together with the first and last letter, combined into one word like so (WITHOUT a 'The' in front):
alaska -> Alaskalaska
europe -> Europeurope
Can you write a function that takes in a noun as a string, and returns her preferred band name written as a string?
中文版:
**** Hidden Message *****
答案:
**** Hidden Message ***** 本帖最后由 shinemic 于 2017-8-10 09:07 编辑
由于不清楚输入的字符是否全部小写以及输出是否要求严格「仅首字母大写」,试写了两个版本:
def preferred_band_name_raw(rawstr):
if rawstr.lower() == rawstr[-1].lower():
tmp = rawstr * 2
return rawstr.upper() + tmp
else:
return 'The ' + rawstr.upper() + rawstr
def preferred_band_name(rawstr):
if rawstr.lower() == rawstr[-1].lower():
return (rawstr + rawstr * 2).title()
else:
return 'The ' + rawstr.title()
print('preferred_band_name_raw:')
print(preferred_band_name_raw('Dolphin'))
print(preferred_band_name_raw('DolphiN'))
print(preferred_band_name_raw('Dolphid'))
print(preferred_band_name_raw('DolphiD'))
print('preferred_band_name:')
print(preferred_band_name('Dolphin'))
print(preferred_band_name('DolphiN'))
print(preferred_band_name('Dolphid'))
print(preferred_band_name('DolphiD'))输出:preferred_band_name_raw:
The Dolphin
The DolphiN
Dolphidolphid
DolphiDolphiD
preferred_band_name:
The Dolphin
The Dolphin
Dolphidolphid
Dolphidolphid手法低劣,还望大Lao指正!{:10_270:} 本帖最后由 小Q学Python 于 2017-8-10 09:40 编辑
def test(n):
n = n.lower()
if n==n[-1]:
return n.upper()+n*2
else:
return 'The '+ n.upper()+n def test(string):
return 'The %s' % string.capitalize() if string != string[-1] else (string*2).capitalize() 支持楼主{:10_270:} #def name():
usrinput = input('plz input a world:')
usrinput.lower()
if usrinput==usrinput[-1]:
usrinput = usrinput+usrinput
return('The '+usrinput.title())
# def bandName(str):
if str == str[-1]:
band_name = str.capitalize() + str
return band_name
else:
band_name = 'The ' +str.capitalize()
return band_name
name = input("input your band name: ")
print("your band name is " + bandName(name.lower()))
word = input('please input a word:')
if word == word[-1]:
band_name = (word + word).capitalize()
else:
band_name = 'The ' + word.capitalize()
print(band_name) 中文版 def fun(s):
return (s[:-1] + s).capitalize() if s == s[-1] else ('The ' + s) 呐呢{:5_94:} 本帖最后由 chunchun2017 于 2017-8-12 10:20 编辑
def NewBand(str):
str0=str.capitalize()
if(str==str[-1]):
str0+=str
else:
str0='The '+str0
print(str0)
name = input("Please input The band name:")
NewBand(name)
运行结果:
====================== RESTART: F:\Python3\code\名字问题.py ======================
Please input The band name:dolphin
The Dolphin
>>>
====================== RESTART: F:\Python3\code\名字问题.py ======================
Please input The band name:alaska
Alaskalaska
>>>
====================== RESTART: F:\Python3\code\名字问题.py ======================
Please input The band name:europe
Europeurope
>>>
上面的代码可以继续精简为:
def NewBrandName(s):
print (s.capitalize()+s if s==s[-1] else "The "+s.capitalize())
s = input("请输入名字:")
NewBrandName(s) 看看看 果断中文
def pre_tit():
origin = input("Please input title: ")
new=''
if not origin == origin[-1]:
new= origin.upper()+origin
new='The '+new
else:
new= origin+origin
new=new.upper()+new
print(new)
while True:
band=input("Please Input:")
if band!=band[-1]: #首尾字母不相等
print('Result: The '+band.title()+'\n')
else: #首尾字母相等
print('Result: '+band.title()[:-1]+band+'\n')
def fun(name):
return (name[:-1]+name).title() if name == name[-1] else ('The ' + name.title()) 谢谢 str1 = input('请输入一个乐队的名字')
str2 = 'The '+ str1.capitalize()
print(str2)
if str1==str1:
str3 = str1*2
str4=str3[:len(str1)]+str3
str5=str4.capitalize()
print(str5) 英文不咋的,过来看看