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
在某些语言中,为了提高可读性,有时会倾向于使用switch case结构而不是long if elif else语句。 在Python中,没有switch case语句,但是您可以在字典中映射您的选择。函数也可以作为变量存储。
不,Python不是一种命令式语言,尽管它支持印象式编程。(即:Python中没有GOTO)。
应该使用函数和循环。
示例:
这将(非常琐碎地)继续打印“Doyouwantto:A)接近房子。B) 靠近马厩。[A/B]?“当您输入
Q
时停止。我将由你来继续用更多的逻辑来满足你想要的实现的代码结构。注意:以这种方式编写会很快变得困难和复杂,最终您会希望将代码拆分为函数、模块等
类似的,使用您的代码,while循环已被注释:
上面的示例只是一个如何完成任务的示例。 while循环将继续要求键盘旁的人输入“A”或“B”。然后检查输入。
在实际的代码中,您需要创建函数来捕获输入并进行所有花哨的检查。
相关问题 更多 >
编程相关推荐