Python练习--循环,字典

提示用户输入姓名和回答,最终统一打印出来。

  1 responses = {}
  2 polling_active = True
  3 while polling_active:
  4     name = input("\nWhat is your name? ")
  5     response = input("Which mountain would you like?")
  6     responses[name] = response
  7     print("Is that all?yes/no")
  8     account = input()
  9     if account == 'yes':
 10         polling_active = False
 11     else:
 12         continue
 13 for key,value in responses.items():
 14     print(key + "'s favourate mountain is " + value)

最终运行结果是这样的:

[root@centos7 tmp]# python3 mountain_poll.py 

What is your name? jack
Which mountain would you like?alibaba
Is that all?yes/no
no

What is your name? pony
Which mountain would you like?tecent
Is that all?yes/no
yes
jack's favourate mountain is alibaba
pony's favourate mountain is tecent
检查的时候,发现自己有个坏习惯需要改,就是要多加注释!

猜你喜欢

转载自blog.csdn.net/zsx0728/article/details/80772930