输入数字到数组

时间:2017-02-03 15:08:40

标签: python arrays python-3.x input numbers

我是编程的初学者 我试图制作能够输入10个数字的程序 因此,我可以制作这种程序:

while True:
        s = input('Enter 10 numbers : ')
        if len(s) == 10:
            break
        else:
            print('Retype your 10 personal numbers!!')
print('Done')

但是,我想将数字输入到像s[10]这样的数组中? 例如,如果我输入'1234567890',则会输入s[0]=1s[1]=2,...,s[10]=0

请具体说明一下。

2 个答案:

答案 0 :(得分:4)

如果-- `menu_click`, by Jacob Rus, September 2006 -- -- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}` -- Execute the specified menu item. In this case, assuming the Finder -- is the active application, arranging the frontmost folder by date. on menuClick(mList) local appName, topMenu, r -- Validate our input if mList's length < 3 then error "Menu list is not long enough" -- Set these variables for clarity and brevity later on set {appName, topMenu} to (items 1 through 2 of mList) set r to (items 3 through (mList's length) of mList) -- This overly-long line calls the menu_recurse function with -- two arguments: r, and a reference to the top-level menu tell application "System Events" to my menuClickRecurse(r, ((process appName)'s ¬ (menu bar 1)'s (menu bar item topMenu)'s (menu topMenu))) end menuClick on menuClickRecurse(mList, parentObject) local f, r -- `f` = first item, `r` = rest of items set f to item 1 of mList if mList's length > 1 then set r to (items 2 through (mList's length) of mList) -- either actually click the menu item, or recurse again tell application "System Events" if mList's length is 1 then click parentObject's menu item f else my menuClickRecurse(r, (parentObject's (menu item f)'s (menu f))) end if end tell end menuClickRecurse local destination, libraryName, choices set destination to "/Users/bryandunphy/music" set libraryName to "Testing.xml" tell application "iTunes" to activate menuClick({"iTunes", "File", "Library", "Export Library…"}) tell application "System Events" to set the value of the text field "Save As:" of window "iTunes" of process "iTunes" to libraryName tell application "System Events" to tell process "iTunes" to tell its front window's group 1's pop up button 1 to click tell application "System Events" to tell process "iTunes" to set choices to the title of every menu item of menu 1 of pop up button 1 of group 1 of its front window repeat with ndx from 1 to count of choices if the value of choices's item ndx is "" then tell application "System Events" to tell process "iTunes" to select (the menu item of menu 1 of pop up button 1 of group 1 of its front window whose title is equal to item (ndx - 1) of choices) end if end repeat ,您可以将s='1234567890'转换为具有简单列表理解的数字列表:

s

然后

s = [int(d) for d in s]

>>> s [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] >>> s[9] 0 超出范围BTW:))

答案 1 :(得分:0)

或者您可以在程序中进行简单更新,即首先将s声明为空列表。 它是:

s=[]
while True:
    s = input('Enter 10 numbers : ')
    if len(s) == 10:
        break
    else:
        print('Retype your 10 personal numbers!!')
print('Done')