简体   繁体   English

如何从用户输入中获得不同的结果(下一个问题将第一个问题的用户输入考虑在内进行计算)

[英]how to have different outcomes from a user input (next question taking the first question user input into account to do calculations)

#Elecricity Plan
#these are the electricity plans

eplan = input('Enter your electricity plan (EFIR or EFLR): ')

if eplan.lower() == 'efir':
 print('Thank you for choosing EFIR' )

elif eplan.lower() == 'eflr':
 print('Thank you for choosing EFLR')

else:
 print('Please enter your electricity plan in abbreviation')
  

Amount of Electricity used用电量

i am trying to make it so that when the person enters their answer to the electricity我正在努力做到这一点,以便当人们输入他们对电力的回答时
plan it does a different calculation in the next question based on if they chose EFIR or EFLR (responds to their input after taking into account the amount of electricity used and what plan they had in the last question) plan 它根据他们选择的是 EFIR 还是 EFLR 在下一个问题中进行不同的计算(在考虑到使用的电量和他们在上一个问题中的计划后响应他们的输入)

 kwha = int(input('Enter the amount of electricity you used in month ')) 

Simply you can do two types of calculations based on the first input because you already stored it in the eplan variable:您可以简单地根据第一个输入进行两种类型的计算,因为您已经将其存储在 eplan 变量中:

kwha = int(input('Enter the amount of electricity you used in month ')) 
if eplan.lower() == 'efir':
   #do calculation
elif eplan.lower() == 'eflr':
   #do another calculation

You can create different functions for different user inputs.您可以为不同的用户输入创建不同的函数。 But its not necessary, you can just use if blocks like you have done!但这不是必需的,你可以像你所做的那样使用 if 块!

def func_one():
    print('Thank you for choosing EFIR')
    # ...

def func_two():
    print('Thank you for choosing EFLR')
    # ...

and then do something like this:然后做这样的事情:

if eplan.lower() == 'efir':
    func_one()
elif eplan.lower() == 'eflr':
    func_two()
else:
    print('Please enter your electricity plan in abbreviation')

TIP : You can create a hash map for situations like this提示:您可以为这种情况创建一个 hash map

map = {'efir': func_one, 'eflr': func_two}

if eplan.lower() in map:
    map[eplan.lower()]()
else:
    print('Please enter your electricity plan in abbreviation')

You can also use match !您也可以使用匹配

You could consider using match/case like this by swapping the input order - ie, ask for the usage amount first then the plan type:您可以考虑通过交换输入顺序来考虑像这样使用匹配/大小写 - 即,首先询问使用量,然后询问计划类型:

amount = float(input('Enter the amount of electricity used: '))

match input('Enter your electricity plan (EFIR or EFLR): '):
    case 'EFIR':
        pass # do EFIR calculations here
    case 'EFLR':
        pass # do EFLR calculations here
    case _:
        print('Unknown plan type')

Pretty simple.很简单。 You can put the code directly inside the conditional blocks or separate it into functions您可以将代码直接放在条件块中或将其分成函数

if some_condition_is_true:
    # do calculation or call another function
elif ...
else ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据上一个函数的用户输入使下一个输入问题重复多次 - how to make next input question repeat a number of times based on user input from previous function 我如何创建一个函数,该函数根据在Python中输入相同输入的次数,使用户输入具有不同的结果? - How do I create a function that allows for user input to have a different outcomes based on how many times the same input was entered in Python? 如何根据用户对第一个问题的输入提示用户 x 次 - How do I promt user x amount of times based on their input on the first question 是否有一种简单的方法可以将用户输入作为整数使用,并将该整数用作下一个问题被问多少次的量? - Is there a simple way to take user input as an integer and use that integer as an amount for how many times the next question is asked? 用户输入的决策+将结果存储在列表中[未解决的问题] - Decision Making from User Input + store results in a list [Open Question] 您如何在python中创建一个自定义异常来处理用户输入,并考虑帐户中的值范围? - How do you create a custom exception that handles user input taking range of values in account, in python? 在 PYTHON 中接受用户的输入 - Taking input from the user in PYTHON 将列表作为用户的输入 - Taking a list as input from user 在python中,如何从用户那里获取输入并在输入后跳转到不同的行? - In python how can I take input from user and jump to a different line after taking input? 从用户获取数组输入 - Taking array input from user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM