繁体   English   中英

刽子手游戏,如果条件不正常

[英]Hangman game, if condition not working properly

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

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   

我还注意到,如果用户重复错误的字母,这会夺走他们的生命。 我希望它只为错误字母的第一次审判而夺走生命。

最简单的做法是在“You've already guessed”打印语句之后添加一个“continue”。 但是,我也会建议一些更好的编程实践并使用 elif 和 else 语句。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM