我正在学习如何编码,我想知道如何获得一个总计。我有代码来做基本的计算器功能,现在想学习如何让计算器能够同时用前面的答案做出多个问题(5 + 5 = 10 - 5 = 5 * 5 = 25/5 )= 5.我希望用户能够像他或她想要的那样对之前的结果进行尽可能多的问题,但也可以随时退出。我正在使用python,并且真的希望如果有人可以帮助/指出我正确的直接我需要编码以实现这一点。我不是在寻求答案,只是一些帮助。谢谢。
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
print "Lets Calculate"
print ("Calculator: ")
print ("1: +")
print ("2: -")
print ("3: *")
print ("4: /")
print ("5: done")
foo1 = input("math solution: ")
num1 = float(raw_input("number 1: "))
num2 = float(raw_input("number 2: "))
if foo1 == 1:
print "{0} + {1} = {2}".format(num1, num2, add(num1, num2))
elif foo1 == 2:
print "{0} - {1} = {2}".format(num1, num2, subtract(num1, num2))
elif foo1 == 3:
print "{0} * {1} = {2}".format(num1, num2, multiply(num1, num2))
elif foo1 == 4:
print "{0} / {1} = {2}".format(num1, num2, divide(num1, num2))
elif foo1 == 5:
print ("done")
结果:
Lets Calculate
Calculator:
1: +
2: -
3: *
4: /
5: done
math solution: 1
number 1: 2
number 2: 2
2.0 + 2.0 = 4.0
我想在完成之前选择接受答案并使用其他数学解决方案。先感谢您。
这是我得到的。香港专业教育学院尝试了一百万种不同的东西,无法弄明白。
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
print "Lets Calculate"
print ("Calculator: ")
print ("1: +")
print ("2: -")
print ("3: *")
print ("4: /")
print ("5: done")
foo1 = input("math solution: ")
rec1 = 0
num1 = float(raw_input("number 1: "))
while foo1 > 0 and foo1 < 6:
num2 = float(raw_input("number 2: "))
if foo1 == 1:
rec1 = "{0} + {1} = {2}".format(num1, num2, add(num1, num2))
elif foo1 == 2:
rec1 = "{0} - {1} = {2}".format(num1, num2, subtract(num1, num2))
elif foo1 == 3:
rec1 = "{0} * {1} = {2}".format(num1, num2, multiply(num1, num2))
elif foo1 == 4:
rec1 = "{0} / {1} = {2}".format(num1, num2, divide(num1, num2))
elif foo1 == 6:
rec1 = "Done"
print rec1
foo2 = input("math solution: ")
print "done"
我添加了num1 = rec1,每次我尝试继续下一个问题时都会弹出一条错误消息。我编辑了代码,以便您可以看到它现在的样子。
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
print "Lets Calculate"
print ("Calculator: ")
print ("1: +")
print ("2: -")
print ("3: *")
print ("4: /")
print ("5: done")
foo1 = input("math solution: ")
rec1 = 0
num1 = float(raw_input("number 1: "))
while foo1 > 0 and foo1 < 6:
num2 = float(raw_input("number 2: "))
if foo1 == 1:
rec1 = "{0} + {1} = {2}".format(num1, num2, add(num1, num2))
elif foo1 == 2:
rec1 = "{0} - {1} = {2}".format(num1, num2, subtract(num1, num2))
elif foo1 == 3:
rec1 = "{0} * {1} = {2}".format(num1, num2, multiply(num1, num2))
elif foo1 == 4:
rec1 = "{0} / {1} = {2}".format(num1, num2, divide(num1, num2))
elif foo1 == 6:
rec1 = "Done"
print rec1
num1 = rec1
foo1 = input("math solution: ")
print "done"
答案 0 :(得分:0)
要继续,直到用户选择退出,您可以使用循环,例如:
foo1 = input("math solution: ")
while foo1 > 0 and foo1 < 5:
num1 = float(raw_input("number 1: "))
num2 = float(raw_input("number 2: "))
....
# Ask for the next operation
foo1 = input("math solution: ")
print "done"
第二点,您想要使用之前的结果。你可以做的是将它存储到变量中,然后打印它,而不是直接打印它。
类似的东西:
ans = 0
if foo1 == 1:
ans = ...
elif foo1 == 2:
ans = ...
print ans
最后,为了允许用户,我们可以使用一个小技巧。例如,如果用户在输入中写入'ans',则它将是最后的结果。
为此,我们可以在将输入字符串转换为float之前处理输入字符串:
num1 = raw_input("number 1: ")
if num1 == 'ans':
num1 = ans
else:
num1 = float(num1)
我还没有写完整段代码让你玩这个并弄清楚如何把所有东西放在一起。但是,如果你需要它告诉我! :)
如果您总是想使用前一个,我们可以做的是在循环之前初始化一个输入:
num1 = float(raw_input("number 1: "))
while foo1 > 0 and foo1 < 5:
num2 = float(raw_input("number 2: "))
....
然后,最后:
# Store the result
num1 = ans
# Ask for the next operation
foo1 = input("math solution: ")
print "done"
您忘了实施此块的一部分:
ans = 0
if foo1 == 1:
ans = ...
elif foo1 == 2:
ans = ...
print ans
当然,如果您将结果存储为输出字符串,则无法使用它。
正确的版本可能是:
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
print "Lets Calculate"
print ("Calculator: ")
print ("1: +")
print ("2: -")
print ("3: *")
print ("4: /")
print ("5: done")
result = 0
# Ask for the operation and for the first number
operation = input("math solution: ")
num1 = float(raw_input("number 1: "))
# Continue until the user chooses to exit
while 0 < operation < 5:
# Ask for the second number
num2 = float(raw_input("number 2: "))
# Operate
if operation == 1:
result = add(num1, num2)
print "{0} + {1} = {2}".format(num1, num2, result)
elif operation == 2:
result = subtract(num1, num2)
print "{0} - {1} = {2}".format(num1, num2, result)
elif operation == 3:
result = multiply(num1, num2)
print "{0} * {1} = {2}".format(num1, num2, result)
elif operation == 4:
result = divide(num1, num2)
print "{0} / {1} = {2}".format(num1, num2, result)
# Store the result as the first number
num1 = result
# Ask for the next operation
operation = input("math solution: ")
# Finish
print "Done"
跟踪:
Lets Calculate
Calculator:
1: +
2: -
3: *
4: /
5: done
math solution: 1
number 1: 10
number 2: 5
10.0 + 5.0 = 15.0
math solution: 2
number 2: 7
15.0 - 7.0 = 8.0
math solution: 3
number 2: 8
8.0 * 8.0 = 64.0
math solution: 4
number 2: 4
64.0 / 4.0 = 16.0
math solution: 5
Done