Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 零一数字_Python_Python 3.x - Fatal编程技术网

Python 零一数字

Python 零一数字,python,python-3.x,Python,Python 3.x,更新:输入的数字大于44时超时。有没有办法阻止它超时? 我的代码应该输出最小的数字,该数字只包含输入分成的1或0。在本例中,34的最小倍数(仅包含1或0)为111010。然而,我的代码只输出一个无限循环……有什么想法吗 #example input: 34 #example ouput: 111010 #2<n<1000 print("Please enter your number...") n = int(input()) counter = 0 mylist = ['2','

更新:输入的数字大于44时超时。有没有办法阻止它超时? 我的代码应该输出最小的数字,该数字只包含输入分成的1或0。在本例中,34的最小倍数(仅包含1或0)为111010。然而,我的代码只输出一个无限循环……有什么想法吗

#example input: 34
#example ouput: 111010
#2<n<1000
print("Please enter your number...")
n = int(input())
counter = 0

mylist = ['2','3','4','5','6','7','8','9']

t = n
check = str(t)
while any(x in check for x in mylist):
    counter += 1
    t = n * counter
    continue
else:
    print(t)

只需将给定的数字添加到总和,直到其字符串仅包含0和1:

# you already solved the input part, so skipping it here
n = 34
summed = n
allowed = {"0","1"}
while set(str(summed)) - allowed:   #  @MadPhysicists suggestion instead of 
                                          #  any(x not in allowed for x in str(summed)):
    summed += n
else:
    print(summed)
输出:

111010

不知道,但我的猜测是,从长远来看,加法要比乘法快。

为什么不是一个无限循环呢?循环中既不更新x,也不更新check或list,因此您的any。。。条件的计算结果总是相同的。你正在测试检查并更新t。@Aran Fey哦……对不起,我是个编码新手!有什么建议可以解决吗@valkiyare-修改您测试的值以使循环结束…我们当然可以为您修复代码,但最好知道这是否只是一个简单的疏忽,或者您是否真心希望循环结束。因为如果你这样做了,我们有很多要解释的…检查数字的更好方法可能是当setstrsummar-允许:谢谢-我的代码对大于34的数字超时,但是:@Valkiyare>34的超时是什么意思?