Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何摆脱我的while循环_Python_List_For Loop_While Loop - Fatal编程技术网

Python 如何摆脱我的while循环

Python 如何摆脱我的while循环,python,list,for-loop,while-loop,Python,List,For Loop,While Loop,因此,我的代码的问题是,即使我输入了正确猜测的单词,我的代码仍然认为它不正确;因此,请我再试一次。我如何摆脱这个循环?谢谢 import random count = 1 word = ['orange' , 'apple' , 'chicken' , 'python' , 'zynga'] #original list randomWord = list(random.choice(word)) #defining randomWord to make sure random ch

因此,我的代码的问题是,即使我输入了正确猜测的单词,我的代码仍然认为它不正确;因此,请我再试一次。我如何摆脱这个循环?谢谢

 import random

 count = 1
 word = ['orange' , 'apple' , 'chicken' , 'python' , 'zynga'] #original list
 randomWord = list(random.choice(word)) #defining randomWord to make sure random
 choice jumbled = ""
 length = len(randomWord)

 for wordLoop in range(length):

    randomLetter = random.choice(randomWord)
    randomWord.remove(randomLetter)
    jumbled = jumbled + randomLetter

 print("The jumbled word is:", jumbled)
 guess = input("Please enter your guess: ").strip().lower()

 while guess != randomWord:
      print("Try again.")
      guess = input("Please enter your guess: ").strip().lower()
      count += 1
      if guess == randomWord:
       print("You got it!")
       print("Number of guesses it took to get the right answer: ", count)
此行删除变量中的每个字母。 您可以使用:

randomWord2 = randomWord.copy()
for wordLoop in range(length):
    randomLetter = random.choice(randomWord2)
    randomWord2.remove(randomLetter)
    jumbled = jumbled + randomLetter
这将复制您的变量。如果您不这样做,您的结果将是同一变量的两个名称

如果将列表与字符串进行比较,请尝试以下操作:

while guess != ''.join(randomWord):

它会将您的列表转换回字符串。

中断存在循环当这个问题是关于while循环时,这怎么可能是关于for循环的问题的重复?更多关于这个问题的讨论甚至不是关于如何摆脱while循环。它应该是类似于“为什么我的while条件总是评估为true”的内容。@spow您可以投票重新打开。结束评论是@Cœur谢谢你的提示。如果可以,我会投票赞成重新开放。看来我的名声不够好。但我认为我的回答达到了OP的要求,而且可能已经有所帮助。
while guess != ''.join(randomWord):