Python 如何在不重复的情况下重复我的程序

Python 如何在不重复的情况下重复我的程序,python,python-3.6,Python,Python 3.6,我试图找出如何在不重复的情况下重复我的程序代码,但我读过的所有答案都没有意义。有人能帮帮我吗? 这是我正在做的程序 import random words=['hello','run','apple','day','month','cat','dog','bird','car','water'] word=random.choice(words) length=len(word) life=50 print('\t\tGuess the word!') print('instructions:

我试图找出如何在不重复的情况下重复我的程序代码,但我读过的所有答案都没有意义。有人能帮帮我吗? 这是我正在做的程序

import random
words=['hello','run','apple','day','month','cat','dog','bird','car','water']
word=random.choice(words)
length=len(word)
life=50
print('\t\tGuess the word!')
print('instructions: Guess the word. The word is only written by the alphabets.')
pn=input('Type your player name :')
print('Use this to help you! :',words)
print('The length of the word is',length,'letters')
fl=input('Guess the first letter of the word! :')
if fl==word[0]:
    print('Whoah! Nice guess',pn)
else:
    life=life-1
    print('Nice guess but wrong. Try again! You have',life,'lives left!')

执行一个具有永久真实条件的while循环

只需在代码中添加whileTrue:将使该代码段永远运行

import random

while(True):
    words=['hello','run','apple','day','month','cat','dog','bird','car','water']
    word=random.choice (words)
    length=len(word)
    life=50
    print('\t\tGuess the word!')
    print('instructions: Guess the word. The word is only written by the alphabets.')
    pn=input('Type your player name :')
    print('Use this to help you! :',words)
    print('The length of the word is',length,'letters')
    fl=input('Guess the first letter of the word! :')
    if fl==word[0]:
        print('Whoah! Nice guess',pn)
    else:
        life=life-1
        print('Nice guess but wrong. Try again! You have',life,'lives left!')

我不确定我是否理解正确,但您可以添加布尔值correct或not,并使用while循环继续循环,直到给出正确答案。 大概是这样的:

while incorrect:
    run
    loop
answer is correct

循环中的代码应该缩进。

我相信这就是您要做的:

import random

life=50
words=['hello','run','apple','day','month','cat','dog','bird','car','water']
print('\t\tGuess the word!')
print('instructions: Guess the word. The word is only written by the alphabets.')
pn=input('Type your player name :')
print('Use this to help you! : {0}'.format(words))

while life:
    word=random.choice(words)
    length=len(word)
    print('The length of the word is {0} letters'.format(length))
    fl=input('Guess the first letter of the word! :')
    if fl==word[0]:
        print('Whoah! Nice guess {0}'.format(pn))
    else:
        life=life-1
        print('Nice guess but wrong. Try again! You have {0} lives left!'.format(life))

你听说过循环吗?这些答案中哪一部分没有意义?很可能你会得到同样的答案