如何根据用户输入获取if语句?

时间:2018-02-03 05:06:15

标签: python python-3.x

所以我正在制作一个基于文本的小游戏,而且我被卡住了。我得到了我的代码来打印选项,但它打印两次。这是我的代码:

#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,虽然两次。

3 个答案:

答案 0 :(得分:0)

你需要从字典中选择值,即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])

答案 1 :(得分:0)

试试这个:

 Chart.types.doughnut.extend({
        draw: function (ease) {
                var ctx = this.chart.chart.ctx;

            var easingDecimal = ease || 1;
            Chart.helpers.each(this.getDataset().metaData, function (arc, index) {
                arc.transition(easingDecimal).draw();

                var vm = arc._view;
                var radius = (vm.outerRadius + vm.innerRadius) / 2;
                var thickness = (vm.outerRadius - vm.innerRadius) / 2;
                var angle = Math.PI - vm.endAngle - Math.PI / 2;

                ctx.save();
                ctx.fillStyle = vm.backgroundColor;
                ctx.translate(vm.x, vm.y);
                ctx.beginPath();
                ctx.arc(radius * Math.sin(angle), radius * Math.cos(angle), thickness, 0, 2 * Math.PI);
                ctx.arc(radius * Math.sin(Math.PI), radius * Math.cos(Math.PI), thickness, 0, 2 * Math.PI);
                ctx.closePath();
                ctx.fill();
                ctx.restore();
            });
        },
    }); 

当输入为'A'时: enter image description here

当输入为'B'时:

enter image description here

当输入为'C'时:

enter image description here

答案 2 :(得分:0)

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.

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