Python 的 While 循环中的输入验证

在 while 循环中验证输入:

  1. 使用 try/except 或 if/else 语句来验证输入。
  2. 如果输入无效,则使用 continue 语句继续下一次迭代。
  3. 如果输入有效,则使用 break 语句跳出循环。
# ✅ 带有异常处理的while循环中的输入验证

num = 0

while True:
    try:
        num = int(input("Enter your favorite integer: "))
    except ValueError:
        print("Please enter a valid integer")
        continue
    else:
        print(f'You entered: {num}')
        break

print(num)

if num > 100:
    print('The provided number is greater than 100')
elif num == 100:
    print('The provided number is equal to 100')
else:
    print('The provided number is less than 100')

# ------------------------------------------------------

# ✅ Input validation in while loop with if/else statement

password = ''

while True:
    password = input('Enter your password: ')

    if len(password) < 5:
        print('Password too short')
        continue
    else:
        print(f'You entered {password}')
        break

print(password)

第一个示例使用异常处理验证 while 循环中的输入。

代码片段不断提示用户输入,直到他们输入一个有效的整数。

num = 0

while True:
    try:
        num = int(input("Enter your favorite integer: "))
    except ValueError:
        print("Please enter a valid integer")
        continue
    else:
        print(f'You entered: {num}')
        break

print(num)

if num > 100:
    print('The provided number is greater than 100')
elif num == 100:
    print('The provided number is equal to 100')
else:
    print('The provided number is less than 100')

Python 的 While 循环中的输入验证

如果 try 块中的代码引发 ValueError,则 except 块运行,我们使用 continue 语句继续下一次迭代。

如果用户输入一个有效整数,try 块成功完成,然后 else 块运行,我们使用 break 语句退出 while 循环。

continue 语句继续循环的下一次迭代。

break 语句跳出最里面的 for 或 while 循环。

在 while 循环中验证用户输入时,我们在输入无效时使用 continue 语句,例如 在 except 块或 if 语句中。

如果输入有效,我们使用 break 语句退出 while 循环。

我们可以使用相同的方法使用 if/else 语句验证用户输入。

password = ''

while True:
    password = input('Enter your password: ')

    if len(password) < 5:
        print('Password too short')
        continue
    else:
        print(f'You entered {password}')
        break

print(password)

while 循环不断迭代,直到用户输入一个长度至少为 5 的值。

如果值太短,我们使用 continue 语句继续下一次迭代。

如果该值至少有 5 个字符长,我们使用 break 语句作为输入有效。

如果需要检查多个条件,可以使用布尔值或和和运算符。

password = ''

common_passwords = ['abcde', 'password123']

while True:
    password = input('Enter your password: ')

    if len(password) < 5 or password in common_passwords:
        print('Pick a strong password')
        continue
    else:
        print(f'You entered {password}')
        break

print(password)

if 语句检查密码是否少于 5 个字符或是否在常用密码列表中。

我们使用了布尔值或运算符,因此如果满足两个条件中的任何一个,if 块就会运行。

如果密码少于5个字符或者包含在常用密码列表中,我们继续下一次迭代,再次提示用户。

如果我们需要在验证输入时检查是否满足多个条件,请使用 and 布尔运算符。

password = ''

common_passwords = ['abcde', 'password123']

while True:
    password = input('Enter your password: ')

    if len(password) > 5 and password not in common_passwords:
        print(f'You entered {password}')
        break
    else:
        print('Pick a strong password')
        continue

print(password)

我们使用了 and 布尔运算符,因此要运行 if 块,必须满足两个条件。

密码必须超过 5 个字符,并且必须不在常用密码列表中。

如果条件满足,我们使用 break 语句退出 while True 循环。

如果条件不满足,我们使用 continue 语句继续下一次迭代。