当两个整数之间没有质数时打印文本

时间:2018-09-12 01:01:46

标签: python-3.x

如果输入的整数之间没有质数,我尝试打印单词“数字之间无质数”。例如,如果用户输入8和10,则没有任何质数,因此应打印上面的消息。 但是,我很难在我的代码中放置打印语句:

num_1 = int(input("Enter your first number: "));
num_2 = int(input("Enter your second number: "));
num_3 = 0

if num_2 > num_1:
    num_3 = num_1
    num_1 = num_2
    num_2 = num_3

delim_list = [":","!",","]
blank_list = []
for num in range(num_3, num_1):
    if num > 1:
            for i in range(2,num):
                if (num%i) == 0:
                    break 

            else:
                blank_list.append(str(num) + delim_list[len(blank_list)%3])

blank_list = "".join(blank_list)
print("No Prime between the numbers")
print(blank_list[:-1])

我的输出将打印“数字之间没有素数”,然后打印素数:

Enter your first number:  1
Enter your second number:  10
No Prime between the numbers
2:3!5,7

1 个答案:

答案 0 :(得分:1)

在尝试打印消息之前检查数组是否为空

if not blank_list:
    print("No Prime between the numbers")