繁体   English   中英

如何根据用户输入显示if语句?

[英]How do I get if statements to print based on user input?

所以我在做一个小型的基于文本的游戏,所以我陷入了困境。 我得到了打印选项的代码,但打印了两次。 这是我的代码:

#options are presented here

a1 = "A. Inspect desk"

b1 = "B. Open door"

c1 = "C. Turn off lights"

print (a1)

print (b1)

print (c1)

ans1 = input('Type A, B, or C and press enter:')

option_1 = ("You find a notebook and a pen; there are illegible markings in the notebook.")

option_2 = ("You walk up and open the door. Some creature, vaguely humanoid, slams you into the ground and injects your arm \
with something. The creature leaves and you slowly lose consciousness…")

option_3 = ("You turn off the lights. Something or someone opens the door and peers in, it then closes the door. A moment \
passes and you believe the being is gone. You then walk about and encounter a hallway.")

dic1 = {"A": option_1, 'B': option_2, 'C': option_3}

for a in ans1:

    print (option_1)

该代码当前将选项1打印两次。 我不太确定如何格式化它,以便如果用户键入“ B”会打印选项2,如果用户键入“ C”会打印选项3。 任何帮助表示赞赏。

编辑:我最初尝试if语句无济于事。 一个for循环至少打印了选项A,尽管两次。

您需要从字典(即a)中选择值,例如:-

#options are presented here

a1 = "A. Inspect desk"

b1 = "B. Open door"

c1 = "C. Turn off lights"

print (a1)

print (b1)

print (c1)

ans1 = input('Type A, B, or C and press enter:')

option_1 = ("You find a notebook and a pen; there are illegible markings      in the notebook.")

option_2 = ("You walk up and open the door. Some creature, vaguely humanoid, slams you into the ground and injects your arm \
with something. The creature leaves and you slowly lose consciousness…")

option_3 = ("You turn off the lights. Something or someone opens the door and peers in, it then closes the door. A moment \
passes and you believe the being is gone. You then walk about and encounter a hallway.")

dic1 = {"A": option_1, 'B': option_2, 'C': option_3}

print (dict1[a])

试试这个:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
a1 = "A. Inspect desk"

b1 = "B. Open door"

c1 = "C. Turn off lights"

print (a1)

print (b1)

print (c1)

ans1 = raw_input('Type A, B, or C and press enter:')

option_1 = "You find a notebook and a pen; there are illegible markings in the notebook."

option_2 = "You walk up and open the door. Some creature, vaguely humanoid, slams you into the ground and injects your arm \
            with something. The creature leaves and you slowly lose consciousness…"

option_3 = ("You turn off the lights. Something or someone opens the door and peers in, it then closes the door. A moment \
            passes and you believe the being is gone. You then walk about and encounter a hallway.")

dic1 = {'A': option_1, 'B': option_2, 'C': option_3}

print(dic1[ans1])

当输入为“ A”时: 在此处输入图片说明

当输入为“ B”时:

在此处输入图片说明

当输入为“ C”时:

在此处输入图片说明

以下方法可以打印选项。 如果您键入A或B或C,那么您将在字典中获得相应的选项。

option_1 = "You find a notebook and a pen; there are illegible markings in the notebook."
option_2 = "You walk up and open the door. Some creature, vaguely humanoid, slams you into the ground and injects your arm with something. The creature leaves and you slowly lose consciousness…"
option_3 = "You turn off the lights. Something or someone opens the door and peers in, it then closes the door. A moment passes and you believe the being is gone. You then walk about and encounter a hallway."
dic1 = {"A": option_1, 'B': option_2, 'C': option_3}

ans1 = input('Enter type A, B, C\t:')
if ans1 in dic1:
    print(dic1[ans1])
else:
    print("Invalid Key")

输出:

Enter type A, B, C  :A
You find a notebook and a pen; there are illegible markings in the notebook.

Enter type A, B, C  :B
You walk up and open the door. Some creature, vaguely humanoid, slams you into the ground and injects your arm with something. The creature leaves and you slowly lose consciousness…

Enter type A, B, C  :C
You turn off the lights. Something or someone opens the door and peers in, it then closes the door. A moment passes and you believe the being is gone. You then walk about and encounter a hallway.

Enter type A, B, C  :a
Invalid Key

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM