引入用户输入作为 def 函数参数

时间:2021-03-15 17:04:32

标签: python python-3.x function syntax

我一边上课一边做一些基础练习。其中一个练习使用了 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 语句总是会触发。

谢谢,

一个新的代码爱好者。

1 个答案:

答案 0 :(得分:1)

函数中的所有代码都按顺序运行。所以输入发生在 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 :)")

这还具有自动重试错误用户输入的好处。