在它询问您是否要重新启动程序的底部,我需要找到一种方法来重新启动它。我已经尝试过函数和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")
答案 0 :(得分:0)
首先,你有
的错误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:
但对于像这样的小项目来说没关系。