在 Python 中制作一个非常简单的多项选择故事时,如果没有选择任何选项,我可以调用一行重复吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21082037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When making a very simple multiple choice story in Python, can i call a line to repeat if neither of the options are selected
提问by Hailstone13
d1a = input ("Do you want to: A) Approach the house. B) Approach the stable. [A/B]? : ")
if d1a == "A":
print ("You approach the cottage.")
elif d1a == "B":
print ("You approach the stables.")
else
I have only just started learning python and it is my first language so I am just playing about with it, Is it possible for the else statement to ask for the input again and it to be saved as the same variable when neither A or B is typed in.
我才刚刚开始学习 python,它是我的第一语言,所以我只是在玩它,else 语句是否有可能再次要求输入,并且当 A 或 B 都不是时,它会被保存为相同的变量输入。
P.S sorry before hand for the complete naivety of this question it turns out to be so
PS很抱歉这个问题的完全天真,事实证明是这样
EDIT:
编辑:
import time
import random
import sys
print ("You wake up to find yourself in a clearing off a forest, sounded by tall")
print (" trees on all sides with a path ahead of you")
d1 = input ("Do you want to : A) Walk down the path. B)Move your way through the trees? [A/B]: ")
if d1 == "A":
print ("You begin to walk down the path.")
print (" As a Sidenote, during this adventure 'dice' will be thrown and the success of your chosen action")
print (" will be determined by the result.")
r1 = random.randint(0.0,10.0)
if 4 > r1 > 0.1:
print (" Your groggyness from waking means you reach the edge of the forest after nightfall.")
print (" You see that the path continues towards a small cottage, the lights are out and no smoke rises from the chimney.")
print (" Away from the cottage is a stable, where you can see a horse standing with its head outside the door")
d1a = input ("Do you want to: A) Approach the house. B) Approach the stable. [A/B]? : ")
if d1a == "A":
print ("You approach the cottage.")
elif d1a == "B":
print ("You approach the stables.")
else :
I am both sorry for the code and the story but I would appreciate the help. If you have any suggestions of a better way to learn the basics of the language as well as construct this type of story I would love to know :D
我对代码和故事都感到抱歉,但我会很感激你的帮助。如果您对学习语言基础知识以及构建此类故事的更好方法有任何建议,我很想知道:D
回答by James Mills
No. Python is not an imperative language; although it does support impreative programming. (i.e: There is no GOTO in Python).
不。Python 不是命令式语言;尽管它确实支持命令式编程。(即:Python 中没有 GOTO)。
You should use either functions and loops.
您应该使用函数和循环。
Example:
例子:
while True:
d1a = input ("Do you want to: A) Approach the house. B) Approach the stable. [A/B]? : ")
if d1a == "A":
print ("You approach the cottage.")
elif d1a == "B":
print ("You approach the stables.")
elif dia == "Q":
break
This will (very trivially) keep printing "Do you want to: A) Approach the house. B) Approach the stable. [A/B]?" and stop when you enter Q. I twould be up to you to continue this structure of code with more logic to suit your desired implementation.
这将(非常简单地)继续打印“你想:A)接近房子。B)接近马厩。[A/B]?” 并在您进入时停止Q。我必须由您决定使用更多逻辑来继续这种代码结构以适合您所需的实现。
Note:Writing in this style will get difficulty and complex very quickly and eventually you will want to split up your code into functions, modules, etc.
注意:以这种方式编写会很快变得困难和复杂,最终您会想要将代码拆分为函数、模块等。
回答by Andy W
Something like this, using your code, while loop has been annotated:
像这样,使用您的代码,while 循环已被注释:
# gather the input
# "while" is the loop statement, checking the condition and executing the code in the body of loop while the condition holds true
# obviously, "while True" will execute its body forever until "break" statement executes or you press Ctrl+C on keyboard
while True:
d1a = input ("Do you want to: A) Approach the house. B) Approach the stable. [A/B]? : ")
# check if d1a is equal to one of the strings, specified in the list
if d1a in ['A', 'B']:
# if it was equal - break from the while loop
break
# process the input
if d1a == "A":
print ("You approach the cottage.")
elif d1a == "B":
print ("You approach the stables.")
The sample above is just an example of how to accomplish the thing. The while loop will keep asking the guy at the keyboard to enter 'A' or 'B'. Then you check your input.
上面的示例只是如何完成这件事的一个例子。while 循环将不断要求键盘上的人输入“A”或“B”。然后你检查你的输入。
In the real code you'll want to create function to capture the input and make all fancy checks.
在实际代码中,您需要创建函数来捕获输入并进行所有花哨的检查。
回答by Martin Hansen
In some languages You would sometimes tend to use switch-case structures instead of long if-elif-else statements in order to improve readability. In Python, there is no switch-case statement, but You can map Your choices in a dictionary. Functions can be stored as variables too.
在某些语言中,您有时会倾向于使用 switch-case 结构而不是长的 if-elif-else 语句以提高可读性。在 Python 中,没有 switch-case 语句,但您可以在字典中映射您的选择。函数也可以存储为变量。
def opt_a():
print("You approach the cottage.")
def opt_b():
print("You approach the stables.")
def invalid_opt():
print("Invalid choice")
options = {"A":["Approach the house",opt_a], "B":["Approach the stable",opt_b]}
for option in options:
print(option+") "+options.get(option)[0])
choise = input("Please make Your choise: ")
val = options.get(choise)
if val is not None:
action = val[1]
else:
action = invalid_opt
action()
回答by user4301088
# this code below welcomes the user
print ("hello and welcome to my python quiz,my name is brandon and i am the quiz master")
print ("please type your name")
your_name = input ()
your_name = str(your_name)
score = 0# this code set the veriable to zero
# this is the question
print ("Question 1")
print ("what is 50x10")
answer = input ()
answer =int(answer)
if answer == 500:
print ("good work")
score = score + 1
else:
print ("better luck next time")
score = score - 1
print ("Question 2")
print ("what is (5x10)-4x2")
answer = input ()
answer =int(answer)
if answer == 94:
print ("good work")
score = score + 1
else:
print ("better luck next time")
score = score - 1
print ("Question 3")
print ("what is 600000000x10")
answer = input ()
answer =int(answer)
if answer == 6000000000:
print ("good work")
score = score + 1
else:
print ("better luck next time")
score = score - 1
print ("Question 4")
print ("what is 600000000/10")
answer = input ()
answer =int(answer)
if answer == 60000000:
print ("good work")
score = score + 1
else:
print ("try again")
score = score - 1
print ("Question 5")
print ("what is 19x2-2")
answer = input ()
answer =int(answer)
if answer == 36:
print ("good work")
score = score + 1
else:
print ("try again")
score = score - 1
print ("Question 6")
print ("what is (8x4) x 9")
answer = input ()
answer =int(answer)
if answer == 288:
print ("good work")
score = score + 1
else:
print ("try again")
score = score - 1
print ("Question 7")
print ("what is 15 x4")
answer = input ()
answer =int(answer)
if answer == 60:
print ("good work")
score = score + 1
else:
print ("try again")
score = score - 1
print ("Question 8")
print ("what is 19 x9")
answer = input ()
answer =int(answer)
if answer == 171:
print ("good work")
score = score + 1
else:
print ("try again")
score = score - 1
print ("Question 9")
print ("what is 9 x9")
answer = input ()
answer =int(answer)
if answer == 81:
print ("good work")
score = score + 1
else:
print ("try again")
score = score - 1
print ("Question 10")
print ("what is 10 x10")
answer = input ()
answer =int(answer)
if answer == 171:
print ("good work")
score = score + 1
else:
print ("try again")
score = score - 1
print ("Question 10")
print ("what is 10 x10")
answer = input ()
answer =int(answer)
if answer == 171:
print ("good work")
score = score + 1
else:
print ("try again")
score = score - 1
print("Question 11")
print ("6+8x2: \n\
1.28 \n\
2.14 \n\
3.38 \n\
4.34 \n"
answer =int (input (Menu))
if answer == 1.:
print ("well done")
elif answer == 2.:
print ("better luck next time")
elif answer == 3.:
print ("looser")
elif answer == 4.:
print ("go back to primary school and learn how to add")
print("Question 12")
print ("6194+10x2: \n\
1.12409 \n\
2.124081 \n\
3.14321 \n\
4.12408 \n"
answer =int(input (Menu))
if answer == 1.:
print ("well done")
elif answer == 2.:
print ("better luck next time")
elif answer == 3.:
print ("looser")
elif answer == 4.:
print ("go back to primary school and learn how to add")
if score > 8:# this line tells the program if the user scored 2 points or over to print the line below
print("amazing your score is" + str(score))# this code tells the user that they have done well if they have got 2or more points or over
elif score < 4:
print ("good work your score is" + str(score))
else:
print ("better look next time your scor is" + str(score))
print("well done your score is " +str (score))#this print the users score
print("thank you for playing in the python maths quiz")

