gpt4 book ai didi

python - 我将如何使用 while 循环,以便如果他们输入一个数字,它会再次询问他们?

转载 作者:行者123 更新时间:2023-11-28 22:33:54 26 4
gpt4 key购买 nike

fn = input("Hello, what is your first name?")
firstname = (fn[0].upper())
ln = input("Hello, what is your last name?")
lastname = (ln.lower())

我希望 fn 循环,这样如果他们输入数字而不是字母,它会重复问题

最佳答案

我猜你需要这样的东西

final_fn = ""
while True:
fn = input("Hello, what is your first name?")
if valid(fn):
final_fn = fn
break

在它之前定义你的验证方法。一个例子就是 Joran 提到的

def valid(fn):
return fn.isalpha()

关于python - 我将如何使用 while 循环,以便如果他们输入一个数字,它会再次询问他们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39457203/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com