Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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 if语句中的布尔表达式太多,如何修复?_Python_Loops - Fatal编程技术网

Python if语句中的布尔表达式太多,如何修复?

Python if语句中的布尔表达式太多,如何修复?,python,loops,Python,Loops,我尝试循环并加倍一个数字,直到它得到所有的数字0-9,它工作,但我的if语句有许多布尔表达式,看起来很难看,我尝试使用if('0123456789'在first_num中),但不工作。。。有人知道如何使if语句更漂亮吗 all_numbers = '0123456789' first_num = int(input("Enter a number you want to multiply: ")) second_num = int(input(&quo

我尝试循环并加倍一个数字,直到它得到所有的数字0-9,它工作,但我的if语句有许多布尔表达式,看起来很难看,我尝试使用if('0123456789'在first_num中),但不工作。。。有人知道如何使if语句更漂亮吗

    all_numbers = '0123456789'
    first_num = int(input("Enter a number you want to multiply: "))
    second_num = int(input("Enter how many attempts you want to make: "))
    count = 0

    while(second_num != 0):
        second_num = second_num - 1
        if('0' in str(first_num) and '1' in str(first_num) and \
        '2' in str(first_num) and '3' in str(first_num) and \
        '4' in str(first_num) and '5' in str(first_num) and \
        '6' in str(first_num) and '7' in str(first_num) and \
        '8' in str(first_num) and '9' in str(first_num)):
            break
        first_num = first_num * 2
        count = count + 1

    print(str(count))

用数字的数字做一个
设置
,并进行比较:

all_numbers = set("0123456789")
...
if all_numbers == set(str(first_num)):
    break

与您的问题无关,但是为什么先将输入转换为
int
,然后再转换为
str
?为什么不将输入保持为字符串,然后在需要将值转换为整数时转换为
int
?您可以编写一个示例输入和一个示例输出吗?很好,我会更改它们。我只是通过habitsample输入将int输入:first_num=1和second_num=1000。输出应为68
all_numbers = set("0123456789")
...
if all_numbers == set(str(first_num)):
    break