简体   繁体   中英

Why my code doesn't work? "while" doesn't stop

Why it doesn't work? It should be stopped. My v == 3 .

while t < 3 , it should be stopped,

You see in the code that my t is worth to 4 ,

Why my while doesn't stop?

t = 4 < 3  == false

My code:

import random

stages = ['''
  +---+
  |   |
  O   |
 /|\  |
 / \  |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|\  |
 /    |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|\  |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|   |
      |
      |
=========''', '''
  +---+
  |   |
  O   |
  |   |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
      |
      |
      |
=========
''', '''
  +---+
  |   |
      |
      |
      |
      |
=========
''']

letters_number = 3

word_list = ["ardvark", "baboon", "camel"]

c = []
a = random.randint(0,(len(word_list) -1))
word_generator = word_list[a]
#print(word_generator)
d = 0
q = ""
z = 0
h = ''
t = 0
v = 3

for i in range(0,len(word_generator)):
    if i < len(word_generator):
      c += '_'

while  d  < len(word_generator) or int(t) < v:
  chose = input("Guess a letter\n")
  for i in range(0,len(word_generator)):

    if chose == word_generator[i]:
       c[i] = word_generator[i]
       d += 1
       #print("right")
    if c[i] == chose:
      z += 1000
    if c[i] != chose:
      z += -1
    if z < 0:
      h ="Wrong"
      z = 0
    else:
      h = "True"

  if h == "Wrong":
    t += 1

  print(c)
  print(z)
  print(h)
  h = ''
  z = 0
  print("t =",t)
if d == len(word_generator):
  print("you win")
if t == letters_number:
  print("you loose")

运行代码的输出截图

The while condition has two parts:

while  d  < len(word_generator) or int(t) < v

If the t < v part is false, the loop will still run if the d < len() part is true, because it is an or condition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM