码迷,mamicode.com
首页 > 其他好文 > 详细

函数:动手试一试2

时间:2017-12-31 19:34:57      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:else   append   函数返回   用户输入   不重复   str   结合   姓名   ret   

 

练一练:函数返回值结合循环以及if语句的判断条件

def full(xing,ming):#定义一个姓名的函数
    xingming=xing+ +ming
    return xingming.title()
all=[]
while True:
    x=input(xing)
    m=input(ming)
    if x and m:#如果输入的姓和名都不为空则继续
        c=full(x,m)
        print(c)
        all.append(c)#将接收到的姓名添加进列表
    else:
        break#如果输入的姓和名有一个为空则跳出循环
print(all)#打印列表

6.创建函数,接受用户输入城市名和所属国家,默认国家为中国,返回一个‘城市-国家’的字符串,至少三个国家城市调用这个函数,打印返回值,最后在做个国家-城市的字典,国家是键,键不重复,键重复的合并值为一个列表,随时可以退出。最后输出字典

还没完成,回家接着写

def full(city,country):
    all=city+-+country
    return all.title()
fulldic={}
print(\n输入城市和国家,不输人国家则默认为China,输入q退出)
while True:
    ci=input(city)
    if ci==q:
        break
    co=input(country)
    if co==q:
        break
    if not co:
        co=China
    fullstr=full(ci,co)
    print(fullstr)
    if co not in fulldic:
        fulldic[str(co)]=str(ci)
    else:#还没弄明白怎么在一个键下添加多个值组成一个列表
        #same=list(fulldic[co])
        #print(same)
print(fulldic)

 

函数:动手试一试2

标签:else   append   函数返回   用户输入   不重复   str   结合   姓名   ret   

原文地址:https://www.cnblogs.com/xusuns/p/8158570.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!