Python 的 While 循环中的输入验证
在 while 循环中验证输入:
-
使用
try/except
或if/else
语句来验证输入。 -
如果输入无效,则使用
continue
语句继续下一次迭代。 -
如果输入有效,则使用
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')
如果 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 语句继续下一次迭代。
相关文章
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中将 Timedelta 转换为 Int
发布时间:2024/04/23 浏览次数:231 分类:Python
-
可以使用 Pandas 中的 dt 属性将 timedelta 转换为整数。
Python 中的 Pandas 插入方法
发布时间:2024/04/23 浏览次数:112 分类:Python
-
本教程介绍了如何在 Pandas DataFrame 中使用 insert 方法在 DataFrame 中插入一列。
使用 Python 将 Pandas DataFrame 保存为 HTML
发布时间:2024/04/21 浏览次数:106 分类:Python
-
本教程演示如何将 Pandas DataFrame 转换为 Python 中的 HTML 表格。
如何将 Python 字典转换为 Pandas DataFrame
发布时间:2024/04/20 浏览次数:73 分类:Python
-
本教程演示如何将 python 字典转换为 Pandas DataFrame,例如使用 Pandas DataFrame 构造函数或 from_dict 方法。
如何在 Pandas 中将 DataFrame 列转换为日期时间
发布时间:2024/04/20 浏览次数:101 分类:Python
-
本文介绍如何将 Pandas DataFrame 列转换为 Python 日期时间。