簡體   English   中英

python app陷入混亂和循環

[英]python app gets stuck on shuffle and loop

我正在使用python編寫此小片段,當我運行它時,它永遠不會超過打印'c'行,並停留在while循環中。 我究竟做錯了什么? 鏈接到文本文件: http : //downloads.sourceforge.net/wordlist/12dicts-5.0.zip

enter code here
import sys
import random

inp = open('5desk.txt', 'r')
lis = inp.readlines()
inp.close()
print lis

def proc():

 a = raw_input('What are the scrambled letters? ')
 copy = list(a)
 print a
 print copy
 if a in lis:
  print a, ' is a word'
 elif a not in lis:
  print 'c'
  while copy not in lis:
   random.shuffle(copy)
  print 'd'


 print "A word that ", a, " might equal is:\n", copy


if __name__ == "__main__":
 proc()

也許您的意思是這樣的:

while copy in lis:

無論哪種方式,都不能保證重新排列字母最終會創建一個不在列表中的單詞。 特別是如果輸入僅包含一個字母,則隨機播放將完全無效。 最好以隨機順序遍歷所有排列,如果到達列表的末尾,則會出錯並退出循環。

readline()和readlines()保留輸入每一行的尾隨換行符; raw_input()刪除換行符。 因此,從來沒有一場比賽。

從外部源讀取輸入時,通常最好使用strip()函數清理內容-默認情況下,它從輸入的每一端刪除空格。

嘗試:

lis = [line.strip() for line in inp.readlines()]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM