[英]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')
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.