我正在上课的作业要求我们制作一个程序,通过重复减法计算整数除法(就像乘法相当于重复加法)。到目前为止,除了用户想要计算商的次数之外,一切都很好。
由于某种原因,程序总是运行一次,而不是我提示程序在for循环中执行的迭代/输入次数。
我可以得到一些帮助吗?我是新功能的,所以我确定程序不是可读的,但我需要弄清楚我的get_num_of_inputs()函数是什么问题和/或num_of_inputs变量。
谢谢! :)
def main():
num_of_inputs = get_num_of_inputs()
numerator, denominator = get_input()
quotient = calc_quotient(num_of_inputs, numerator, denominator)
display_output(numerator, denominator, quotient)
def get_num_of_inputs():
return int(input("What is the number of inputs?: "))
def get_input():
numerator = int(input("\nEnter the numerator: "))
denominator = int(input("Enter the denominator (must not be zero): "))
return numerator, denominator
def calc_quotient(num_of_inputs, numerator, denominator):
if denominator == 0:
return print("Error: invalid input")
if numerator == 0:
return 0
if denominator == 1:
return numerator
if denominator == -1:
return -numerator
numerator_abs = abs(numerator)
denominator_abs = abs(denominator)
for iterations in range(num_of_inputs):
#case 1
if numerator_abs > denominator_abs:
result = 5
acc = numerator_abs
quotient = 0
while result > 0:
acc = acc - denominator_abs
result = acc
quotient += 1
if result < 0:
return quotient - 1
if (numerator > 0 and denominator > 0) or (numerator < 0 and denominator < 0):
return quotient
if numerator_abs % denominator_abs == 0 and ((numerator > 0 and denominator < 0) \
or (numerator < 0 and denominator > 0)):
return -quotient
if numerator_abs % denominator_abs > 0 and ((numerator > 0 and denominator < 0) \
or (numerator < 0 and denominator > 0)):
return -quotient - 1
#case 2
if denominator_abs > numerator_abs:
if (numerator > 0 and denominator > 0) or (numerator < 0 and denominator < 0):
return 0
if (numerator > 0 and denominator < 0) or (numerator < 0 and denominator > 0):
return -1
#case 3
if numerator_abs == denominator_abs:
if (numerator > 0 and denominator > 0) or (numerator < 0 and denominator < 0):
return 1
if (numerator > 0 and denominator < 0) or (numerator < 0 and denominator > 0):
return -1
def display_output(numerator, denominator, quotient):
print("\nThe quotient of", numerator, "and", denominator, "is:", quotient)
main()
答案 0 :(得分:1)
你的逻辑有点纠结。无需将num_of_inputs
传递给calc_quotient
函数。相反,您需要创建循环for
次的num_of_inputs
循环。在循环内部,您将得到一个新的分子和分母,调用calc_quotient
,然后输出结果。
在下面的代码中,我简化了calc_quotient
功能。
def calc_quotient(numerator, denominator):
if denominator == 0:
raise ValueError("denominator can't be zero!")
sign = -1 if numerator * denominator < 0 else 1
numerator = abs(numerator)
denominator = abs(denominator)
quotient = 0
while numerator >= denominator:
numerator -= denominator
quotient += 1
return sign * quotient
def get_num_of_inputs():
return int(input("What is the number of inputs?: "))
def get_input():
numerator = int(input("\nEnter the numerator: "))
denominator = int(input("Enter the denominator (must not be zero): "))
return numerator, denominator
def display_output(numerator, denominator, quotient):
print("\nThe quotient of", numerator, "and", denominator, "is:", quotient)
def main():
num_of_inputs = get_num_of_inputs()
for i in range(num_of_inputs):
numerator, denominator = get_input()
quotient = calc_quotient(numerator, denominator)
display_output(numerator, denominator, quotient)
if __name__ == '__main__':
main()