试图制作一个程序,只接受一位数的输入,然后在达到第10个输入后显示

2024-09-29 20:23:55 发布

您现在位置:Python中文网/ 问答频道 /正文

userInput = []
for i in xrange(1, 10):
    userInput.append(raw_input('Enter the %s number: '))
userInput = ''
while len(userInput) != 1:
    userInput = raw_input(':')
guessInLower = userInput.lower()
print"the numbers you have entered are: ", userInput

所以我试着做一个程序,接受10个单位数的输入,然后把它们放到一个字符串中,当它输入10个个个位数后,它就会打印出我到目前为止得到的所有个位数的输入,但是我找不到方法让它先检查。有什么建议吗?在


Tags: theinnumberforinputrawlenlower
2条回答

你应该把它放在一个循环中,然后检查输入是否是个位数:

user_inp = ""
while len(user_inp) < 10:
    inp = raw_input("Enter a digit")
    if len(inp) == 1 and inp.isdigit(): # check length and make sure a digit is entered
        user_inp += inp
    else:
        print("Invalid input")
print("The numbers you have entered are: {}".format(user_inp))

根据您所说的,以下代码是您想要的:

演示repl.it

代码:

userInput = ""

for i in xrange(1, 11):
    userInput += (raw_input('Enter the %s number: '))

print"the numbers you have entered are: ", userInput

输出:

^{pr2}$

相关问题 更多 >

    热门问题