简体   繁体   English

Python餐厅提示计算器

[英]Python Restaurant Tip Calculator

So I am a complete newbie to coding and need some help. 所以我是编码的新手,需要一些帮助。 I am trying to create a code for a tip calculator but am not quite sure where I am going with it. 我正在尝试为小费计算器创建代码,但不确定如何处理它。 I was never in class to be taught by my teacher so I am going out on a whim with this. 我从来没有上过老师的课,所以我对此一时兴起。 There is a requirement to have a class but I know I am going about it the wrong way. 有一个课程的要求,但是我知道我会以错误的方式进行学习。 Any help is appreciated. 任何帮助表示赞赏。 This is what I have so far: 这是我到目前为止的内容:

import decimal

class Tip:

    def __init__(self):
        self.tip=0

    def __str__(self):
        return 'Tip['+str(self.sub_total)+' * '+str(self.percentage)+']'

    def sub_total():
        self.sub_total = input()

    def percentage():
        self.percentage = input()

    def get_tip(percentage, sub_total):
        self.percentage = decimal.Decimal(percentage)
        self.sub_total = decimal.Decimal(sub_total)
        self.tip = ((sub_total * percentage) / 100)
        self.total = (sub_total + tip)
        return self.__str__()

t = Tip()

print ("How much is the bill?")
t.sub_total()

print ("What percentage should the tip be?")
t.percentage()

print (t.get_tip())

Everytime I run it I get this: 每次运行它,我都会得到:

get_tip() takes exactly 2 arguments (1 given)

Like I said, I am kindof making it up as I go and any help is appreciated. 就像我说的那样,我随时随地都在进行弥补,感谢您的帮助。

Hoo boy, there's a lot of things to cover. 哎呀,有很多事情要讲。 I'll try to get it all, but will start with working code. 我会尽力而为,但将从工作代码开始。

class Bill(object):  # note: not Tip!
    def __init__(self, sub_total):
        self.sub_total = sub_total

    def calculate_tip(self, tip_pct):
        return self.sub_total * tip_pct

    def calculate_total(self, tip_pct):
        return self.sub_total * (1 + tip_pct)

This defines a Bill object. 这定义了一个Bill对象。 If we want to use it, we could do: 如果要使用它,我们可以这样做:

subtotal = float(raw_input("Bill subtotal: "))
b = Bill(subtotal)

Then to get the tip we could do 然后获得提示,我们可以做

tip_percentage = float(raw_input("Tip Percent (15% --> 0.15): "))
tip_amt = b.calculate_tip(tip_percentage)

And to get the grand total: 并获得总计:

grand_total = b.calculate_total(tip_percentage)

Here's where you went wrong: 这是您出错的地方:

Your original class, for reference, looks like: 供您参考的原始课程如下:

class Tip:

    def __init__(self,sub_total,percentage):
        self.tip= (sub_total * percentage)

    def __str__(self):
        return 'Tip['+str(sub_total)+' * '+str(percentage)+']'

    def sub_total():
        sub_total = input()

    def percentage():
        percentage = input()

    def get_tip(percentage, sub_total):
        percentage = decimal.Decimal(percentage)
        sub_total = decimal.Decimal(sub_total)
        tip = ((sub_total * percentage) / 100)
        total = (sub_total + tip)
        return Tip(total)

We'll talk about it method by method. 我们将逐个方法讨论它。

  1. __init__

This one is great! 这个很棒! It means you create a Tip object by passing the sub_total and the percentage (as a ratio to 1) in the constructor, so t = Tip(15.00, 0.15) is a 15% tip on $15. 这意味着您通过在构造函数中传递sub_total和百分比(与1的比率)来创建Tip对象,因此t = Tip(15.00, 0.15)是15美元的15%小费。 Hooray! 万岁!

  1. __str__

Only briefly going to go into this one, other than to mention that this looks more like a __repr__ than a __str__ and you should use ( / ) instead of [ / ] . 只是简要地介绍了这一点,除了提到它看起来更像是__repr__不是__str__ ,您应该使用( / )而不是[ / ] Remember that sub_total and percentage aren't attributes of the object -- they're just arguments that were passed to its constructor. 请记住,小sub_totalpercentage不是对象的属性,它们只是传递给其构造函数的参数。 You can't reference them here if you don't save them in the __init__ method. 如果未将它们保存在__init__方法中,则不能在此处引用它们。

  1. sub_total

First of all, you can't call this since it doesn't have a self argument. 首先,由于没有self变量,因此无法调用它。 Second of all it doesn't do anything even if you could. 第二,即使可以,它也不会做任何事情。 sub_total = input() gets user input, then throws it away. sub_total = input()获取用户输入,然后将其丢弃。

  1. percentage

See above 往上看

  1. get_tip

You're missing a self argument, again, but I'll talk about it as if it was get_tip(self, percentage, sub_total) . 再次缺少您一个self变量,但是我将把它当作get_tip(self, percentage, sub_total) You call this by using your already-created Tip object (remember above when we did t = Tip(15, 0.15) ?) and calling it with the arguments that you probably passed the constructor. 您可以通过使用已经创建的Tip对象(记得上面我们做t = Tip(15, 0.15)吗?)并使用可能已传递给构造函数的参数来调用它。 In other words: 换一种说法:

t = Tip(15, 0.15)  # 15% tip on $15
t.get_tip(0.15, 15)  # ...15% tip on $15, but the grand total...

It doesn't make a whole lot of sense as a function. 作为一个函数,它没有太多意义。 Especially since half your work is already done for you and saved in self.tip . 特别是因为一半的工作已经为您完成,并保存在self.tip If this is the pattern you wanted to use for your object, you could do something like: 如果这是您要用于对象的模式,则可以执行以下操作:

class Tip(object):
    def __init__(self, sub_total, tip_pct):
        self.sub_total = sub_total
        self.tip_pct = tip_pct
        # saving these as attributes so we can use them later
        self.grand_total = self.sub_total * (1 + self.tip_pct)
        self.tip_amt = self.sub_total * self.tip_pct

But that really looks more like a series of functions to me, than something that needs to be saved as an object! 但这对我来说实际上更像是一系列功能,而不是需要保存为对象的东西!

Nota Bene Nota Bene

Remember that classes are here to provide a state for your code to run in. In this case, the only state you can really apply is "How much to tip," so I suppose you could do the reverse of this and do something like: 请记住,类是在这里为您的代码提供运行状态。在这种情况下,您真正​​可以应用的唯一状态是“要付多少钱”,因此我想您可以执行相反的操作,例如:

t = Tip(0.15)
t.calculate_total(amt_of_bill)

But it seems silly to me. 但这对我来说似乎很愚蠢。 You could maybe make a factory function that does something like: 您也许可以使工厂函数执行以下操作:

def tip(tip_pct):
    def wrapped(bill_amt):
        return bill_amt * (1 + tip_pct)
    return wrapped

But that's a little advanced for where you're at. 但这对于您所处的位置来说有点先进。

You need to create a tip calculator object first. 您需要首先创建一个小费计算器对象。 Then, I see that the object could take the input by themselves. 然后,我看到对象可以自己接受输入。 I think the following code will fix your problem (or at least get you closer to the correct answer). 我认为以下代码可以解决您的问题(或至少可以使您更接近正确的答案)。

So try this: 所以试试这个:

class Tip:

    def __init__(self):
        self.tip=0

    def __str__(self):
        return 'Tip['+str(self.sub_total)+' * '+str(self.percentage)+']'

    def sub_total():
        self.sub_total = input()

    def percentage():
        self.percentage = input()

    def get_tip(percentage, sub_total):
        self.percentage = decimal.Decimal(percentage)
        self.sub_total = decimal.Decimal(sub_total)
        self.tip = ((sub_total * percentage) / 100)
        self.total = (sub_total + tip)
        return self.__str__()

t = Tip()

print ("How much is the bill?")
t.sub_total()

print ("What percentage should the tip be?")
t.percentage()

print (t.get_tip())

There are a few errors in your program. 您的程序中有一些错误。 The most blatent, the one creating the text you see, is the missing parenthesis with the get_tip call. 最简单的方法是创建您看到的文本,这是缺少带有get_tip调用的括号的。 You are literally printing the function , not the result. 您实际上是在打印函数 ,而不是结果。

However, this won't be enough by itself. 但是,这本身还不够。 There are a bunch more and here's a list of them: 还有很多,下面是它们的列表:

  • You don't seem to have an handle of OOP, and it is not really necessary for this assignment. 您似乎没有处理OOP的方法,并且此分配实际上并没有必要。 You can take the functions out of the class. 您可以将功能从类中删除。
  • All the functions are unnecessary, except for get_tip . 除了get_tip之外,所有功能都是不必要的。
  • The print statements can go inside the inputs as in input("foo: ") 打印语句可以像input("foo: ")那样放在input("foo: ")内部
  • You need to save each answer to a different variable. 您需要将每个答案保存到不同的变量。
  • No parens needed with print in python2 不需要在python2中使用print进行parens

Here is my take on the problem: 这是我对这个问题的看法:

def get_tip(percentage, sub_total):
    tip = ((sub_total * percentage) / 100)
    total = (sub_total + tip)
    return total

sub_total = input("How much is the bill?")

percentage = input("What percentage should the tip be?")

print get_tip(percentage, sub_total)

There. 那里。 Mcuh simpler. Mcuh更简单。

It's been a bit since I've actually used Python, but from what I can see you'd want to actually call for an instance of the class Tip instead. 自从我实际使用Python以来已有一段时间,但是从我的眼中可以看出,您实际上是想调用Tip类的实例。 So instead of just referring to Tip, you say x=Tip() instead. 因此,您不只是参考Tip,而是说x=Tip()

The use of input is a little wrong too. 输入的使用也有点错误。 You're calling to answer twice so nothing is really happening there. 您打来电话要answer两次,所以实际上什么也没有发生。 You already have two methods to read input, so you'd use those to get the sub total and percentage instead. 您已经有两种方法来读取输入,因此您将使用这些方法来获取小计和百分比。

This should work better: 这应该更好地工作:

import decimal

class Tip:

    def __init__(self,sub_total,percentage):
        self.tip= (sub_total * percentage)

    def __str__(self):
        return 'Tip['+str(sub_total)+' * '+str(percentage)+']'

    def sub_total():
        sub_total = input()

    def percentage():
        percentage = input()

    def get_tip(percentage, sub_total):
        percentage = decimal.Decimal(percentage)
        sub_total = decimal.Decimal(sub_total)
        tip = ((sub_total * percentage) / 100)
        total = (sub_total + tip)
        return Tip(total)

    t = Tip()

    print ("How much is the bill?")
    t.sub_total()

    print ("What percentage should the tip be?")
    t.percentage()

    print (t.get_tip)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM