【问题标题】:Python: I'm making a simple calculator for class. What's wrong with this code?Python:我正在为课堂制作一个简单的计算器。这段代码有什么问题?
【发布时间】:2013-01-30 18:40:42
【问题描述】:

我的老师要求我制作一个计算器,可以计算提交价格的 15% 小费。我遵循了这个 python 应用程序的在线教程。在做老师要求的东西之前,我做了一个简单的加减乘除计算器练习。在 YouTube 视频中,我跟踪了在他的计算机上运行的 python 应用程序。我在 Python Shell 中收到“无效语法”错误。我正在使用 Python 3.3.0。谢谢。

编辑:我按照不同的教程了解了如何完成我的项目。我遇到的唯一问题是同时使用常规整数(1、2、3、4 等)和浮点整数(4.36、5.45 等)。


print("Welcome to the calculator!")

mealPrice = int(input("Enter your meal price:"))

asw = mealPrice * 0.15

print("The tip you owe is: ", asw)

【问题讨论】:

标签: python calculator


【解决方案1】:

因为y是字符串

y = int(raw_input("Input second integer: "))

【讨论】:

  • 他们在问题中说他们正在使用 python 3.3
  • 我已经格式化了你的答案——你可以使用文本区域上方的{}-按钮来做到这一点
  • 似乎发生的事情是,我所遵循的教程可能没有使用 Python 3.x。对于 3.x 语言标准,我写了哪些内容需要更改?
  • 没错,如果您使用的是 python 2.x,您可以直接使用 input 而不是 int(raw_input('...')) .. 但是在 python 3.x 中输入不可用,, 2.x 作业中的输入是评估表达式,您可以阅读更多相关信息
  • @ComplexCode 你的答案是错误的;他问的是 Python 3.x,而不是 2.x。此外,永远不应该在 python 2.x 中使用输入(除非你绝对知道你在做什么)。此外,raw_input 在 3.x 中更改为 input,这意味着 input() 非常可用。
【解决方案2】:

我发现您的代码存在一些问题。

你在第一行使用print会给你带来麻烦,因为print是Python 3中的一个函数。你应该像print("hello")一样调用它。

在第 8 行,你有一个额外的冒号:

calc():

摆脱它。

最后,当你调用calc()时不需要分号

【讨论】:

    【解决方案3】:

    这有很多改变要做。 首先,第一行代码应该是:

    print ("Hello")
    

    那么,raw_input() 应该变成 input()。

    接下来,除了在定义函数时的第二行之外,calc() 之后不应有冒号或分号。

    代码应如下所示。试试看。

    print ("Hello")
    def calc():
        x = int(input("Input first integer: "))
        y = int(input("Input second integer: "))
        type = str.lower(input("(A)dd, (S)ubstract, (M)ultiply, (D)ivide \n"))
        if type != "a" and type != "s" and type != "m" and type != "d":
            print ("Sorry, the command you entered is not valid.")
            calc()
        else:
            if type =="a":
                print ("The result is '" + str(x+y) + "'")
            elif type == "s":
                print ("The result is '" + str(x-y) + "'")
            elif type =="m":
                print ("The result is '" + str(x*y) + "'")
            elif type == "d":
                print ("The result is '" + str(float(x)/float(y)) + "'")
    
            if int(input("Enter 1 if you would like to perform another calculation? \n")) == 1:
                calc()
            else:
                exit()
    calc()
    

    希望这会有所帮助。

    【讨论】:

      【解决方案4】:

      而不是转换为 int(因此无论您的值是什么,例如 2.99 变为 2)转换为 float 应该可以很好地解决问题;即使您使用 2.0*0.15 而不是 2*0.15,小费计算也应该有效。

      这显然只有在您知道可以预期的输入时才有效。如果有人输入任何无效的内容,它将非常失败。

      【讨论】:

        【解决方案5】:

        如果要使用整数:

        mealprice = int(input("Enter your meal price:"))
        

        如果你想使用浮点数:

        mealprice = float(input("Enter your meal price:"))
        

        建议使用浮点数,因为您也可以使用单个数字,例如 1、2、3 等,也可以使用浮点数。

        我还创建了自己的计算器,它有 6 个功能:

        1 = Add  
        2 = Subtract  
        3 = Multiply  
        4 = Divide  
        5 = Round Off  
        6 = Get Remainder  
        

        就是这么简单。
        我还没有使用四舍五入。
        如果你想要那个代码问我我会给你。
        如果您想了解更多信息,我已准备好提供给您。

        【讨论】:

        • 这里是计算器link。它将在30天后过期..
        • 检查指向calculator的新链接。输入密码:password1。复制代码并将其粘贴到您的空闲位置。然后享受。
        【解决方案6】:

        我想通了。我应该使用:

        int(float(input("Enter your meal price:")))
        

        谢谢大家!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-11-20
          • 1970-01-01
          • 1970-01-01
          • 2021-02-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多