在while语句中使用字符串

时间:2017-07-11 18:38:31

标签: python while-loop

如果在输入错误的输入时如何防止重复“再试一次”:

from __future__ import print_function

print("Hello, World!")

name = input("What is your name?")

hobby = input("Cool, so " + name + " what do you do you for fun? You can say something like play, work, learn, etc.")

actions = {'play': 'awesome', 'work': 'must be busy', 'learn': 'ha, me too', }

while True:
    try:
        print(actions[hobby])
        break
    except KeyError:
        print("Try again.")

0 个答案:

没有答案