【问题标题】:Introduce User Input as def Function Parameter引入用户输入作为 def 函数参数
【发布时间】:2021-06-12 23:31:54
【问题描述】:

我一边上课一边做一些基础练习。其中一个练习使用了 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/2 的用户输入定义为正确执行的所选服务”的意思?

标签: python python-3.x function syntax


【解决方案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 :)")

这还具有对不正确的用户输入自动重试的好处。

【讨论】:

  • 我复制/粘贴了您的解决方案,它一直在终端上提供相同的解决方案。欢迎来到 The Car Wash Company 您想购买哪种服务?对于高级,请按 1... 对于标准,请按 2... 1 抱歉,我们目前没有选择服务 再次尝试从主菜单中选择您的服务 对于高级,请按 1... 对于标准,请按 2.. . 2 抱歉,我们目前没有选择服务 再次尝试从主菜单中选择您的服务 对于高级版,请按 1... 对于标准版,请按 2...
  • 糟糕,忘记提了。 Input 总是返回一个字符串,它永远不会等于整数
  • @MrWalrusIII 我刚刚所做的编辑现在应该可以解决这个问题
  • 请参阅下面的错误 请将您的车辆设置为空档并始终踩下油门/刹车踏板 回溯(最后一次通话):文件“c:\Users\ess576o\Documents\Learning\ SW-Dev\Ex_Files_Programming_Foundations_Fundamentals\Ex_Files_Programming_Foundations_Fundamentals\Exercise Files\Chap05\05_03_begin.py”,第 32 行,在 wash_car(service) 文件“c:\Users\ess576o\Documents\Learning\SW-Dev\Ex_Files_Programming_Foundations_Fundamentals\Ex_Files_Programming_Foundations练习 Files\Chap05\05_03_begin.py",第 20 行,在wash_car raise ValueError ValueError
  • @MrWalrusIII 这行if (service == 2): 应该是elif (service == 2):。这就是为什么您的 else 块不断被执行的原因。它不是第一个 if 的一部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-24
  • 2020-01-30
  • 1970-01-01
  • 2017-08-13
  • 1970-01-01
相关资源
最近更新 更多