這並未不直接回答你的問題,但它可能有助於理解我們遇到的一些問題。大多數情況下,您可以看到如何添加到列表中,並希望看到獲取字符串,列表和整數(您實際上無法做到!)的長度之間的差異。
嘗試運行下面的代碼,並檢查所發生的事情:
def step_forward():
raw_input('(Press ENTER to continue...)')
print('\n.\n.\n.')
def experiment():
""" Run a whole lot experiments to explore the idea of lists and
variables"""
# create an empty list, test length
word_list = []
print('the length of word_list is: {}'.format(len(word_list)))
# expect output to be zero
step_forward()
# add some words to the list
print('\nAdding some words...')
word_list.append('Hello')
word_list.append('Experimentation')
word_list.append('Interesting')
word_list.append('ending')
# test length of word_list again
print('\ttesting length again...')
print('\tthe length of word_list is: {}'.format(len(word_list)))
step_forward()
# print the length of each word in the list
print('\nget the length of each word...')
for each_word in word_list:
print('\t{word} has a length of: {length}'.format(word=each_word, length=len(each_word)))
# output:
# Hello has a length of: 5
# Experimentation has a length of: 15
# Interesting has a length of: 11
# ending has a length of: 6
step_forward()
# set up a couple of counters
short_word = 0
long_word = 0
# test the length of the counters:
print('\nTrying to get the length of our counter variables...')
try:
len(short_word)
len(long_word)
except TypeError:
print('\tERROR: You can not get the length of an int')
# you see, you can't get the length of an int
# but, you can get the length of a word, or string!
step_forward()
# we will make some new tests, and some assumptions:
# short_word: a word is short, if it has less than 9 letters
# long_word: a word is long, if it has 9 or more letters
# this test will count how many short and long words there are
print('\nHow many short and long words are there?...')
for each_word in word_list:
if len(each_word) < 9:
short_word += 1
else:
long_word += 1
print('\tThere are {short} short words and {long} long words.'.format(short=short_word, long=long_word))
step_forward()
# BUT... what if we want to know which are the SHORT words and which are the LONG words?
short_word = 0
long_word = 0
for each_word in word_list:
if len(each_word) < 9:
short_word += 1
print('\t{word} is a SHORT word'.format(word=each_word))
else:
long_word += 1
print('\t{word} is a LONG word'.format(word=each_word))
step_forward()
# and lastly, if you need to use the short of long words again, you can
# create new sublists
print('\nMaking two new lists...')
shorts = []
longs = []
short_word = 0
long_word = 0
for each_word in word_list:
if len(each_word) < 9:
short_word += 1
shorts.append(each_word)
else:
long_word += 1
longs.append(each_word)
print('short list: {}'.format(shorts))
print('long list: {}'.format(longs))
# now, the counters short_words and long_words should equal the length of the new lists
if short_word == len(shorts) and long_word == len(longs):
print('Hurray, its works!')
else:
print('Oh no!')
experiment()
希望,當你通過我們的答案在這裏,並檢查上面的小實驗,你將能夠得到您的代碼做你需要它做的事情:)
嗯......我將它與以前定義/發現的迴文相比較。 但是,我怎樣稱呼他們,讓我可以在這個循環中使用它們,並使它們等於我的長度條件?我知道我的迴文長度必須與數字長度相匹配,但我不確定如何寫。 – user2172079 2013-03-24 08:40:44
好吧,不用擔心。如果我正確理解您的代碼: count_pali()函數將計算迴文總數,通過每次找到迴文時遞增。 ...並且您還要計算這些迴文有多少個字母長度超過3個字母。 因此,你可以在迴文測試後直接嘗試if語句嗎?在僞代碼中,它看起來有點像這樣: #你在這裏的代碼... 如果字是迴文: count_pali + 1 – 2013-03-24 09:01:18
增加更多到最後的評論: 「#你前面的代碼在這裏... 如果字是迴文: count_pali + 1 如果字是不是3個字母長: long_word + 1' 我們接近嗎? – 2013-03-24 09:07:13