首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何与用户输入有不同的结果(考虑到第一个问题用户输入的下一个问题来进行计算)

如何与用户输入有不同的结果(考虑到第一个问题用户输入的下一个问题来进行计算)
EN

Stack Overflow用户
提问于 2022-10-07 17:38:36
回答 4查看 46关注 0票数 0
代码语言:javascript
复制
#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')

用电量

我试着做到这样,当那个人进入他们的电答案时

计划--在下一个问题中,根据他们是否选择EFIR或EFLR (在考虑到用电量和他们在上一个问题中的计划后对他们的输入作出回应),做一个不同的计算。

代码语言:javascript
复制
 kwha = int(input('Enter the amount of electricity you used in month ')) 
EN

回答 4

Stack Overflow用户

发布于 2022-10-07 17:43:17

只需根据第一个输入进行两种类型的计算,因为您已经将其存储在eplan变量中:

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2022-10-07 17:49:30

您可以为不同的用户输入创建不同的函数。但这不是必要的,你可以只是使用,如果你已经做了块!

代码语言:javascript
复制
def func_one():
    print('Thank you for choosing EFIR')
    # ...

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

然后做这样的事情:

代码语言:javascript
复制
if eplan.lower() == 'efir':
    func_one()
elif eplan.lower() == 'eflr':
    func_two()
else:
    print('Please enter your electricity plan in abbreviation')

提示:您可以为这样的情况创建一个哈希映射

代码语言:javascript
复制
map = {'efir': func_one, 'eflr': func_two}

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

您也可以使用match

票数 1
EN

Stack Overflow用户

发布于 2022-10-07 17:59:25

您可以考虑像这样使用match/case,方法是交换输入顺序--也就是说,先询问使用量,然后再输入计划类型:

代码语言:javascript
复制
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')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73990770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档