引入用户输入作为 def 函数参数
Introduce User Input as def Function Parameter
我正在做一些基础练习,同时向 class 学习。其中一个练习使用了 def 函数,我认为他们提供的示例相当不切实际,所以我想将它们放在一起。
print("Welcome to The Car Wash Company")
print("Which service would you like to purchase?")
def wash_car(service):
if (service == 1):
print("Please see the selected products that will be provided to your vehicle")
print("Wash with tri-color foam")
print("Rinse twice")
print("Dry with large blow dryer")
print("Please set your vehicle to neutral and step-off the gas/break pedal at all times")
if (service == 2):
print("Please see the selected products that will be provided to your vehicle")
print("Wash with white foam")
print("Rinse once")
print("Air dry")
print("Please set your vehicle to neutral and step-off the gas/break pedal at all times")
else:
print("Sorry, We do not have the service selected at this time")
print("Try again to choose your service from the Main Menu")
service = input("For Premium press 1... For Standard press 2... ")
if service ==1:
print("Thank you for choosing our Premium car wash service, Please stand by.")
if service == 2:
print("Thank you for choosing our Standard car wash service, Please stand by.")
wash_car(service=1)
print("Thank you for choosing The Car Wash Company as your service provider!")
print("Have a wonderful rest of your day! Come again soon :)")
我遇到的问题是我不知道如何将 1/2 的用户输入定义为使用 def wash_car 函数正确执行的选择服务。另外,无论输入值如何,我的 else 语句总是触发。
谢谢,
新代码爱好者。
函数中所有代码按顺序运行。所以输入发生在 else 语句已经执行之后。我建议将用户输入分解为一个单独的函数,如下所示:
print("Welcome to The Car Wash Company")
print("Which service would you like to purchase?")
def wash_car(service):
if (service == 1):
print("Please see the selected products that will be provided to your vehicle")
print("Wash with tri-color foam")
print("Rinse twice")
print("Dry with large blow dryer")
print("Please set your vehicle to neutral and step-off the gas/break pedal at all times")
if (service == 2):
print("Please see the selected products that will be provided to your vehicle")
print("Wash with white foam")
print("Rinse once")
print("Air dry")
print("Please set your vehicle to neutral and step-off the gas/break pedal at all times")
else:
raise ValueError
def select_service():
service = input("For Premium press 1... For Standard press 2... ")
while service not in ["1","2"]:
print("Sorry, We do not have the service selected at this time")
print("Try again to choose your service from the Main Menu")
service = input("For Premium press 1... For Standard press 2... ")
return int(service)
service = select_service()
wash_car(service)
print("Thank you for choosing The Car Wash Company as your service provider!")
print("Have a wonderful rest of your day! Come again soon :)")
这还有在用户输入不正确时自动重试的好处。
我正在做一些基础练习,同时向 class 学习。其中一个练习使用了 def 函数,我认为他们提供的示例相当不切实际,所以我想将它们放在一起。
print("Welcome to The Car Wash Company")
print("Which service would you like to purchase?")
def wash_car(service):
if (service == 1):
print("Please see the selected products that will be provided to your vehicle")
print("Wash with tri-color foam")
print("Rinse twice")
print("Dry with large blow dryer")
print("Please set your vehicle to neutral and step-off the gas/break pedal at all times")
if (service == 2):
print("Please see the selected products that will be provided to your vehicle")
print("Wash with white foam")
print("Rinse once")
print("Air dry")
print("Please set your vehicle to neutral and step-off the gas/break pedal at all times")
else:
print("Sorry, We do not have the service selected at this time")
print("Try again to choose your service from the Main Menu")
service = input("For Premium press 1... For Standard press 2... ")
if service ==1:
print("Thank you for choosing our Premium car wash service, Please stand by.")
if service == 2:
print("Thank you for choosing our Standard car wash service, Please stand by.")
wash_car(service=1)
print("Thank you for choosing The Car Wash Company as your service provider!")
print("Have a wonderful rest of your day! Come again soon :)")
我遇到的问题是我不知道如何将 1/2 的用户输入定义为使用 def wash_car 函数正确执行的选择服务。另外,无论输入值如何,我的 else 语句总是触发。
谢谢,
新代码爱好者。
函数中所有代码按顺序运行。所以输入发生在 else 语句已经执行之后。我建议将用户输入分解为一个单独的函数,如下所示:
print("Welcome to The Car Wash Company")
print("Which service would you like to purchase?")
def wash_car(service):
if (service == 1):
print("Please see the selected products that will be provided to your vehicle")
print("Wash with tri-color foam")
print("Rinse twice")
print("Dry with large blow dryer")
print("Please set your vehicle to neutral and step-off the gas/break pedal at all times")
if (service == 2):
print("Please see the selected products that will be provided to your vehicle")
print("Wash with white foam")
print("Rinse once")
print("Air dry")
print("Please set your vehicle to neutral and step-off the gas/break pedal at all times")
else:
raise ValueError
def select_service():
service = input("For Premium press 1... For Standard press 2... ")
while service not in ["1","2"]:
print("Sorry, We do not have the service selected at this time")
print("Try again to choose your service from the Main Menu")
service = input("For Premium press 1... For Standard press 2... ")
return int(service)
service = select_service()
wash_car(service)
print("Thank you for choosing The Car Wash Company as your service provider!")
print("Have a wonderful rest of your day! Come again soon :)")
这还有在用户输入不正确时自动重试的好处。