我需要幫助包住:用while語句循環,而讀一字符串整數
if any(c.isdigit() for c in name):
print("Not a valid name!")
while語句的內部。這幾乎只是讀取「name」變量的輸入,並查看是否有一個整數。我怎麼能在一個while語句中使用它?我只是希望如果用戶在輸入中輸入了一個變量,它會打印出上面的字符串並循環回去,並再次詢問用戶名稱,直到他們成功輸入一個沒有整數的字符串,然後我想要它打破。任何幫助?
print("Hello there!")
yn = None
while yn != "y":
print("What is your name?")
name = raw_input()
if any(c.isdigit() for c in name):
print("Not a valid name!")
print("Oh, so your name is {0}? Cool!".format(name))
print("Now how old are you?")
age = raw_input()
print("So your name is {0} and you're {1} years old?".format(name, age))
print("y/n?")
yn = raw_input()
if yn == "y":
break
if yn == "n":
print("Then here, try again!")
print("Cool!")
是的,我希望它保持循環,直到用戶輸入一個有效的名稱(一個沒有整數) – Xiam 2013-05-09 18:20:08
所以......這不是你的問題的答案嗎? – 2013-05-09 18:22:36