Python餐厅小费计算器
所以我是一个完全的编码新手,需要一些帮助。我正在尝试为小费计算器创建一个代码,但不太确定我要用它去哪里。我从来没有在课堂上被我的老师教过,所以我会一时兴起。上课是有要求的,但我知道我走错了路。感谢您的帮助。 这就是我到目前为止所做的:Python餐厅小费计算器,python,calculator,python-2.5,Python,Calculator,Python 2.5,所以我是一个完全的编码新手,需要一些帮助。我正在尝试为小费计算器创建一个代码,但不太确定我要用它去哪里。我从来没有在课堂上被我的老师教过,所以我会一时兴起。上课是有要求的,但我知道我走错了路。感谢您的帮助。 这就是我到目前为止所做的: import decimal class Tip: def __init__(self): self.tip=0 def __str__(self): return 'Tip['+str(self.sub_to
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())
每次运行时,我都会得到以下信息:
get_tip() takes exactly 2 arguments (1 given)
正如我所说的,我正在弥补这一点,因为我去和任何帮助表示感谢 首先需要创建一个tip计算器对象。然后,我看到对象可以自己接受输入。我认为下面的代码将解决您的问题(或者至少让您更接近正确答案) 所以试试这个:
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())
首先需要创建一个tip计算器对象。然后,我看到对象可以自己接受输入。我认为下面的代码将解决您的问题(或者至少让您更接近正确答案) 所以试试这个:
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())
首先需要创建一个tip计算器对象。然后,我看到对象可以自己接受输入。我认为下面的代码将解决您的问题(或者至少让您更接近正确答案) 所以试试这个:
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())
首先需要创建一个tip计算器对象。然后,我看到对象可以自己接受输入。我认为下面的代码将解决您的问题(或者至少让您更接近正确答案) 所以试试这个:
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())
您的程序中有一些错误。最明显的,就是创建您看到的文本的那一个,是缺少的带有
get\u tip
调用的括号。您实际上是在打印函数,而不是结果
然而,这本身是不够的。还有很多,下面是它们的列表:
- 您似乎没有OOP的处理能力,对于这个任务来说也不是必需的。你可以把函数带出课堂
- 除了
之外,所有功能都是不必要的get\u tip
- print语句可以像
input(“foo:”)
- 您需要将每个答案保存到不同的变量
- python2中的打印不需要参数
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)
在那里。Mcuh更简单。您的程序中有一些错误。最明显的,就是创建您看到的文本的那一个,是缺少的带有
get\u tip
调用的括号。您实际上是在打印函数,而不是结果
然而,这本身是不够的。还有很多,下面是它们的列表:
- 您似乎没有OOP的处理能力,对于这个任务来说也不是必需的。你可以把函数带出课堂
- 除了
之外,所有功能都是不必要的get\u tip
- print语句可以像
input(“foo:”)
- 您需要将每个答案保存到不同的变量
- python2中的打印不需要参数
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)
在那里。Mcuh更简单。您的程序中有一些错误。最明显的,就是创建您看到的文本的那一个,是缺少的带有
get\u tip
调用的括号。您实际上是在打印函数,而不是结果
然而,这本身是不够的。还有很多,下面是它们的列表:
- 您似乎没有OOP的处理能力,对于这个任务来说也不是必需的。你可以把函数带出课堂
- 除了
之外,所有功能都是不必要的get\u tip
- print语句可以像
input(“foo:”)
- 您需要将每个答案保存到不同的变量
- python2中的打印不需要参数
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)
在那里。Mcuh更简单。您的程序中有一些错误。最明显的,就是创建您看到的文本的那一个,是缺少的带有
get\u tip
调用的括号。您实际上是在打印函数,而不是结果
然而,这本身是不够的。还有很多,下面是它们的列表:
- 您似乎没有OOP的处理能力,对于这个任务来说也不是必需的。你可以把函数带出课堂
- 除了
之外,所有功能都是不必要的get\u tip
- print语句可以像
input(“foo:”)
- 您需要将每个答案保存到不同的变量
- python2中的打印不需要参数
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)
在那里。Mcuh更简单。自从我实际使用Python以来,已经有一段时间了,但从我所看到的情况来看,您实际上希望调用类提示的实例。因此,您可以说
x=Tip()
,而不仅仅是指Tip
输入的使用也有点错误。你打了两次电话给应答
,所以那里没有发生任何事情。您已经有两种方法来读取输入,因此您可以使用它们来获取小计和百分比
这应该更好地发挥作用:
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)
自从我实际使用Python以来,已经有一段时间了,但从我所看到的情况来看,您实际上希望调用类Tip的实例。因此,您可以说
x=Tip()
,而不仅仅是指Tip
输入的使用也有点错误。你打了两次电话给应答
,所以那里没有发生任何事情。您已经有两种方法来读取输入,因此您可以使用它们来获取小计和百分比
这应该更好地发挥作用:
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)
自从我实际使用Python以来,已经有一段时间了,但从我所看到的情况来看,您实际上希望调用类Tip的实例。因此,您可以说
x=Tip()
,而不仅仅是指Tip
t = Tip(0.15)
t.calculate_total(amt_of_bill)
def tip(tip_pct):
def wrapped(bill_amt):
return bill_amt * (1 + tip_pct)
return wrapped