我想知道用户是否在数字前输入了空格。目前,如果您按下空格然后输入数字,程序会忽略空格并在您刚刚输入数字时看到它。
我尝试了在这个网站上找到的一些方法,但我一定遗漏了一些东西。
import re
while True:
enternum=input('Enter numbers only')
try:
enternum=int(enternum)
except ValueError:
print ('Try again')
continue
conv = str(enternum) # converted it so I can use some of the methods below
if conv[0].isspace(): # I tried this it does not work
print("leading space not allowed")
for ind, val in enumerate(conv):
if (val.isspace()) == True: # I tried this it does not work
print('leading space not allowed')
if re.match(r"\s", conv): # I tried this it does not work (notice you must import re to try this)
print('leading space not allowed')
print('Total items entered', len(conv)) # this does not even recognize the leading space
print ('valid entry')
continue
慕桂英4014372
相关分类