簡體   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