我有这个单词un-scrambler游戏,只能在CMD或python shell中运行。当用户正确或错误地猜出单词时,它会显示“按任意键再次播放”
如何重新开始?
答案 0 :(得分:5)
在评估用户的输入后,不要让程序退出;相反,在循环中执行此操作。例如,一个甚至不使用函数的简单示例:
phrase = "hello, world"
while input("Guess the phrase: ") != phrase:
print("Incorrect.") # Evaluate the input here
print("Correct") # If the user is successful
这会输出以下内容,同时显示我的用户输入:
Guess the phrase: a guess
Incorrect.
Guess the phrase: another guess
Incorrect.
Guess the phrase: hello, world
Correct
这显然很简单,但逻辑听起来像你所追求的。一个稍微复杂一点的版本,有定义的函数可供您查看逻辑适合的位置,可以是这样的:
def game(phrase_to_guess):
return input("Guess the phrase: ") == phrase_to_guess
def main():
phrase = "hello, world"
while not game(phrase):
print("Incorrect.")
print("Correct")
main()
输出相同。
答案 1 :(得分:1)
尝试循环:
while 1==1:
[your game here]
input("press any key to start again.")
或者如果你想得到幻想:
restart=1
while restart!="x":
[your game here]
input("press any key to start again, or x to exit.")
答案 2 :(得分:1)
这是一个可用于重新运行代码块的模板。可以将#code视为一行或多行Python代码的占位符。
def my_game_code():
#code
def foo():
while True:
my_game_code()
答案 3 :(得分:1)
while True:
print('Your game yada-yada')
ans=input('''press o to exit or any key to continue
''')
if ans=='o':
break
答案 4 :(得分:0)
您可以使用简单的while
循环:
line = "Y"
while line[0] not in ("n", "N"):
""" game here """
line = input("Play again (Y/N)?")
希望这会有所帮助
答案 5 :(得分:0)
一种简单的方法也是使用布尔值,如果你是初学者(比如我),你会更容易理解。这就是我为团队项目所做的事情:
restart = True
while restart:
#the program
restart = raw_input("Press any key to restart or q to quit!")
if restart == "q":
restart = False
答案 6 :(得分:0)
您需要将代码块埋在另一个代码块中。 请按照以下说明进行操作:
Step 1: Top of code def main()
Step 2: restart = input("Do you want to play a game?").lower()
Step 3: Next line; if restart == "yes":
Step 4: Next line; Indent - main()
Step 5: Next line; else:
Step 6: Indent - exit()
Step 7: Indent all code under def main():
Step 8: After all code indent. Type main()
您正在做的是将代码块封装到主变量中。该程序在main()变量中运行一次,然后退出并返回以再次运行主变量。重复游戏。希望这会有所帮助。
def main():
import random
helper= {}
helper['happy']= ["It is during our darkest moments that we must focus to see the light.",
"Tell me and I forget. Teach me and I remember. Involve me and I learn.",
"Do not go where the path may lead, go instead where there is no path and leave a trail.",
"You will face many defeats in life, but never let yourself be defeated.",
"The greatest glory in living lies not in never falling, but in rising every time we fall.",
"In the end, it's not the years in your life that count. It's the life in your years.",
"Never let the fear of striking out keep you from playing the game.",
"Life is either a daring adventure or nothing at all."]
helper['sad']= ["Dont cry because it’s over, smile because it happened.",
"Be yourself; everyone else is already taken",
"No one can make you feel inferior without your consent.",
"It’s not who you are that holds you back, its who you think you're not.",
"When you reach the end of your rope, tie a knot in it and hang on."]
answer = input ('How do you feel : ')
print("Check this out : " , random.choice(helper[answer]))
restart = input("Do you want a new quote?").lower()
if restart == "yes":
main()
else:
exit()
main()
答案 7 :(得分:0)
如何在python中使用用户输入[是/否]重新运行代码?
强文本
如何在 python 中使用用户输入 [是/否] 重新运行代码? 强文本
内部代码 定义主(): 试试:
print("Welcome user! I am a smart calculator developed by Kushan\n'//' for Remainder\n'%' for Quotient\n'*' for Multiplication\n'/' for Division\n'^' for power")
num1 = float(input("Enter 1st number: "))
op = input("Enter operator: ")
num2 = float(input("Enter 2nd number: "))
if op == "+":
print(num1 + num2)
elif op =="-":
print(num1 - num2)
elif op =="*":
print(num1 * num2)
elif op =="/" :
print(num1 / num2)
elif op =="%":
print(num1 % num2)
elif op =="//":
print(num1 // num2)
elif op == "^":
print(num1 ** num2)
else:
print("Invalid number or operator, Valid Operators < *, /, +, -, % , // > ")
except ValueError:
print("Invalid Input, please input only numbers")
restart = input("Do you want to CALCULATE again? : ")
if restart == "yes":
main()
else:
print("Thanks! for calculating keep learning! hope you have a good day :)")
exit()
主() 强文本
您可能尝试运行整个代码,并选择让用户键入“是”或“否”以再次运行程序而无需手动运行。 这是我的“计算器”代码,我在其中使用了这个东西。 这是您的解决方案吗?
顺便说一下,这是我学习应用此功能的参考链接。 https://prnt.sc/12uos4s