gpt4 book ai didi

python - 函数迭代的问题

转载 作者:太空宇宙 更新时间:2023-11-03 16:51:50 24 4
gpt4 key购买 nike

我正在做的类作业要求我们编写一个程序,通过重复减法来计算整数除法(就像乘法相当于重复加法一样)。到目前为止,除了用户想要计算商的次数之外,一切都运行良好。

由于某种原因,程序总是只运行一次,而不是我提示程序在 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()

最佳答案

你的逻辑有点困惑。无需将 num_of_inputs 传递给 calc_quotient 函数。相反,您需要创建一个循环 num_of_inputs 次的 for 循环。在循环内,您将获得新的分子和分母,调用 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()

关于python - 函数迭代的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35789975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com