-3

我试图找出如何在不重复的情况下重复我的程序(代码),但我读过的所有答案都没有意义。那么有人可以帮助我吗?这是我正在研究的程序

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!')
4

3 回答 3

0

执行具有永久 True 条件的 while 循环

只需将while(True):添加到代码中即可使该代码段永远运行...

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!')
于 2017-01-28T20:48:17.087 回答
0

我不确定我是否理解正确,但您可以添加一个布尔值是否正确,并使用 while 循环继续循环,直到给出正确答案。像这样的东西可能:

while incorrect:
    run
    loop
answer is correct

循环内的代码应该缩进。

于 2017-01-28T20:51:51.703 回答
0

我相信这就是你想要做的:

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))
于 2017-01-28T20:53:47.613 回答