Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在仍然执行脚本的情况下清除python解释器?_Python - Fatal编程技术网

如何在仍然执行脚本的情况下清除python解释器?

如何在仍然执行脚本的情况下清除python解释器?,python,Python,我正试图建立我自己的刽子手游戏,我想让用户选择用自己的话。到目前为止,这一切似乎都是可行的,但当用户确实想输入自己的单词以供猜测时,其他猜测的人仍然能够在解释器中或脚本运行的任何地方看到它 import random # welcome-message as ASCII welcome = """ ** ** ** **** ** ******** **** **** **

我正试图建立我自己的刽子手游戏,我想让用户选择用自己的话。到目前为止,这一切似乎都是可行的,但当用户确实想输入自己的单词以供猜测时,其他猜测的人仍然能够在解释器中或脚本运行的任何地方看到它

import random

# welcome-message as ASCII
welcome = """
  **      **       **       ****     **     ********    ****     ****       **       ****     **
/**     /**      ****     /**/**   /**    **//////**  /**/**   **/**      ****     /**/**   /**
/**     /**     **//**    /**//**  /**   **      //   /**//** ** /**     **//**    /**//**  /**
/**********    **  //**   /** //** /**  /**           /** //***  /**    **  //**   /** //** /**
/**//////**   **********  /**  //**/**  /**    *****  /**  //*   /**   **********  /**  //**/**
/**     /**  /**//////**  /**   //****  //**  ////**  /**   /    /**  /**//////**  /**   //****
/**     /**  /**     /**  /**    //***   //********   /**        /**  /**     /**  /**    //***
//      //   //      //   //      ///     ////////    //         //   //      //   //      /// 
"""

# preset for words to be guessed
random_nouns = ["brick", "servant", "pan", "vest", "bead", "end", "stocking", "lettuce", "flag", "wash", "sea", "ice",
                "amount", "fruit", "train", "railway", "dolls", "stitch",
                "bone", "shame", "step", "smell", "error", "stone", "theory", "cows", "shape", "apparatus",
                "representative", "crown", "wish", "space", "calculator", "actor",
                "beginner"]
numerals = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]
solution = None

print(welcome)

user_choice = input(
    "Do you want to guess a preset word or do you want your own word to be guessed? Y for preset, N for own word. ").lower()
if user_choice == "y":
    print("\nA preset word has been chosen. The game will begin now!\n")
    solution = random_nouns[random.randint(0, len(random_nouns) - 1)]
elif user_choice == "n":
    solution = input(str("\nWhich word do you want to be guessed? ")).lower()
    for letter in solution:
        if letter in numerals or " " in solution:
            print("\nYour word contains invalid characters (i.e. numbers or spaces), please start again.")
            quit()
        else:
            print("\nA word to be guessed has been chosen. The game will begin now!\n")
            break
elif user_choice != "y" and user_choice != "n":
    print("\nWrong choice. Either enter Y for preset word or N for own word. Try again.\n")
    quit()

# split word in characters and add it to a list so the guessed letters can be matched
solution_chars = list(solution)
solution_as_underscore = list("_" * len(solution_chars))
solution_as_underscore_string = "".join(solution_as_underscore)
# print(solution)
# print(solution_chars)

# to check if input is not numerical and only alphabetical
number_of_chars = len(solution)
solution_try = []
right_letter_index = 0
fail_count = 0
fail_count_max = len(solution)
all_indexes = []

print(solution_as_underscore_string, "\n")

while fail_count < len(solution):
    solution_as_underscore = [x for x in solution_as_underscore if x != " "]
    # print(solution_as_underscore_string, "\n")
    # print(solution_chars)
    guessed_letter = input("Try a letter! ").lower()
    if len(guessed_letter) > 1:
        print("\nInvalid input, input can only be 1 letter.\n")
        print("".join(solution_as_underscore), "\n")
        fail_count += 1
        pass
    elif guessed_letter in numerals:
        print("\nPlease only use letters, not numbers.\n")
        print("".join(solution_as_underscore), "\n")
        fail_count += 1
        pass
    elif guessed_letter not in solution_chars:
        # TODO: Build Hangman
        print("\nThat was not correct.\n")
        print("".join(solution_as_underscore), "\n")
        fail_count += 1
        # print(fail_count)
        pass
    elif guessed_letter in solution_chars:
        for fum, letter in enumerate(solution_chars):
            if letter == guessed_letter:
                for i in range(0, len(solution_chars)):
                    if solution_chars[i] == guessed_letter:
                        solution_as_underscore[i] = guessed_letter
                    elif solution_chars[0] == guessed_letter:
                        solution_as_underscore[0] = guessed_letter.upper()
                print("\nThat was correct.\n")
                print("".join(solution_as_underscore), "\n")
                if "_" not in solution_as_underscore:
                    print(f"You won! The solution was \"{solution.title()}\".")
                    quit()
                break
    if solution_as_underscore[0] != "_":
        solution_as_underscore[0] = solution_as_underscore[0].upper()
    if fail_count == len(solution):
        print(f"\nOut of guesses. You lost. The solution was \"{solution.title()}\".")
        quit()
随机导入
#欢迎信息为ASCII
欢迎光临“”
**      **       **       ****     **     ********    ****     ****       **       ****     **
/**     /**      ****     /**/**   /**    **//////**  /**/**   **/**      ****     /**/**   /**
/**     /**     **//**    /**//**  /**   **      //   /**//** ** /**     **//**    /**//**  /**
/**********    **  //**   /** //** /**  /**           /** //***  /**    **  //**   /** //** /**
/**//////**   **********  /**  //**/**  /**    *****  /**  //*   /**   **********  /**  //**/**
/**     /**  /**//////**  /**   //****  //**  ////**  /**   /    /**  /**//////**  /**   //****
/**     /**  /**     /**  /**    //***   //********   /**        /**  /**     /**  /**    //***
//      //   //      //   //      ///     ////////    //         //   //      //   //      /// 
"""
#预设要猜测的单词
随机名词=[“砖头”、“仆人”、“盘子”、“背心”、“珠子”、“末端”、“长袜”、“莴苣”、“旗帜”、“洗涤”、“海”、“冰”,
“金额”、“水果”、“火车”、“铁路”、“娃娃”、“针迹”,
“骨头”、“耻辱”、“脚步”、“气味”、“错误”、“石头”、“理论”、“奶牛”、“形状”、“器械”,
“代表”、“王冠”、“愿望”、“空间”、“计算器”、“演员”,
“初学者”]
数字=[“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“0”]
解决方案=无
印刷(欢迎)
用户选择=输入(
“您是想猜一个预设单词还是想猜自己的单词?Y表示预设,N表示自己的单词。”)。lower()
如果用户_选项==“y”:
打印(“\n已选择预设单词。游戏将立即开始!\n”)
解=随机名词[random.randint(0,len(随机名词)-1]
elif user_choice==“n”:
solution=input(str(“\n您想猜哪个词?”)。lower()
对于解决方案中的字母:
如果数字中的字母或溶液中的“”:
打印(“\n您的单词包含无效字符(即数字或空格),请重新开始。”)
退出
其他:
打印(“\n已选择要猜测的单词。游戏将立即开始!\n”)
打破
elif用户选择!=“y”和用户选择!=“n”:
打印(“\N错误选择。为预设单词输入Y或为自己的单词输入N。重试。\N”)
退出
#将单词拆分为几个字符,并将其添加到列表中,以便匹配猜测的字母
解决方案\字符=列表(解决方案)
解决方案作为下划线=列表(“\u”*len(解决方案字符))
解决方案作为下划线作为字符串=“加入(解决方案作为下划线)
#打印(解决方案)
#打印(解决方案字符)
#检查输入是否不是数字且仅按字母顺序
字符数=len(溶液)
解决方案_try=[]
右字母索引=0
失败计数=0
失败计数最大值=len(解决方案)
所有_索引=[]
打印(解决方案作为下划线字符串“\n”)
当失败计数1:
打印(“\n无效输入,输入只能是1个字母。\n”)
打印(“.join(解决方案作为下划线),“\n”)
失败计数+=1
通过
elif用数字猜出字母:
打印(“\n请只使用字母,不要使用数字。\n”)
打印(“.join(解决方案作为下划线),“\n”)
失败计数+=1
通过
elif猜测字母不在解决方案字符中:
#TODO:建造刽子手
打印(“\n不正确。\n”)
打印(“.join(解决方案作为下划线),“\n”)
失败计数+=1
#打印(失败计数)
通过
elif猜到了解决方案字符中的字母:
对于fum,枚举中的字母(溶液字符):
如果字母==猜测字母:
对于范围(0,len(溶液字符))内的i:
如果解决方案字符[i]==猜测字母:
解决方案作为下划线[i]=猜测字母
elif解决方案字符[0]==猜测字母:
解决方案作为下划线[0]=猜测字母。上限()
打印(“\n正确。\n”)
打印(“.join(解决方案作为下划线),“\n”)
如果解决方案中没有“u”作为下划线:
打印(f“您赢了!解决方案是\“{solution.title()}\”)
退出
打破
如果解决方案为下划线[0]!="_":
解决方案作为下划线[0]=解决方案作为下划线[0]。上限()
如果失败_count==len(解决方案):
打印(f“\n没有猜测。您丢失了。解决方案是\“{solution.title()}\”)
退出
如果我输入“hello”作为测试,这是我的输出

“你想让人猜哪个词?喂

已经选择了一个要猜的词,游戏现在就开始!”

我想在输入单词后删除第一行,然后打印欢迎信息

有什么想法吗?

你可以这样做

导入操作系统
os.system('cls'如果os.name='nt'或者'clear')

您可能只需使用操作系统模块并清除终端即可