ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

when ‘while‘ meets the ‘function‘

2021-05-28 09:05:39  阅读:151  来源: 互联网

标签:function meets last name pet while input Input first


Q1, Let a dictionary collect the pet's name and pet's type, and print it all in the way that "I have a dog. My dog's name is Willie."

The output:

Input the name of the pet.alice

Input the type of the pet.dog
Continue to input or not. yes/no
switch = 
yes

Input the name of the pet.tony

Input the type of the pet.cat
Continue to input or not. yes/no
switch = 
yes

Input the name of the pet.emma

Input the type of the pet.hamster
Continue to input or not. yes/no
switch = 
no
I have a dog.
My dog 's name is alice.
I have a cat.
My cat 's name is tony.
I have a hamster.
My hamster 's name is emma.

The codes:

def describe_pets(animal_type,pet_name):
    print('I have a '+animal_type+'.')
    print('My '+animal_type+" 's name is "+pet_name+".")
pets = {}
active = True
while active:
    name = input('Input the name of the pet.')
    pet_type = input('Input the type of the pet.')
    pets[name] = pet_type
    print('Continue to input or not. yes/no')
    print('switch = ',end=(''))
    switch = input()
    if switch == 'no':
        active = False
for name,pet_type in pets.items():
    describe_pets(animal_type = pet_type, pet_name = name)

Q2: generate a function that need the first name and last name to complete the full name.

The output:

Jimi Hendrix

The codes:

def get_formatted_name(first_name,last_name):
    full_name = first_name+' '+last_name
    return full_name.title()
musician = get_formatted_name('jimi', 'hendrix')
print(musician)
def get_formatted_name(first_name,last_name):
    full_name = first_name+' '+last_name
    return full_name.title()
musician_first = input('The first name of the musician: ')
musician_last = input('The last name of the musician: ')
musician = get_formatted_name( musician_first, musician_last)
print(musician)

Q3. let the function of get_formatted_name can handle the middle name:

Input your first name:changqi

Input your last name:sun
If you have no middle name, input "null"

Input your middle name:null
Changqi Sun
Would you like to input again,(yes/no)

yes

Input your first name:einstein

Input your last name:albert
If you have no middle name, input "null"

Input your middle name:sun
Einstein Sun Albert
Would you like to input again,(yes/no)

no

The codes:

def get_formatted_name(first_name,last_name,middle_name=''):
    if middle_name == '':
        full_name = first_name+' '+last_name
    else:
        full_name = first_name+' '+middle_name+' '+last_name
    return full_name.title()
active = True
while active:
    first = input('Input your first name:')
    last = input('Input your last name:')
    print('If you have no middle name, input "null"')
    middle = input('Input your middle name:')
    if middle =='null':
       musician = get_formatted_name(first, last)
    else:
       musician = get_formatted_name(first,last,middle)
    print(musician)
    print('Would you like to input again,(yes/no)')
    switch = input()
    if switch == 'no':
        active = False
    

 

标签:function,meets,last,name,pet,while,input,Input,first
来源: https://blog.csdn.net/weixin_38396940/article/details/117349638

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有