簡體   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