简体   繁体   English

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

[英]How would I use a while loop so that if they enter a number it would ask them the again?

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

I want fn to be on a loop so that if they enter their a number instead of letters, it would repeat the question 我希望fn处于循环状态,以便如果他们输入数字而不是字母,它将重复这个问题

I guess you need something like this 我想你需要这样的东西

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

Define you validation method before it. 在此之前定义您的验证方法。 An example would be as Joran mentioned 一个例子就是乔兰提到的

def valid(fn):
    return fn.isalpha()
if result.isalpha():
   print "the string entered contains only letters !"

I guess ? 我猜 ?

a="6"
while not a.isalpha():
    a = raw_input("Enter your name:")
print "You entered:",a

if you just wanted to eliminate only words that contained numbers you could do 如果您只想消除包含数字的单词,则可以这样做

while any(ltr.isdigit() for ltr in a):

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎么会问他们是否要重新计算? - How would I ask if they want to calculate again? 我将如何循环这段代码,以便它重新开始? - How would i loop this bit of code so that It will go to the start again? 我将如何在此使用一会儿真实循环? - How would I use a while true loop in this? 循环 python,同时在文档中输入单词,然后再次循环。 我该怎么做? - Loop python while typing words into a document then looping back around again. How would I do this? 我如何在 Python 中一遍又一遍地向一个数字添加一些东西 - How would I add something to a number over and over again in Python 我将如何做到这一点,以便我可以输入我想在上下大写中使用的内容? 示例 - sin/SIN、tan/TAN - How would I make it so I can enter what I want to use in lower and upper caps?. Example - sin/SIN, tan/TAN 如何在 Python 中编写一个 for 循环以反复要求用户输入一个数字,直到他们输入一个整数(0、1、2 等)? - How do I write a for loop in Python to repeatedly ask the user to enter a number until they enter in a whole number (0, 1, 2, etc)? 你会如何使用 `for` 或 `while` 循环来查找句子中的第 n 个单词 - How would you use a `for` or `while` loop to find the nth word in a sentence 我将如何向管理员可以用来登录的这段代码添加一个while循环和计数器 - how would i add a while loop and counter to this code which an admin can use to login 我想知道如何制作while循环? - I would like to know how can I make while loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM