You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何根据用户二选一输入调用对应函数?该逻辑是否可行?

Hey there! Let’s tackle your two questions step by step—this is a super common pattern in interactive programming, so it’s totally doable with basic conditional logic.

1. Can we call one of two functions based on user input?

Absolutely! Every mainstream programming language supports conditional branching, which lets you route execution to different functions based on user input. The core idea is to capture the user’s input, evaluate it against your expected options, and trigger the matching function.

For example, in Python, here’s a quick snippet to show this in action:

def function_a():
    print("You called Function A!")

def function_b():
    print("You called Function B!")

# Get user input
user_choice = input("Enter 'A' or 'B': ").strip().upper()

# Route to the right function
if user_choice == "A":
    function_a()
elif user_choice == "B":
    function_b()
else:
    print("Invalid input—please enter 'A' or 'B'.")
2. How to call the corresponding function after asking a two-choice question?

This builds directly on the first question, but let’s structure it as a complete workflow to make it clear:

Step 1: Define your target functions

First, create the two functions you want to trigger—they can do anything from sending notifications to processing data.

def send_reminder_email():
    print("Reminder email sent successfully!")

def send_reminder_sms():
    print("Reminder SMS sent successfully!")

Step 2: Ask the two-choice question and capture input

Frame your question clearly so the user knows exactly what options to pick. Always clean up the input (like trimming whitespace or normalizing case) to avoid mismatches.

# Ask the question and process input
user_preference = input("How would you like to receive your reminder? (Enter 'email' or 'sms'): ").strip().lower()

Step 3: Use conditional logic to call the right function

Check the processed input against your options, and execute the matching function. Add a fallback for invalid inputs to make your program robust.

if user_preference == "email":
    send_reminder_email()
elif user_preference == "sms":
    send_reminder_sms()
else:
    print("Sorry, that's not a valid option. Please choose 'email' or 'sms'.")

Pro tip: Use dictionaries for scalability

If you ever want to add more functions later, using a dictionary to map input values to functions makes your code cleaner and easier to maintain:

# Map choices to functions
reminder_methods = {
    "email": send_reminder_email,
    "sms": send_reminder_sms
}

# Call the function directly from the dictionary
if user_preference in reminder_methods:
    reminder_methods[user_preference]()
else:
    print("Invalid option!")

This approach works across most languages—just adjust the syntax for things like input handling or function references (e.g., in JavaScript you’d use object literals instead of Python dictionaries).

内容的提问来源于stack exchange,提问作者Connor Denis-Barnard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:29:20