-------答えは、正しいことをしていたが、私はそれが間違ったことだったと思う異なったエラーがあった------------------Python:リストのインデックスをユーザに問い合わせる方法
これは非常に簡単だとわかっていますが、ユーザー入力を数値として受け取り、それを使用する方法については本当に混乱していますその番号のリストから索引を付ける番号。 だから私がしようとしているものはこれです: あなたの選択を入力してください:(ユーザが1を入力) あなたは1を選択します。 どの文章ですか? (ユーザーは入力する文章の数の範囲内に0または任意の数字を入力します)
次に、入力した番号を使用してリストから索引付けしたいだけです。彼らは自分のリストについては、これら2つの文入っ もしそうなら:彼らはどの文を聞いて、そして1言うときに、私はインデックスを作成する 良い 悪い
をsentenceListは、[1]及びそれらに戻ってそれを印刷します。
しかし、これは任意の数値にスケール可能である必要があります。したがって、sentenceList [variable]、 ですが、これを正しく行う方法はわかりません。
ありがとう、私はこれが混乱するかもしれないことを知っています。リストsentenceList
から文をプルアップする場合
#declare variable
TOTAL_SENTENCES = 5
def main():
#print greeting
print ('This program will demonstrate lists')
#establish array sentences
sentenceList = list()
#prompt user for how many sentences to store (assume integer, but if
negative, set to 5)
TOTAL_SENTENCES = int(input('How many sentences? '))
if TOTAL_SENTENCES < 0:
TOTAL_SENTENCES = 5
else:
pass
#store in a list in all lower case letters (assume no period at the end)
while len(sentenceList) < TOTAL_SENTENCES:
userSentence = input('Enter a sentence: ')
sentenceList.append(userSentence.lower())
#print 5 options for the user to choose from (looping this for the total
number of sentences)
for i in range(TOTAL_SENTENCES):
print ('Enter 1 to see a sentence\n' 'Enter 2 to see the whole list\n'
'Enter 3 to change a sentence\n' 'Enter 4 to switch words\n'
'Enter 5 to count letters')
#prompt user for their choice
userChoice = int(input('Enter your choice: '))
#print their choice back to them
print ('You selected choice' ,userChoice)
#prompt for which sentence
#CHOICE-1 (pull from the list and print the sentence)
どこに問題があるのかわかりません。あなたはすでにインプットをintに変換しています。なぜそれをインデックスとして使うことができないのですか? –
それは混乱していた場合は申し訳ありません。インデックスの数字として、数字のユーザー入力を使用するにはどうすればよいですか? –
userInput = 1、sentenceList [userInput] –