您好,欢迎访问代理记账网站
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

when ‘while‘ meets the ‘function‘

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
    

 


分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进