【发布时间】:2021-01-07 22:55:41
【问题描述】:
这是我的代码(用于刽子手游戏):
import random, os
def main():
print("******THIS IS HANGMAN******")
print("1. Play Game ")
print("2. Quit Game ")
choice = input("Please enter option 1 or 2")
if choice == "1":
words = ["school", "holiday", "computer", "books"]
word = random.choice(words)
guess = ['_'] * len(word)
guesses = 7
while '_' in guess and guesses > 0:
print(' '.join(guess))
character = input('Enter character: ')
if len(character) > 1:
print('Only enter one character.')
continue
if character not in word:
guesses -= 1
for i, x in enumerate(word):
if x == character:
guess[i] = character
if guesses == 0:
print('You LOST!')
break
else:
print('You have only', guesses, 'chances left to win.')
else:
print('You won')
elif choice == "2":
os.system("cls")
main()
else:
print("that is not a valid option")
main()
我尝试过os.system("clear"),但它没有清除屏幕,我希望它清除整个屏幕,但是 (cls) 让它再次打印我的菜单,并且 (clear) 除了清除 2 之外什么都不做。如果我'我遗漏了一些明显的东西,这可能是因为我是 python 新手。
【问题讨论】:
标签: python-3.x