【问题标题】:I want modify this code as when a user gives right name but wrong password code must run from enter password not from enter username again我想修改此代码,因为当用户提供正确的名称但错误的密码代码必须从输入密码而不是再次输入用户名运行时
【发布时间】:2020-10-27 08:03:23
【问题描述】:
while True:
    print("who is this?")
    name=input()
    if name!='shubh':
        continue
    print("hi its you shubh. type your password")  
    password=input()
    if password!="shubh": 
        print("try again. wrong password")
    elif password=="shubh":
        break
print("access granted")

当用户提供正确的 id 但密码错误时,代码必须从密码输入而不是从开始运行

【问题讨论】:

  • 那么你可能需要两个循环。 1 代表用户名,2) 代表密码。

标签: python while-loop passwords user-input continue


【解决方案1】:

您可以按照@ewong 的建议尝试

while True:
    print("who is this?")
    name=input()
    if name=='shubh':
        break
    print("Please try again")
while True:
    print("hi its you shubh. type your password")
    password=input()
    if password!="shubh":
        print("try again. wrong password")
    elif password=="shubh":
        break
print("access granted")

【讨论】:

    猜你喜欢
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 2019-04-19
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    相关资源
    最近更新 更多