def addition(number, number2, mathematic_assignment):
sum_value = number + number2
return f'The sum of {number} {mathematic_assignment} {number2} is {sum_value}'
def subtraction(number, number2, mathematic_assignment):
subtract_value = number - number2
return f'The subtraction of {number} {mathematic_assignment} {number2} is {subtract_value}'
def multiply(number, number2, mathematic_assignment):
multiplied_value = number * number2
return f'The result of multiplying {number} {mathematic_assignment} {number2} is {multiplied_value}'
def division(number, number2, mathematic_assignment):
divided_value = number / number2
return f'The division of {number} {mathematic_assignment} {number2} is {round(divided_value)}'
def run_program():
print("Welcome to the base calculator")
is_running = True
while is_running:
first_number = int(input('Please provide the first number: '))
second_number = int(input('Please provide the second number: '))
mathematic_assignment = input('Please provide mathematical preference: \n+\n-\n*\n/\n')
if mathematic_assignment == '+':
print(addition(first_number, second_number, mathematic_assignment))
elif mathematic_assignment == '-':
print(subtraction(first_number, second_number, mathematic_assignment))
elif mathematic_assignment == '*':
print(multiply(first_number, second_number, mathematic_assignment))
elif mathematic_assignment == '/':
print(division(first_number, second_number, mathematic_assignment))
else:
print('There can be an error in the output, you must only put in "+", "-", "*", "/"')
choice = input("Press 'q' to quit or anything to go on: ")
if choice == 'q':
print('The program will now quit')enter code here
is_running = False
if __name__ == '__main__':
run_program()enter code here
以下代码是由函数组成的基本计算器的示例。然而,我一直在努力解决的问题是启动计算器,其中第一个数字是前一个结果,所以你只能得到第二个数字和所需的数学运算(所以如果 5+5 是 10,那么下次运行代码时(使用 while 循环)现在第一个数字是 10 + {another number})。
如您所见,除了以下挑战之外,一切就绪。我非常确定我可以在没有函数的情况下自己解决这个问题,但是我想知道函数是如何运行的并更详细地学习它们。我真的很感激答案
答案 0 :(得分:1)
首先计算方法应该返回数值,以便您稍后使用它,我删除了参数 ffmpeg -ss 00:06:51 -i truth.mp4 -c:a aac -c:v copy -t 8 true-30sec.mp4
,因为每个方法都知道自己的
mathematic_assignment
然后在主代码中,你可以
def addition(number, number2):
sum_value = number + number2
return sum_value, f'The sum of {number} + {number2} is {sum_value}'
def subtraction(number, number2):
subtract_value = number - number2
return subtract_value, f'The subtraction of {number} - {number2} is {subtract_value}'
def multiply(number, number2):
multiplied_value = number * number2
return multiplied_value, f'The result of multiplying {number} * {number2} is {multiplied_value}'
def division(number, number2):
divided_value = number / number2
return divided_value, f'The division of {number} / {number2} is {round(divided_value)}'
这是一个示例运行
def run_program():
numeric_result = None
print("Welcome to the base calculator")
while True:
result = None
if numeric_result:
first_number = input(
f'Please provide the first number or empty to use the previous result ({numeric_result}):')
first_number = int(first_number) if first_number else numeric_result
else:
first_number = int(input('Please provide the first number: '))
second_number = int(input('Please provide the second number: '))
mathematic_assignment = input('Please provide mathematical preference: + - * / : ')
if mathematic_assignment == '+':
result = addition(first_number, second_number)
elif mathematic_assignment == '-':
result = subtraction(first_number, second_number)
elif mathematic_assignment == '*':
result = multiply(first_number, second_number)
elif mathematic_assignment == '/':
result = division(first_number, second_number)
else:
print('There can be an error in the output, you must only put in "+", "-", "*", "/"')
if result:
numeric_result, str_result = result
print(str_result)
choice = input("Press 'q' to quit or anything to go on: ")
if choice == 'q':
print('The program will now quit')
break