【问题标题】:How to store a variable and use it afterwards in a calculator?如何存储变量并随后在计算器中使用它?
【发布时间】:2020-06-23 16:28:22
【问题描述】:

这是我的代码:

cont = "Y"
greeting = print("Hi, welcome to the calculator. What is your calculation?: ")


num1 = float(input("What is the first number you would like to use? "))
op = input("Select operation: (+, -, /, *, **) ")
num2 = float(input("What is the second number you would like to use? "))

if op == '+':
    x = num1 + num2
    print (x)
elif op == '-':
    x = num1 - num2
    print(x)
elif op == '/':
    x = num1 / num2
    print(x)
elif op == '*':
    x = num1 * num2
    print(x)
elif op == '**':
    x = num1 ** num2
    print(x)
else:
    print("error")
    pass


cont = input("Would you like to use another term? (Y/N): ")

while cont == "Y":
   op1 = input("Select operation: (+, -, /, *, **)")
   num = float(input(("Input the next number: ")))
   if op1 == '+':
        y = x + num
        print (y)
        cont = input("Would you like to continue? (Y/N): ")
   elif op1 == '-':
        z = x - num
        print(z)
        cont = input("Would you like to continue? (Y/N): ")
   elif op1 == '/':
        div = x / num
        print(div)
        cont = input("Would you like to continue? (Y/N): ")
   elif op1 == '*':
        mult = x * num
        print(mult)
        cont = input("Would you like to continue? (Y/N): ")
   elif op1 == '**':
        car = (x ** num)
        print(car)
        cont = input("Would you like to continue? (Y/N): ")


   else:
        print("error")
        pass
    
else:
       print("Finished")

基本上,当添加 num1 和 num2 时,它会再次起作用,因为它使用 x 和新的数字(num)以及新的运算符。但是,继续 AGAIN 时会出现问题,因为程序使用 x 而不是新数字 (y/z/div/mult/car)。

对不起,如果这措辞不好(我是新人),但基本上我希望它在选择 continue 两次后使用新变量,但我不知道该怎么做。

【问题讨论】:

  • 在循环外创建一个变量来跟踪最后的结果
  • 您是否尝试将每个操作的结果保存回x?像x = x * num 这样的东西使用旧的x 计算产品,然后将结果保存在x
  • 为什么不将新结果存回 X 中?为什么要将它存储到不同的变量名中以进行不同的操作?
  • 您还应该考虑 DRY 原则。每当您看到自己一次又一次地编写相同的代码时,可能会有更好的方法来编写它。
  • @MirrorMaster 有任何答案回答了您的问题吗?

标签: python python-3.x variables input calculator


【解决方案1】:

您可以重复使用相同的变量。不用写 y/z/div/mult/car = ...,你可以用 x = ...替换所有的。

【讨论】:

    【解决方案2】:

    您可以使用列表来存储数据

    some_data = 5
    some string = "hello"
    my_list = [] # create empty list that can store any data type (int, str, float...)
    
    my_list.append(some_data) # my_list = [5]
    my_list.append(some_string) # my list = [5, "hello"]
    
    # to access the element within the list you can use the subscript operator '[]'
    # for example my_list[0] returns 5, my list[1] returns "hello"
    # my_list[:] returns the entire list
    

    【讨论】:

      【解决方案3】:

      您应该将最后一个答案始终指定为 x,这样每次您重复它时它都可以工作。例如:

      while cont == "Y":
         op1 = input("Select operation: (+, -, /, *, **)")
         num = float(input(("Input the next number: ")))
         if op1 == '+':
              x += num  # Re-assign to x
              print (x)
              cont = input("Would you like to continue? (Y/N): ")
         elif op1 == '-':
              x -= num  # re-assign to x
              print (x)
              cont = input("Would you like to continue? (Y/N): ")
         elif op1 == '/':
              x /= num  # Re-assign x
              print (x)
              cont = input("Would you like to continue? (Y/N): ")
         elif op1 == '*':
              x += num  # Re-assign x
              print (x)
              cont = input("Would you like to continue? (Y/N): ")
         elif op1 == '**':
              x **= num  # Re-assign x
              print (x)
              cont = input("Would you like to continue? (Y/N): ")
      
      
         else:
              print("error")
              pass
          
      else:
             print("Finished")
      

      现在这个程序可以永远工作,因为最后一个答案总是重新分配给 x。

      如果您不确定我用来执行操作的代码行,例如:

      x += num  # Same as x = x + num
      
      x **= num  # Same as x = x**num
      

      它只是让你的代码更简洁。

      结论:通过始终将变量“x”分配给先前计算的答案,我们可以无限重复对它的操作,因为它始终被存储,然后重新分配以适应程序的下一次迭代。

      【讨论】:

        猜你喜欢
        • 2016-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-14
        • 2019-08-27
        • 1970-01-01
        • 1970-01-01
        • 2019-02-09
        相关资源
        最近更新 更多