#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 (在考虑到用电量和他们在上一个问题中的计划后对他们的输入作出回应),做一个不同的计算。
kwha = int(input('Enter the amount of electricity you used in month ')) 发布于 2022-10-07 17:43:17
只需根据第一个输入进行两种类型的计算,因为您已经将其存储在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发布于 2022-10-07 17:49:30
您可以为不同的用户输入创建不同的函数。但这不是必要的,你可以只是使用,如果你已经做了块!
def func_one():
print('Thank you for choosing EFIR')
# ...
def func_two():
print('Thank you for choosing EFLR')
# ...然后做这样的事情:
if eplan.lower() == 'efir':
func_one()
elif eplan.lower() == 'eflr':
func_two()
else:
print('Please enter your electricity plan in abbreviation')提示:您可以为这样的情况创建一个哈希映射
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!
发布于 2022-10-07 17:59:25
您可以考虑像这样使用match/case,方法是交换输入顺序--也就是说,先询问使用量,然后再输入计划类型:
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')https://stackoverflow.com/questions/73990770
复制相似问题