首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Collatz回路结构

Collatz回路结构
EN

Stack Overflow用户
提问于 2018-06-30 02:06:15
回答 4查看 1.5K关注 0票数 2

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

Collatz序列编写一个名为collatz()的函数,该函数有一个名为number的参数。如果数字是偶数,那么collatz()应该打印number // 2并返回这个值。如果数字是奇数,那么collatz()应该打印并返回3* number + 1。

然后编写一个程序,让用户输入一个整数,并一直对这个数字调用collatz(),直到函数返回值1。(令人惊讶的是,这个序列实际上适用于任何整数-使用这个序列,您迟早会得到1!)就连数学家也不知道为什么。您的程序正在探索所谓的Collatz序列,有时被称为“最简单的不可能的数学问题”)。

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

提示:整数是偶数%2 == 0,如果是%2 == 1,则为奇数。

代码语言:javascript
复制
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
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-06-30 02:45:21

必须在函数collatz(num)中打印和返回

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2018-06-30 02:30:18

复制- Making a collatz program automate the boring stuff

正如其他人在注释中所指出的,您的函数collatz()必须返回一个整数(也是),才能再次输入collatz()

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

代码语言:javascript
复制
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)

下面是一个输出示例:

代码语言:javascript
复制
>>> collatz_sequence(6)
6
3
10
5
16
8
4
2
1
票数 2
EN

Stack Overflow用户

发布于 2020-06-29 11:30:12

如果你想面对的话,我就是这样写同样的代码的

代码语言:javascript
复制
# 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)

希望对你有帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51111163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档