《Python编程:从入门到实践》答案(7-8章)

小咪咪 2023-02-25 10:15 73阅读 0赞

事实上,除了你自己没人有必要考虑你的感受。你喜欢什么事,什么人,有什么想法,去做就好,不用问,答案在你心中。

20200712170900924.png

  1. message=input("what kind of the car do you want ?")
  2. print('Let me see if I can find you a'+ message)

2020071217175276.png

  1. print('how many people do you have?')
  2. ans=input()
  3. ans=int(ans)
  4. if ans>=8:
  5. print('Sorry ,there are nothing for you')
  6. else:
  7. print('welcome,there are enough sets for you')

20200712172321882.png

  1. message=input("Please input your number: ")
  2. message=int(message)
  3. if message%10==0:
  4. print("Your number is multiple of 10.")
  5. else :
  6. print("Your number can not be divisible by 10.")

20200908164918843.png

  1. message=""
  2. while message!='quit':
  3. message=input("Please input your toppings : ")
  4. if message!='quit':
  5. print("Ok,we will add "+message+" in your pizza.")
  6. else:
  7. break

20200908170441101.png

  1. message=""
  2. while True:
  3. message=input("Enter your age please: ")
  4. message=int(message)
  5. if message<=3:
  6. print("price=0")
  7. elif message<12:
  8. print("price=$10")
  9. else:
  10. print("price=$15")

20200908171223637.png

  1. #使用条件测试来结束循环
  2. message=""
  3. while message!=0:
  4. message=input("Enter your age please: ")
  5. message=int(message)
  6. if message!=0:
  7. if message<=3:
  8. print("price=0")
  9. elif message<12:
  10. print("price=$10")
  11. else:
  12. print("price=$15")
  13. message=""
  14. active=True
  15. while active:
  16. message=input("Enter your age please: ")
  17. message=int(message)
  18. if message==0:
  19. active=False
  20. else:
  21. if message<=3:
  22. print("price=0")
  23. elif message<12:
  24. print("price=$10")
  25. else:
  26. print("price=$15")
  27. message=""
  28. while True:
  29. message=input("Enter your age please: ")
  30. if message=='quit':
  31. break
  32. else:
  33. message=int(message)
  34. if message<=3:
  35. print("price=0")
  36. elif message<12:
  37. print("price=$10")
  38. else:
  39. print("price=$15")

20200908204141661.png

  1. sandwich_orders=['orange','banana','apple']
  2. finished_sandwichs=[]
  3. while sandwich_orders:
  4. for order in sandwich_orders:
  5. print(order+" is making .")
  6. finished_sandwichs.append(order)
  7. sandwich_orders.remove(order)
  8. print("your order is finished.")
  9. print(finished_sandwichs)

20200908211014679.png

  1. sandwich_orders=['orange','pastrami','banana','apple','pastrami']
  2. finished_sandwichs=[]
  3. print("The pastrami has been sale off.")
  4. while sandwich_orders:
  5. for order in sandwich_orders:
  6. if order!='pastrami':
  7. print(order+" is making .")
  8. finished_sandwichs.append(order)
  9. sandwich_orders.remove(order)
  10. else:
  11. sandwich_orders.remove(order)
  12. continue
  13. print("your order is finished.")
  14. print(finished_sandwichs)

20200910063335416.png

  1. def display_message():
  2. print("I learn about the function and model")
  3. display_message()
  4. def favorite_book(title):
  5. print("One of my favorite book is "+title.title()+".")
  6. favorite_book("The Call of Wild")

20200910071505701.png

  1. def make_shirt(size,words):
  2. print("\nSize: "+size)
  3. print("words:"+words)
  4. make_shirt("medium","Immortal")
  5. def make_shirt(size,words):
  6. print("\nSize: "+size)
  7. print("words:"+words)
  8. make_shirt(words="Immortal",size="medium")

20200910071919681.png

  1. def make_shirt(size="large",words="I love python"):
  2. print("\nSize: "+size)
  3. print("words:"+words)
  4. make_shirt("medium","Immortal")
  5. make_shirt("samll","haha")
  6. make_shirt()

20200910072129758.png

  1. def discribe_city(city,country="China"):
  2. print("The "+city+" is belong to "+country+". ")
  3. discribe_city("New York","America")
  4. discribe_city("金山屯")

20200910081813789.png

  1. def city_country(city,country):
  2. message=city+','+country
  3. return message
  4. ex1=city_country("金山屯","China")
  5. ex2=city_country("New York","America")
  6. ex3=city_country("Venice","Italy")
  7. print(ex1)
  8. print(ex2)
  9. print(ex3)

20200910082331663.png

20200910082340349.png

  1. def make_album(song,album,amount=""):
  2. if amount:
  3. album={'song':song,'name':album,'amount':amount}
  4. else:
  5. album={'song':song,'name':album}
  6. return album
  7. ex1=make_album('东风破','菊花台')
  8. ex2=make_album('青花瓷','夜莺',30)
  9. ex3=make_album("将军令",'花田',90)
  10. print(ex1)
  11. print(ex2)
  12. print(ex3)

20200910083300671.png

  1. def make_album(song,album,amount=""):
  2. if amount:
  3. album={'song':song,'name':album,'amount':amount}
  4. else:
  5. album={'song':song,'name':album}
  6. return album
  7. while True:
  8. print("\nPlease enter the song ,album: ")
  9. song=input("Enter your song name: ")
  10. album=input("Enter your album name: ")
  11. if song=='quit' and album=='quit':
  12. break
  13. else:
  14. solution=make_album(song,album)
  15. print(solution)

20200910113621906.png

  1. magician_name=["红烧肉",'辣子鸡','水晶凉皮','彩虹拼盘','糖醋里脊']
  2. def show_magicians(magician_name):
  3. for name in magician_name:
  4. print(name)
  5. show_magicians(magician_name)

20200910123247563.png

  1. magician_name=["红烧肉",'辣子鸡','水晶凉皮','彩虹拼盘','糖醋里脊']
  2. def show_magicians(magician_name):
  3. for name in magician_name:
  4. print(name)
  5. show_magicians(magician_name)
  6. print("\n")
  7. def make_great(magician_name):
  8. for index in range(0,len(magician_name)):
  9. magician_name[index]='the Great '+magician_name[index]
  10. make_great(magician_name)
  11. show_magicians(magician_name)

20200910123928203.png

  1. magician_name=["红烧肉",'辣子鸡','水晶凉皮','彩虹拼盘','糖醋里脊']
  2. def show_magicians(magician_name):
  3. for name in magician_name:
  4. print(name)
  5. show_magicians(magician_name)
  6. print("\n")
  7. def make_great(magician_name):
  8. for index in range(0,len(magician_name)):
  9. magician_name[index]='the Great '+magician_name[index]
  10. return magician_name
  11. new_magicians=make_great(magician_name[:])
  12. show_magicians(magician_name)
  13. show_magicians(new_magicians)

20200910140311644.png

  1. def sandwich_topping(*toppings):
  2. print("your toppings include : ")
  3. for top in toppings:
  4. print(top)
  5. print("\n")
  6. sandwich_topping("banana",'apple')
  7. sandwich_topping("elephant",'panda','peacock')
  8. sandwich_topping("mouse",'dragon','horse')

2020091014082325.png

  1. def build_profile(first,last,**user_info):
  2. profile={}
  3. profile['first name']=first
  4. profile['last name']=last
  5. for key,value in user_info.items():
  6. profile[key]=value
  7. return profile
  8. me=build_profile('张','宸宸',身高=1.75,apperance='hot',hometown='Harbin')
  9. print(me)
  10. #注意,在调用写键值对的时候,键是不加引号的,值才加引号
  11. #注意,for循环中给键与值对应的语句是 profile[key]=value

20200910142051772.png

20200910142105709.png

  1. def car_info(manufacturer,model,**other_info):
  2. profile={}
  3. profile['manufacturer']=manufacturer
  4. profile['model']=model
  5. for key,value in other_info.items():
  6. profile[key]=value
  7. return profile
  8. car=car_info('Benz','s450L',颜色='黑色',级别='大型车',发动机='48V轻混')
  9. print(car)

20200910145512631.png

  1. def record_dream(name,age,**others):
  2. dream={}
  3. dream['name']=name
  4. dream['age']=str(age)
  5. for key,value in others.items():
  6. dream[key]=value
  7. for key,value in dream.items():
  8. print(key+" : "+value)
  9. import model
  10. model.record_dream('小明',19,财务="100000$",家庭="three child and a beautiful girl")

发表评论

表情:
评论列表 (有 0 条评论,73人围观)

还没有评论,来说两句吧...

相关阅读