python - “无”出现在输出中,我不知道为什么
问题描述
所以我正在尝试用python构建一个刽子手游戏。我已经完成了,但我无法弄清楚我的代码中是什么导致“无”出现。我读过它是由于打印“空”数据导致“无”出现。我试过单独删除“打印”部分,但有时程序无法运行(Jupyter Notebook),我不太明白。
我只想删除 None 输出(它只在我第一次输入后出现)。谢谢!
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))
您的print_graphics
函数正在打印,但不返回任何值。默认返回值为None
. 只需更改为:
print_graphics(wrong_guesses)
推荐阅读
- css - 如何创建具有不同颜色的左上角和右下角边框?
- python - python中的多变量优化
- python-3.x - 如何用字典中的值替换数据框系列/列中的旧字符串值?
- shell - 'adb shell' 这两个壳是链式的吗?
- ios - iOS:将 Braintree 返回的 UIView 添加到屏幕
- angular-material - 我需要导入哪些指令才能使用材料列表项?
- excel-formula - 如何根据列中的值从列表中选择值
- apache-spark - Spark 输出 JSON 与 Parquet 文件大小差异
- r - R中的海洋测深
- php - 为什么我的 MySQL 全文搜索不起作用?