繁体   English   中英

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

[英]trying to make a program that only accepts single digit inputs and then displays after it hits the 10th input

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个单位数输入后,再打印出我到目前为止所得到的所有单位数输入,但是我无法找到一种方法可以先检查。 有什么建议么?

按照您的意思,下面的代码就是您想要的:

演示上repl.it

码:

userInput = ""

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

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

输出:

Enter the %s number:  1
Enter the %s number:  2
Enter the %s number:  3
Enter the %s number:  4
Enter the %s number:  5
Enter the %s number:  6
Enter the %s number:  7
Enter the %s number:  8
Enter the %s number:  9
Enter the %s number:  0
the numbers you have entered are:  1234567890

您应该将所有内容放在一个循环中,并检查输入是否为单个数字:

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))

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM