当用Python编写一个非常简单的多项选择故事时,如果两个选项都没有选择,我可以调用一行来重复吗

2024-09-25 10:28:38 发布

您现在位置:Python中文网/ 问答频道 /正文

  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 

我刚刚开始学习python,它是我的第一门语言,所以我只是在使用它,else语句是否有可能再次请求输入,当A或B都没有输入时,它将被保存为同一个变量。

很抱歉,这个问题太幼稚了

编辑:

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 :

我对代码和故事都很抱歉,但我很感激你的帮助。如果你有任何更好的方法来学习语言的基础知识,以及构建这种类型的故事的建议,我很想知道:D


Tags: thetopathyouinputifdostable
3条回答

在某些语言中,为了提高可读性,有时会倾向于使用switch case结构而不是long 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()

不,Python不是一种命令式语言,尽管它支持印象式编程。(即:Python中没有GOTO)。

应该使用函数和循环。

示例:

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

这将(非常琐碎地)继续打印“Doyouwantto:A)接近房子。B) 靠近马厩。[A/B]?“当您输入Q时停止。我将由你来继续用更多的逻辑来满足你想要的实现的代码结构。

注意:以这种方式编写会很快变得困难和复杂,最终您会希望将代码拆分为函数、模块等

类似的,使用您的代码,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.")

上面的示例只是一个如何完成任务的示例。 while循环将继续要求键盘旁的人输入“A”或“B”。然后检查输入。

在实际的代码中,您需要创建函数来捕获输入并进行所有花哨的检查。

相关问题 更多 >