简体   繁体   English

我如何将 input() 函数放入 python 3 中的 def 函数中

[英]how can i put input() function into a def fonction in python 3

I am working on a collatz sequence that i have found in a book and i want to input the number but it dosen t work , when i put the number the enter keybord key does not work , have i made something wrong in my program ?我正在研究我在书中找到的 collat​​z 序列,我想输入数字但它不起作用,当我输入数字时,输入键盘键不起作用,我的程序有问题吗? :

even = range(0,10**5,2)
odd = range(1,10**5,2)

def collatz_s(num):
    while num !=1 :
        if num in even :
            result = num /2
            print(result)
            num = result

        elif num is odd :
            result = num *3+1
            num = result
            print(result)


num = int(input('choose a random number'))
collatz_s(num)

To check whether a number is odd or even, rather than generating range objects and checking whether a number is in them, look at the remainder in the division by two, which you can get with num % 2 .要检查一个数字是否是奇数还是偶数,而不是生成range对象和检查号码是否是in由两个,你可以得到他们,看在余数num % 2 If the remained is 1, it means the number is even.如果余数为1,则表示该数为偶数。 Otherwise, it's odd.否则,这很奇怪。

Also, use num // 2 to perform an integer division.此外,使用num // 2执行整数除法。 That way dividing 16 by 2 will give you 8 (the integer number), instead of 8.0 (the floating point number.) It turns out this is really important here, especially since you're interested in finding the remainder of the division (a concept mainly concerning integer numbers.)这样,将 16 除以 2 将得到 8(整数),而不是 8.0(浮点数)。事实证明这在这里非常重要,特别是因为您有兴趣找到除法的余数(a概念主要涉及整数。)

I believe it's quite possible that a rounding error in the floating point division might have put your program into an infinite loop (since a number with a tiny decimal part would not be in either even or odd range.) Or if you were surpassing the limit of your ranges (10 5 ) you'd be in the same situation where neither branch matches.我相信浮点除法中的舍入错误很可能使您的程序进入无限循环(因为小数部分很小的数字不会在evenodd范围内。)或者如果您超过了限制在您的范围 (10 5 ) 中,您将处于两个分支都不匹配的相同情况。 Using integer division and checking for even or odd using the remainder should fix both issues.使用整数除法并使用余数检查偶数或奇数应该可以解决这两个问题。

def collatz_s(num):
    while num != 1:
        if num % 2 == 0:
            num = num // 2
            print(num)
        else:
            num = num * 3 + 1
            print(num)

The problem is within your elif block, here elif num is odd : change is to in when dealing with remainder use integer division(//) instead to avoid precision error .问题出在您的elif块中,这里elif num is odd : in处理余数时更改is in使用integer division(//)来避免precision错误。

def collatz_s(num):
    while num != 1:
        if num in even:
            result = num // 2
            print(result)
            num = result

        elif num in odd:
            result = num * 3 + 1
            print(result)
            num = result



num = int(input('choose a random number'))
collatz_s(num) 
# input 5
# output 

16
8
4
2
1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM