python-3.x 刽子手游戏,如果条件不正常工作

lokaqttq  于 2023-01-18  发布在  Python
关注(0)|答案(1)|浏览(129)

我想在屏幕上打印一条消息,说“你已经输入了那个字母”,只有在该字母是以前输入的情况下。如果该字母是第一次输入,它不应该打印。下面是我的代码,但它打印的消息,即使该字母是第一次输入:

import requests
import random
import hangman_art #import logo
word_site = "https://www.mit.edu/~ecprice/wordlist.10000"
response = requests.get(word_site)
WORDS = response.text.splitlines() 
chosen=random.choice(WORDS) #use this or lines 8,9,10
#https://stackoverflow.com/questions/75139406/how-to-pick-a-string-from-a-list-and-convert-it-to-a-list-python?noredirect=1#comment132596447_75139406
# ~ rand=random.randint(0,10000)
# ~ pick=WORDS[rand]
# ~ pick_as_list=list(pick)
print(hangman_art.logo)
print(chosen)
stages = ['''
  +---+
  |   |
  O   |
 /|\  |
 / \  |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|\  |
 /    |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|\  |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|   |
      |
      |
=========''', '''
  +---+
  |   |
  O   |
  |   |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
      |
      |
      |
=========
''', '''
  +---+
  |   |
      |
      |
      |
      |
=========
''']

x=[]
lives=6
for letter in chosen:
    x+='_'
s=' '.join(x)    
print(s)
while '_' in x:
    user=input("Guess a letter: ").lower()
    if user in chosen:                       #<---------------------------- My trial
        print(f"You've already guessed {user}")
    for i in range(0,len(chosen)):
        if chosen[i]==user:
            x[i]=user
    s=' '.join(x)
    print(s)
    if '_' not in x:
        print("You Win!")
    if user not in chosen:
        lives-=1
        print(stages[lives])
        print(f"You guessed {user}, that's not in the word. You lose a life ({lives} left).")
        if lives==0:
            print("You lose.")
            break

我还注意到,如果用户重复错误的字母,它会夺走用户的生命。我希望它只在第一次尝试错误的字母时夺走用户的生命。

zpqajqem

zpqajqem1#

最简单的方法就是在“你已经猜到了”print语句后面加上一个“continue”,但是我也建议你使用elif和else语句来实现更好的编程。

相关问题