第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

python的一道題目

python的一道題目

慕慕森 2019-04-14 11:08:00
Write a function sum_name_length that consumes no parameters and produces no value.The function will repeatedly ask the user to enter names until the empty string is entered.The function then displays the sum of the lengths of all names entered. This is illustrated inthe example below (where the user’s input is indicated in bold):Enter a name: ChesterEnter a name: PixelEnter a name: LaptopEnter a name: WhiskersEnter a name:The sum of all name lengths is 26You may assume that a name does not contain any space characters.謝謝了~
查看完整描述

3 回答

?
慕工程0101907

TA貢獻1887條經(jīng)驗 獲得超5個贊

如果只需要非空格名字的話就這樣:
#-*- coding: utf-8 -*-
import re
def sum_name_length():
sumLen=0
while True:
s=raw_input("Enter a name:")
if s=="":
break
elif re.search(r"\s",s):
print "Contains should not contains spaces!"
continue
else:
sumLen+=len(s)
print "The sum of all name lengths is %d"%sumLen
if __name__=="__main__":
sum_name_length()

改良版(名字只能是字母):
#-*- coding: utf-8 -*-
import re
def sum_name_length():
sumLen=0
while True:
s=raw_input("Enter a name:")
if s=="":
break
elif re.search(r"\s",s):
print "Contains should not contains spaces!"
continue
elif re.search(r"[^a-zA-Z]",s):
print "Name should only contains alphabet!"
continue
else:
sumLen+=len(s)
print "The sum of all name lengths is %d"%sumLen
if __name__=="__main__":
sum_name_length()



查看完整回答
反對 回復 2019-04-15
?
鴻蒙傳說

TA貢獻1865條經(jīng)驗 獲得超7個贊

def sum_name_length():
print 'Enter your names, I will displays the sum of the lengths of all names entered'
length = 0
while True:
s = raw_input('Enter a name: ')
if s == '': break
length += len(s)

print 'The sum of all name lengths is', length

if __name__ == '__main__':
sum_name_length()

 


查看完整回答
反對 回復 2019-04-15
  • 3 回答
  • 0 關注
  • 559 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號