简体   繁体   中英

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. 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". Any help is appreciated.

Edit: I initially tried an if statement to no avail. A for loop at least printed option A, albeit twice.

You need to choose value from dictionary ie a, like this:-

#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': 在此处输入图片说明

When the input is 'B':

在此处输入图片说明

When the input is '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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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