我正在制作这款名为" Fruit Guessing"的游戏。我有一个9个水果的列表,我让游戏正常工作,直到你猜到所有字母都正确。不幸的是,我似乎无法弄清楚如何让游戏结束。我试图让它在代码的第44行结束我做了一个变量,每当你猜到一个正确的字母时,它会为该变量加1。该变量称为letters_guessed_correct。我试图得到它,以便当letters_guessed_correct等于所选择的随机水果的长度时,它会说"恭喜!你猜到了我想到的水果!"但是我无法让这部分工作,如果我开始工作,我仍然不确定如何让它结束游戏。这是一个学校项目,将于周二到期。这是我在计算机科学原理中的15%。请帮帮我。
我在名为repl.it的网站上运行Python 3
以下是完整代码:
import random
def beginning_of_program():
user_value = input('Would you like to look at the instructions?:')
if user_value=='yes':
instructions_page()
elif user_value=='no':
user_value=input('Are you ready to play Fruit Guessing?:')
if user_value=='yes':
run_game()
def instructions_page():
print()
print('1. I am going to come up with random fruits.')
print()
print('2. Try and guess the letters in the word')
print()
print('3. Write all of your guesses in lowercase letters')
print()
print('4. If you guess wrong 7 times then the game ends')
print()
user_value=input('Are you ready to play Fruit Guessing?:')
if user_value=='no':
print('Okay, have a good day')
elif user_value=='yes':
run_game()
def run_game():
wordlist=['apple','orange','grape','kale','kiwi','mango','cherry','peach','plum']
randfruit=random.choice(wordlist)
blanks='_ '*len(randfruit)
letters_guessed_correct=0
print()
print("Word: ",blanks)
newblanks=blanks
count=7
while count>0:
letters_guessed_correct=0
letters_guessed_list=[]
letters_guessed_correct_list=[]
print()
guess=input('Guess a letter from the fruit that I am thinking of:')
if letters_guessed_correct==len(randfruit):
print('Congratulations! You guessed the correct fruit I was thinking of!')
exit()
elif guess in letters_guessed_list:
print('You have already guessed this letter, please guess again.')
elif len(guess) !=1:
print('Guess one letter at a time!')
elif guess not in 'abcdefghijklmnopqrstuvwxyz':
print('Please only guess letters')
else:
for letters in randfruit:
if guess==letters:
if guess not in letters_guessed_list:
letterindex=randfruit.index(guess)
newblanks=newblanks[:letterindex*2]+guess+newblanks[letterindex*2+1:]
print(newblanks)
print('Guess is correct!')
letters_guessed_correct += 1
letters_guessed_correct_list.append(guess)
letters_guessed_list.append(guess)
break
if guess not in randfruit:
if count!=0:
count -= 1
letters_guessed_list.append(guess)
print("Guess is wrong!",count,"more failed attempts left.")
print()
print("Word:",newblanks)
print("Guess is wrong!",count,"more failed attempts left.")
print()
print("Word:",randfruit)
print()
print('You guessed,',str(letters_guessed_list))
print("Guess is wrong!",count,"more failed attempts left."
答案 0 :(得分:0)
import random
def beginning_of_program():
user_value = raw_input('Would you like to look at the instructions?:')
if user_value.lower() == 'yes':
instructions_page()
elif user_value.lower() == 'no':
user_value=raw_input('Are you ready to play Fruit Guessing?:')
if user_value=='yes':
run_game()
def instructions_page():
print('I am going to come up with random fruits.')
print('Try and guess the letters in the word')
print('Write all of your guesses in lowercase letters')
print('If you guess wrong 7 times then the game ends')
user_value=raw_input('Are you ready to play Fruit Guessing?:')
if user_value=='no':
print('Okay, have a good day')
elif user_value=='yes':
run_game()
def run_game():
wordlist=['apple','orange','grape','kale','kiwi','mango','cherry','peach','plum']
randfruit=random.choice(wordlist)
blanks='_ '*len(randfruit)
letters_guessed_correct=0
print()
print("Word: ",blanks)
newblanks=blanks
count=7
while count>0:
letters_guessed_correct=0
letters_guessed_list=[]
letters_guessed_correct_list=[]
print()
guess=raw_input('Guess a letter from the fruit that I am thinking of:')
if guess in letters_guessed_list:
print('You have already guessed this letter, please guess again.')
elif len(guess) !=1:
print('Guess one letter at a time!')
elif guess not in 'abcdefghijklmnopqrstuvwxyz':
print('Please only guess letters')
else:
for letters in randfruit:
if guess==letters:
if guess not in letters_guessed_list:
letterindex=randfruit.index(guess)
newblanks=newblanks[:letterindex*2]+guess+newblanks[letterindex*2+1:]
print(newblanks)
print('Guess is correct!')
letters_guessed_correct += 1
letters_guessed_correct_list.append(guess)
letters_guessed_list.append(guess)
break
if guess not in randfruit:
count -= 1
letters_guessed_list.append(guess)
print("Guess is wrong!",count,"more failed attempts left.")
print()
print("Word:",newblanks)
if newblanks.replace(" ", "") == randfruit:
print('Congratulations! You guessed the correct fruit I was thinking of!')
exit()
if __name__ == "__main__":
beginning_of_program()