Collat​​z循环结构

时间:2018-06-30 02:06:16

标签: python python-3.x collatz

请帮助我了解我做错了什么。问题是

  

Collat​​z序列   编写一个名为collat​​z()的函数,该函数具有一个名为number的参数。如果number是偶数,则collat​​z()应该打印数字// 2并返回该值。如果数字为奇数,则应该打印collat​​z()并返回3 *数字+ 1。

然后编写一个程序,让用户键入一个整数,并继续对该数字调用collat​​z(),直到该函数返回值1为止。(令人惊讶的是,此序列实际上适用于任何整数-迟早使用这个序列,您将得出1!即使是数学家也不知道为什么。您的程序正在探索所谓的Collat​​z序列,有时称为“最简单不可能的数学问题”。)

记住要使用int()函数将输入()的返回值转换为整数;否则,它将是一个字符串值。

提示:即使数字%2 == 0,整数也是偶数,如果数字%2 == 1,则整数是奇数。

def collatz(number):
    if number%2==0:
        print(number//2)
    else:
        print(3*number+1)

x = int(input('print num'))
while TRUE:
    if collatz(x)!=1:
        break

4 个答案:

答案 0 :(得分:2)

重复-Making a collatz program automate the boring stuff

正如其他人在评论中指出的那样,您的函数collatz()必须返回整数并且,才能再次输入collatz()

以您已经完成的工作为基础,我们可以让函数collatz_sequence(x)重复调用collatz()以得到所需的结果:

def collatz(x):
    if x % 2 == 0:
        a = x//2
    else:
        a = 3*x+1
    print(a)
    return a

def collatz_sequence(x):
    print(x)
    while x!=1:
        x=collatz(x)

以下是示例输出:

>>> collatz_sequence(6)
6
3
10
5
16
8
4
2
1

答案 1 :(得分:2)

您必须打印并返回函数collatz(num)

def collatz(number):
    """prints and returns the next number in the Collatz sequence
    """
    if number % 2 == 0:
        next_collatz_number = number // 2
    else:
        next_collatz_number = 3 * number + 1
    print(next_collatz_number)
    return next_collatz_number

x = int(input('print num'))

while True:
    x = collatz(x)
    if x == 1:
        break

答案 2 :(得分:0)

如果您想面对,这就是我编写相同代码的方式

# Write a function named collatz() that has one parameter named number. If
# number is even, then collatz() should print number // 2 and return this value.
# If number is odd, then collatz() should print and return 3 * number + 1


# Then write a program that lets the user type in an integer and that keeps
# calling collatz() on that number until the function returns the value 1.


# Add try and except statements to the previous project to detect whether the
# user types in a noninteger string. Normally, the int() function will raise a
# ValueError error if it is passed a noninteger string, as in int('puppy'). In the
# except clause, print a message to the user saying they must enter an integer.

integernuber = False
while not integernuber:

    try:
        number = int(input('Type a number: '))

        integernumber = True

    except:
        print('Please entere an integer number')

        def collatz():

            global number

            if (number % 2) == 0:
                return number / 2
                number = (number / 2)
            elif (number % 2) == 1:
                return 3 * number + 1
                number = (3 * number + 1)
            else:
                return

        while collatz() <= 1:
            number = int(collatz())
            print(number)

希望对您有帮助

答案 3 :(得分:0)

'''

make[3]: *** No rule to make target '../../../lib/linux-x86_64/libfort.a', needed by 'query'.  Stop.

'''

在此代码中,我匹配了它们的约束(整数约束)并返回了值。我在try块中使用了简单的递归来获取所需的输出。