简体   繁体   English

“无”出现在 output 中,我不知道为什么

[英]“None” appears in output and I can't figure out why

so I'm trying to build a hangman game in python.所以我正在尝试在 python 中构建一个刽子手游戏。 I've moreorless finished already but I can't figure out what in my code is causing the "None" to show up.我已经完成了,但我无法弄清楚我的代码中是什么导致“无”出现。 I've read it's due to printing 'empty' data resulting in "None" showing up.我读过它是由于打印“空”数据导致“无”出现。 I've tried removing the 'print' section individually but sometimes the programme doesn't run (Jupyter Notebook) which I don't really understand.我试过单独删除“打印”部分,但有时程序无法运行(Jupyter Notebook),我不太明白。

I'm looking to just remove the None output (it appears only after my first input).我只想删除 None output (它仅在我第一次输入后出现)。 Thanks!谢谢!

Image output图片 output


def get_word():
    wordlist = open("wordlist.txt").readlines()
    word = random.choice(wordlist) # this makes 'word' get a random choice from wordlist.txt
    return word.upper() #this sets all the text uppercase

#the get_word function should select a random value from the wordlist.txt file and set it to UPPER CASE

def play(word):
    word_length = len(word)-1
    word_completion = "_" * word_length
    guessed = False 
    guessed_letters = []
    wrong_guesses = 0
    print("Let's play hangman!")
    print("The word is", word_length, "letters long. You have 6 guesses")
    print(word_completion)
    while not guessed and wrong_guesses <= 6: 
        guess = input("Please guess a latter or word: ").upper()
        if len(guess) == 1 and guess.isalpha():
            if guess in guessed_letters:
                print("You already guessed this letter!", guess)
            elif guess not in word:
                print(guess, "is not in the word.")
                wrong_guesses = wrong_guesses + 1
                guessed_letters.append(guess)
                print("Incorrect guesses =", wrong_guesses)
                print("Letters already guessed: ", guessed_letters)
                
            else:
                print("Good job,", guess, "is in the word")
                guessed_letters.append(guess)
                print("Incorrect guesses =", wrong_guesses)
                print("Letters already guessed: ", guessed_letters)
                
                word_as_list = list(word_completion)
                indices = [i for i, letter in enumerate(word) if letter == guess]
                for index in indices:
                    word_as_list[index] = guess
                word_completion = "".join(word_as_list)
                if  "_" not in word_completion:
                    guessed = True 
                    
        elif len(guess) == len(word) and guess.isalpha():
            if guess in guessed_letters:
                print("You already guessed the word", guess)
                print("Incorrect guesses =", wrong_guesses)
                print("Letters already guessed: ", guessed_letters)
                
            elif guess != word:
                print(guess, "is not in the word.")
                wrong_guesses = wrong_guesses + 1 
                guessed_letters.apppend(guess)
            else:
                guessed = True
                word_completion = word
            
        else: 
            print("Not a valid guess.")
        print(print_graphics(wrong_guesses))
        print(word_completion)
        
    if guessed:
        print("Congrats, you guessed correctly, victory!")
    else:
        print("Sorry you lost, the word was", word)
        
        
def print_graphics(wrong_guesses):
    # list of possible body parts
    body_parts = ['  O     |', '  |     |',' /|     |', ' /|\    |', ' /      |', ' / \    |']
    
    lines = 4 if wrong_guesses != 0 else 5
    # check number provided is usable
    if 0 <= wrong_guesses <= 6:
        print('  +-----+')  # print top of frame
        print('  |     |')
    
        # print the correct body parts for current state
        if wrong_guesses > 0:
            if wrong_guesses > 1:
                print(body_parts[0])
                lines -= 1
            if wrong_guesses > 4:
                print(body_parts[3])
                lines -= 1
            print(body_parts[wrong_guesses-1])

        for i in range(lines):
            print('        |')  # print the lines
        
        print('==========')  # print the floor        
            
def main():
    word = get_word()
    play(word)
    while input("Play again? - Y/N ").upper() == "Y":
        word = get_word()
        play(word)
        
if __name__ == "__main__":
     main()

print(print_graphics(wrong_guesses))

your print_graphics function is doing the printing, but does not return any value.您的print_graphics function 正在打印,但不返回任何值。 The default return value is None .默认返回值为None Just change to:只需更改为:

print_graphics(wrong_guesses)

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

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