簡體   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