作为一个完整的初学者无法弄清楚,最佳实践是从给定的示例中编写代码。 我觉得我想尽可能地避免使用布尔值,因为它使我对代码的可读性和控制性降低了。
我真的很想从一开始就学会编写非常好的干净代码。
我尝试使用布尔值。 没有布尔值。 且变量较少。
secret_word = "giraffe"
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False
while guess != secret_word and not(out_of_guesses):
if guess_count < guess_limit:
guess = input("enter guess: ")
guess_count += 1
else:
out_of_guesses = True
if out_of_guesses:
print("YOU LOSE!")
else:
print("YOU WIN!")
secret_word = "another"
guess = ""
guess_count = 0
guess_limit = 3
while guess != secret_word:
if guess_count < guess_limit:
guess = input("enter guess: ")
guess_count += 1
if guess == secret_word:
print("YOU WIN!")
else:
print("YOU LOSE!")
break
secret_word = "another"
guess = ""
guess_count = 0
while guess != secret_word:
if guess_count < 3:
guess = input("enter guess: ")
guess_count += 1
if guess == secret_word:
print("YOU WIN!")
else:
print("YOU LOSE")
break
在学校示例中,短绒棉布表示在while语句的布尔值中有多余的括号。
答案 0 :(得分:1)
我想在您的情况下,如果您做对了,最好打破循环。 限制循环迭代的最大次数主要是通过for循环和范围来完成的。
easy_install lxml