繁体   English   中英

如何在代码中重新启动 IDLE?

[英]How do i restart IDLE in the code?

在它询问您是否要重新启动程序的底部,我需要找到一种方法来重新启动它。 我尝试过函数和 while 循环,但它们都失败了。

continues=1

while continues==(1):   
    words=["adduct","carpet","garlic","guitar","detail","catkin","choral","longer","lizard","magpie","lawyer","patchy"]
    import random
    import time
    import sys
    import os

    letterr=0
    left=6
    guesses_left=10
    correct=["_","_","_","_","_","_"]

    chosen_word=(random.choice(words))
    letters=list(chosen_word)

    for letter in chosen_word:
            if letter:
             letterr+=1

    print(letters)
    print("The number of letters in the word is:", letterr)

    while guesses_left >= 1:
            time.sleep(0.5)
            print("")
            guess=input("Guess a letter of the word. ")
            print("")
            time.sleep(1)
            if guess in("abcdefghijklmnopjrstuvxywz"):

                    if guess in(letters):

                            if guess in(correct):
                                time.sleep(0.5)
                                print("You have already guessed this letter. ")
                                guesses_left=guesses_left - 1
                                time.sleep(1)
                                print("You have",guesses_left, "guesses left.")
                                time.sleep(1) 
                                print("Letters:"," ".join(correct))
                            else:
                                guesses_left=guesses_left-1
                                time.sleep(1)
                                print("You have succesfully guessed a letter in the word. ")
                                print("")
                                time.sleep(0.5)
                                print("You have",guesses_left, "guesses left.")
                                print("")
                                index=chosen_word.index(guess)
                                correct[index]=guess
                                time.sleep(0.5) 
                                print("Letters:"," ".join(correct))



                    else:
                            guesses_left=guesses_left-1
                            time.sleep(0.5)
                            print("")
                            print("You have incorrectly guessed a letter in the word. ")
                            print("")
                            time.sleep(0.5)
                            print("You have",guesses_left,"guesses left. ")
                            time.sleep(0.5)
                            print("")
                            print("Letters:"," ".join(correct))



                    if guesses_left==(0):
                            time.sleep(0.5)
                            print("")
                            print("Game over, you have not won")
                            time.sleep(0.5)
                            print("")
                            print("The word was:",wordd,)
                            restart0=0
                            while restart0==(0):
                                    restart=input("Would you like to play again? ")
                                    if restart in("yes","yeah","Yes","Yeah"):
                                            restart0=0
                                            continues=1
                                    if restart in("no","nope","No","Nope"):
                                            restart0=0
                                            print("")
                                            time.sleep(0.5)
                                            print("Thank you for playing")
                                            print("")
                                            time.sleep(2)
                                            quit()
                                    if restart not in("no","nope","No","Nope","yes","yeah","Yes","Yeah"):
                                            restart0=1
                                            print("")
                                            time.sleep(1)
                                            print("Enter a valid answer")
                                            print("")


            else:
                    print("Please only enter letters! ")
                    guesses_left=guesses_left - 1
                    print("")
                    time.sleep(0.5)
                    print("There are",left,"letters left in the word. ")
                    time.sleep(0.5)
                    print("")
                    print("Letters:"," ".join(correct))

            if ("_") not in(correct):
                    print("")
                    time.sleep(0.5)
                    print("You have won! ")
                    print("")
                    time.sleep(0.5)
                    restart1=input("Would you like to play again? ")
                    restart2=0
                    while restart2==(0):
                            if restart1 in("yes","yeah","Yes","Yeah"):
                                restart2=1
                                os.execv(idek, sys.argv)
                            if restart1 in("no","nope","No","Nope"):
                                restart2=1
                                print("")
                                time.sleep(1)
                                print("Thank you for playing ")
                                print("")
                                time.sleep(1)
                                quit()
                            if restart1 not in("no","nope","No","Nope","yes","yeah","Yes","Yeah"):
                                restart2=0
                                time.sleep(1)
                                print("")
                                print("Enter a valid answer")

首先你有一个错误

os.exec(idek, sys.argv)

“idek”没有在任何地方定义。

其次,您的代码在重新启动方面有点过于复杂。 您可以将所有内容都包含在一个 while 循环中,该循环将永远运行,除非您给它命令结束。 例如

while True:
# All of your logic for the guessing game goes here.
# at the end, ask the user if they want to restart.
# If they answer 'yes','yeah', etc, do nothing, the loop will automatically
# restart and reset all of your values.
# Else, do a break and the loop will end thus ending your game.

我希望这可以帮助您弄清楚您需要做什么。

旁注:我个人不同意使用

while True:

但是对于像这样的小项目来说还好。

暂无
暂无

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

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