2011-03-11 3 views
0

私が番号を入力するようユーザーに求めています..Pythonのリスト/インデックスクエリ

これは私が現在持っているコードです。

List = ["a", "b", "c", "d", "e", "f"] 

print "The list has the following", len(List), "list:", List 

new_item = raw_input("Which item would you like to add? ") 
List.append(new_item) 
print "The list has the following", len(List), "items:", List 

Number = raw_input ("Please select a number: ") 
+3

あなたの質問がありますか? –

+1

'print List [Number]'? –

+0

ユーザーに番号を入力するように依頼することによって....そのインデックスの項目を表示することができます。ユーザーが2を入力した場合、私は "c"を表示したいと思っています。iveはもっとコードを悩ましていますが、それを動かすことはできません。 – IrishGal

答えて

5

最初の整数にNumberを変換してみてください。

i = int(Number)                 
print "You selected:", List[i] 

ところで、それは良いPythonのスタイルを使用して変数を小文字にし、クラスの大文字で始まる識別子を保持します。したがって、Listの代わりにmy_listを使用し、Numberの代わりにnumberを使用してください。 (listを変数名として使用しないでください。これは組み込みのlistタイプを隠すためです)。

+1

ありがとうございました! :) – IrishGal

1
l = ["a", "b", "c"] 
ii = raw_input("Please select a number: ") 
print l[ii] 
+0

ここで覚えておいてくださいインデックスは0から始まり、1から始まらないことです。 –

+0

表示されるエラーは 'TypeError:リストのインデックスはstrではなく整数でなければなりません。'というときにJim – IrishGal

0

しようとしているのは、文字列を「索引付け」することです。

letter_at_index = my_list[index] 

注意を以下のようにインデックスは0から始まること、すなわち、

my_list = ['a', 'b', 'c'] 
a = my_list[0] 
b = my_list[1] 
c = my_list[2] 

負のインデックスは、シーケンス下例えば

c = my_list[-1] 

詳しい情報、ならびに特定することができる達成されますタイプhere

0
List = ["a", "b", "c", "d", "e", "f"] 
print "The list has the following", len(List), "list:", List 
try: 
    Number = raw_input ("Please select a number: ") 
    item = List[int(Number)] 
    print "The list item at", Number, "is", item 
except EOFError: 
    print "Error. Nothing entered" 
except ValueError: 
    print "Error. Expecting a number instead of:", Number 
except IndexError: 
    print "Error. Number out of range:", Number 
0
List = ["a", "b", "c", "d", "e", "f"] 

print "The list has the following length: "+str(len(List))+ "\n list:"+repr(List) 

List.append(raw_input("\nWhich item would you like to add? ")) 

print "\nThe list has the following length: "+str(len(List))+ "\n list:"+repr(List) 

print '\nThe item at the position you entered is : '+\ 
     repr(List[int(raw_input ("\nPlease select a number: "))])[1:-1] 

結果

The list has the following length: 6 
list:['a', 'b', 'c', 'd', 'e', 'f'] 

Which item would you like to add? (12,52,145) 

The list has the following length: 7 
list:['a', 'b', 'c', 'd', 'e', 'f', '(12,52,145)'] 

Please select a number: 6 

The item at the position you entered is : (12,52,145) 
0

次のことを試してください。

i = None 
listend = len(List)-1 
while i == None: 
    try: 
     print '' 
     raw = raw_input("Which item would you like to add? ") 
     i = int(raw) 
     value = List[i] 
    except IndexError: 
     print i, 'is out of bounds. Expecting an integer from 0 to', listend 
     i = None 
    except: 
     print 'You entered "%s" however I was expecting an integer from 0 to %s'%(raw, listend) 
    else: 
     print 'Result:', List[i] 

このように受け入れ可能な入力を待っている間、それは多くの場合、ループに便利です - あなたはiが唯一の他の何かに設定されていることがわかりますint(raw)が正常に実行された場合はNoneより大きくなり、リストの範囲外の場合はNoneに戻ります。 elseステートメントは、コンテンツがループwhileの外に出る可能性があるため、厳密には必要ではありませんが、tryブロックが例外をスローしないで例外を処理しない場合にのみコードブロックを実行するように設計されているようです自体。