-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,")
我從一本名爲「使用Python進行編程的指南」的書中複製此內容。我不知道爲什麼我們必須添加兩個逗號python中的[]和[,,]之間的區別
[", position, "]
這是什麼意思?爲什麼我刪除兩個逗號時會出錯?
你得到它錯了論據; '[「,position,」]'不是一個整體,你所做的只是分解不同論點的一部分。在出現的那行中,print函數(語句)正在用**用逗號分隔的四個不同參數**調用,第一個參數是字符串「」word [「',第二個參數是變量'位置',第三個字符串'「] \ t」',最後是第四個字符串變量'word [position]'。如果刪除逗號,則完全違反Python語法。 – dabadaba
你指的是'print'中的逗號[],位置,「] \ t」,單詞[position]'? (因爲我沒有在其他地方看到) - 那些需要分開參數的東西(在這種情況下用於'print'),因爲你有一個字符串,一個變量(整數),另一個字符串和一個字符串變量) – UnholySheep