简体   繁体   English

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

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

So I'm working on a small text based game and I am stuck. 所以我在做一个小型的基于文本的游戏,所以我陷入了困境。 I got my code to print the option but it prints it twice. 我得到了打印选项的代码,但打印了两次。 Here is my code: 这是我的代码:

#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)

This code currently prints option 1 twice. 该代码当前将选项1打印两次。 I'm not really sure how to format it so that it prints option 2 if user types in "B" and option 3 if they type in "C". 我不太确定如何格式化它,以便如果用户键入“ B”会打印选项2,如果用户键入“ C”会打印选项3。 Any help is appreciated. 任何帮助表示赞赏。

Edit: I initially tried an if statement to no avail. 编辑:我最初尝试if语句无济于事。 A for loop at least printed option A, albeit twice. 一个for循环至少打印了选项A,尽管两次。

You need to choose value from dictionary ie a, like this:- 您需要从字典(即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])

Try This one: 试试这个:

#!/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])

When the input is 'A': 当输入为“ A”时: 在此处输入图片说明

When the input is 'B': 当输入为“ B”时:

在此处输入图片说明

When the input is 'C': 当输入为“ C”时:

在此处输入图片说明

The following approach works to print the options. 以下方法可以打印选项。 if you type A or B or C, then you will get the corresponding options in the dictionary. 如果您键入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")

The output: 输出:

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