所以我的代码可以完成我想要的事情,但是我敢肯定,有一种更短的方法可以做到,您可以立即获取与输入相关的价格。我只是不确定该怎么做,here是原始说明!
service1 = 'Oil change'
service2 = 'Tire rotation'
service3 = 'Car wash'
name1 = 'oil change'
name2 = 'tire rotation'
name3 = 'car wash'
price1 = 35
price2 = 19
price3 = 7
auto_service = input('Enter desired auto service:\n')
if auto_service in service1:
print('You entered: {}'.format(service1))
print('Cost of {}: ${}'.format(name1, price1))
elif auto_service in service2:
print('You entered: {}'.format(service2))
print('Cost of {}: ${}'.format(name2, price2))
elif auto_service in service3:
print('You entered: {}'.format(service3))
print('Cost of {}: ${}'.format(name3, price3))
else:
print('You entered: {}'.format(auto_service))
print('Error: Requested service is not recognized')
答案 0 :(得分:2)
是的,使用带有服务价格对的字典。
services = {
'oil change': 35,
'tire rotation': 19,
'car wash': 7,
}
由于这是家庭作业,因此我将让您弄清楚如何实现它,但这是一个片段,至少可以帮助您入门:if auto_service.lower() in services:
答案 1 :(得分:1)
prices = {
'Oil change': 35,
'Tire rotation': 19,
'Car wash': 7,
}
service = input('Enter desired auto service: ')
print(f"You entered: {service}")
try:
print(f"Cost of {service}: ${prices[service]}")
except KeyError:
print(f"Error: requested service is not recognized")
答案 2 :(得分:0)
services = ['Oil change', 'Tire rotation', 'Car wash']
prices = [35, 19, 7]
auto_service = input('Enter desired auto service:\n')
print("Cost:", prices[services.index(auto_service)])
您可以将数据存储在list
中,并使用.index()
从另一个列表中获取相应的值。
注意:如果您提供的值不在列表中,它将抛出ValueError
。我将其保留为练习,以供您解决。
答案 3 :(得分:0)
services = [ {'name': 'oil change', 'price': 35},
{'name': 'tire rotation', 'price': 19},
{'name': 'car wash', 'price': 7 } ]
auto_service = input('Enter desired auto service:\n')
for service in services:
if auto_service.lower() in service['name']:
print('You entered: {}'.format(service['name']))
print('Cost of {}: ${}'.format(service['name'], service['price']))
break
答案 4 :(得分:0)
这是我的方法:-
service = ('Oil change','Tire rotation','Car wash')
name = ('oil change','tire rotation','car wash')
price = (35,19,7)
auto_service = input('Enter desired auto service:\n')
if auto_service in service:
pos=service.index(auto_service)
print('You entered: {}'.format(auto_service))
print('Cost of {}: ${}'.format(name[pos], price[pos]))
else:
print('You entered: {}'.format(auto_service))
print('Error: Requested service is not recognized')
答案 5 :(得分:0)
使用字典-并将数据存储在其中;您不需要每个项目3个变量-您只需一个变量。
Name
答案 6 :(得分:0)
这是我为您解决的问题。此页面上的许多其他回复也不错。
if (calcValue == '1')
{
calc.output.value += '1';
}
else if (calcValue == '2')
{
calc.output.value += '2';
}
else if (calcValue == '3')
{
calc.output.value += '3';
}