0
代碼:嘗試/異常流量控制
def get_wordlen():
wordfamily_lst = []
while True:
try:
word_len = int(input('\nPlease enter length of the word you wish to guess: '))
input_File = open('dic.txt', 'r').read().split()
for word in input_File:
if len(word) == word_len:
wordfamily_lst.append(word)
else:
print("Sorry, there's no word of that length")
continue
except ValueError:
print('Please enter a numeric value for word length')
continue
return word_len, wordfamily_lst
wordlen, wordfamilylst = get_wordlen()
print(wordlen,wordfamilylst)
如何修改我的「其他人」的聲明,以防止字長的TXT的用戶輸入。文件不包含。現在,我的代碼將顯示與單詞長度的用戶輸入不匹配的每個單詞的打印語句。
我只想要一個打印語句並循環回到while循環的頂部。
請給我一些建議。
非常感謝你的幫助!你的解釋非常徹底,對於剛開始像我一樣學習計算機科學的人非常有幫助。 – user3454234