【Python】通过while True循环语句,控制代码运行,直到输入正确数据,才执行下一步

日常系统操作时,比如输入账号密码,如果一次不正确,是可以继续输入的,这样就需要用到了循环语句。

# _*_ coding : utf-8 _*_
# 定义1个函数,根据输入的姓+名,输出首字母大写的格式
def get_formatted_name(first, last):
    full_name = first + ' ' + last
    return full_name.title()

# 循环语句控制。只有当fist=wang + last=papa时,才会输出语句。否则一直从头开始
while True:
    first = input("Please input first_name:")
    if first != 'wang':
        print ("!!!Your first_name is wrong!")
        continue
    last = input("Please input last_name:")
    if last != 'papa':
        print ("!!!Your last_name is wrong!")
        continue

    full_name = get_formatted_name(first, last)
    print("Your formatted_name is : " + full_name)
    break
发布了94 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/woshiyigerenlaide/article/details/104168895
今日推荐