【问题标题】:Price Fare Program票价计划
【发布时间】:2019-03-07 20:11:22
【问题描述】:

我正在开发一些在线程序来测试我对 Python 的了解并进行改进,但我目前正忙于一项任务,该任务需要我编写一个程序来询问用户是否想了解一个票,如果选择“否”,则程序退出。如果选择,当前有效。如果选择“是”,则程序将继续。然后它会提示用户输入目的地并询问他们是否有资格享受折扣,根据他们的输入,位置的价格会有所不同,以及他们是否有资格享受折扣。

我对为什么我的程序没有在控制台中显示结果有疑问,我认为这与 if/else 语句有关,是否有更好的方法来做到这一点?只是我的语法不正确?

程序向用户显示费用后,我希望它询问用户是否想知道另一张票的价格,我使用了“退出”,但不确定他们是否输入“是” ' 返回程序顶部。

非常感谢任何有关我可以了解更多信息的帮助或提示!

谢谢!

以下是我目前正在处理的代码。

price = input("Do you want to wish to find the price of a ticket? 
(Yes/No): ")
if price == 'No':
  exit()
place = input("Do you want to travel to London or New York?: ")
discountstatus = input("Are you eligible for discount (Yes/No): ")

def ticketPrice(place, discountstatus):
    if place == 'London' & discountstatus == 'Yes':
       print("£170.47")
    else:
       print("£222.40")
    if place == 'New York' & discountstatus == 'Yes':
       print("£350.30")
   else:
       print("£420.82")

anotherticket = input("Do you want to find the price of another 
ticket? (Yes/No): ")
if anotherticket == 'No':
   exit()
ticketPrice(place, discountstatus)

【问题讨论】:

  • 逻辑“与”运算符是and,而不是&
  • 如果你想根据特定的用户输入重复程序逻辑,你可以考虑使用循环。 (例如while循环)。

标签: python


【解决方案1】:

在 Python 中,& 是按位运算 - 逻辑“与”运算符拼写很简单,and

您的代码中还有一些其他错误...大多数是逻辑错误,因此,由于这是一个练习,我将由您自行查找和修复它们,但其中一个是设计错误:您的 @987654323 @function 不应该打印结果而是返回它(让调用者做他想做的任何事情)。这被命名为“关注点分离”——你的“业务域”层(这里是ticketPrice 函数)不应该知道关于 UI 层的任何事情,所以你可以将它与任何 UI 一起使用(或者根本没有 UI——想想 cron工作等)。

【讨论】:

    【解决方案2】:

    我在您的代码中看到的一些问题:

    1. 将您的函数def ticketPrice(place, discountstatus): 放在最顶部。只有当你像ticketPrice(any_place, any_discountstatus) 这样调用它时才会执行它。函数资源:https://www.tutorialspoint.com/python/python_functions.htm

    2. 然后你需要在人进入地点和折扣状态后调用它。 (在那里,你实际定义了函数。用ticketPrice(place, discountstatus)调用它

    3. 在 python 中得到一个“和”你写and 而不是&https://www.tutorialspoint.com/python/python_basic_operators.htm 然后是部分:PythonLogicalOperators

    4. 如果用户将 discountstatus 设置为 No,会发生什么情况。也许您需要嵌套 if 语句。 https://www.tutorialspoint.com/python/nested_if_statements_in_python.htm

    5. 要使此过程无限次成为可能,您可以使用循环。这个循环只要用户想计算就执行,输入No. Resource at loops:https://www.tutorialspoint.com/python/python_loops.htm

    【讨论】:

      【解决方案3】:

      您可以将代码包装在一个方法中并递归调用它。这也将允许您避免调用 exit(),而是让程序自行终止。

      def ask_travel_questions():
        place = input("Do you want to travel to London or New York?: ")
        discountstatus = input("Are you eligible for discount (Yes/No): ")
      
        def ticketPrice(place, discountstatus):
            if place == 'London' & discountstatus == 'Yes':
               print("£170.47")
            else:
               print("£222.40")
            if place == 'New York' & discountstatus == 'Yes':
               print("£350.30")
           else:
               print("£420.82")
      
        anotherticket = input("Do you want to find the price of another 
        ticket? (Yes/No): ")
        if anotherticket=="Yes":
          ask_travel_questions()  # Recursively call for more information  
      
      price = input("Do you want to wish to find the price of a ticket? 
      (Yes/No): ")
      if price == 'Yes':
        ask_travel_questions()
      

      请注意,我没有调试代码或任何东西,只是重构了它。所以可能还是有一些问题。 bruno desthuilliers的cmets绝对可以用来进一步完善sn-p代码。

      【讨论】:

        【解决方案4】:

        以下是代码中的一些错误。

        1) 你想重复地接受用户的输入。所以需要无限次添加while循环,直到用户想要退出。

        2) 在python中,if条件的逻辑运算符AND用作and而不是&

        3) 您需要添加 else-if 条件(python 中的elif)来显示价格,否则第二个条件块的 else 部分也会在用户插入位置 London 的情况下执行,反之亦然。

        4) 您将 place & discountstatus 值作为 ticketPrice() 函数中的输入。所以不需要在函数中传递参数。

        def ticketPrice():
            place = input("Do you want to travel to London or New York?: ")
            discountstatus = input("Are you eligible for discount (Yes/No): ")
            if place == 'London' and discountstatus == 'Yes':
               print("£170.47")
            elif place == 'London' and discountstatus == 'No':
               print("£222.40")
            elif place == 'New York' and discountstatus == 'Yes':
               print("£350.30")
            else:
               print("£420.82")
        
        while(True):
            ticketPrice()
            anotherticket = input("Do you want to find the price of another ticket? (Yes/No): ")
            if anotherticket == 'No':
               exit()
        

        输出:

        【讨论】:

          猜你喜欢
          • 2018-06-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-04-07
          相关资源
          最近更新 更多