Python 如何创建在输入数字时终止的程序?
#程序仅在输入0到9个数字时终止,但如果输入了任何数字,我希望它终止。使用Python 如何创建在输入数字时终止的程序?,python,Python,#程序仅在输入0到9个数字时终止,但如果输入了任何数字,我希望它终止。使用.isdigit()。这样试试- q = [] num = ["1","2","3","4","5","6","7","8","9","0"] while True: dat = float(input("E
.isdigit()
。这样试试-
q = []
num = ["1","2","3","4","5","6","7","8","9","0"]
while True:
dat = float(input("Enter Name: "))
if dat == "*":
print(q.pop(0))
if dat in num :
break
else:
q.append(dat)
与其检查
dat
是否为数字,不如检查其任何字符是否为数字:
q = []
#num = ["1","2","3","4","5","6","7","8","9","0"]
while True:
dat = input("Enter Name: ")
if dat == "*":
print(q.pop(0))
if dat.isdigit() :
break
else:
q.append(dat)
另一种方法是检查函数isalpha
:
q = []
somedigit = False
while True:
dat = input("Enter Name: ")
if dat == "*":
print(q.pop(0))
for element in dat:
if element.isdigit():
somedigit = True
break
if somedigit == True:
break
else:
q.append(dat)
@心理学家编辑了这篇文章
q = []
while True:
dat = input("Enter Name: ")
if dat == "*":
print(q.pop(0))
if not dat.isalpha():
break
else:
q.append(dat)