2016-12-24 33 views
-1
#string indexing 

import random 

word = "index" 
print "The word is:" , word, "\n" 

high = len(word) 
low = -len(word) 

for i in range(10): 
    position = random.randrange(low,high) 
    print "word[", position, "]\t", word[position] 

raw_input("\n\nPress the enter key to exit,") 

「Guide to Programming with Python」という本からコピーします。なぜ2つのカンマを追加する必要があるのか​​わからないPythonでの[]と[、、]の相違

[", position, "] 

この意味は? 2つのカンマを削除したときにエラーが発生するのはなぜですか? python3で

+2

の引数です。 '["、position、 "]'は全体ではなく、あなたがやっていることは異なる引数の一部分を分割することです。 'print'関数(文)が呼び出されている行に、** 4つの異なる引数がカンマ**で区切られている場合、最初の引数は文字列' 'word [" 'です.2番目の引数は変数' position '、第3の文字列' "] \ t" '、そして最後に第4の文字列' word [position] 'を指定します。カンマを削除すると、Pythonの構文が完全に破られてしまいます。 – dabadaba

+0

あなたは 'print" word ["、position、"] \ t "、word [position]'のコンマを参照していますか? (私は他の場所ではそれが見えないので) - 文字列、変数(整数)、別の文字列、および文字列(文字列変数の)を持つときに引数を区切る必要がある) – UnholySheep

答えて

0

print("word[", position, "]\t", word[position]) 

"ワード["、位置、 "[\トン"、単語[位置]は、あなたはそれが間違って取得しているprint()

関連する問題