You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 3:如何判断随机生成的连续15个数均无法被输入数整除?

Alright, let's walk through building this Python 3 code step by step based on your requirements. I'll break it down into clear, actionable parts so you can follow along easily.

Python 3 Implementation: Random List Generation & Divisibility Check

Step 1: Import the Required Module

First, we need the random module to generate our initial random number—no extra installs needed, it's part of Python's standard library:

import random

Step 2: Generate Random Number & Create the Target List

We'll use random.randint() to pick a random integer (feel free to adjust the range to fit your use case). Then, we'll create a list spanning from that random number up to (but not including) the random number + 15—since Python's range is left-closed, right-open. If you want to include random_num + 15 in the list, just change the upper bound to random_num + 16.

# Generate a random integer (example range: 0 to 100; tweak this as needed)
random_num = random.randint(0, 100)
# Create the list: elements from random_num to random_num + 14
num_list = list(range(random_num, random_num + 15))
print(f"Generated list: {num_list}")

Step 3: Build the Divisibility Check Logic

Next, we'll get user input, add safeguards for invalid inputs (like non-numbers or 0, which would cause a division error), then use Python's all() function to check if every element in the list can't be divided evenly by the input value.

Here's the full logic with error handling:

try:
    # Get user input and convert to integer (use float() instead if you need decimal support)
    input_num = int(input("Enter a number: "))
    
    if input_num == 0:
        print("Oops, you can't input 0—division by zero isn't allowed!")
    else:
        # Use all() to verify every element fails the divisibility check
        # The generator expression checks if element % input_num != 0 for each item
        all_not_divisible = all(element % input_num != 0 for element in num_list)
        
        if all_not_divisible:
            print(True)
            # Add your follow-up operations right here
            print("Running subsequent operations...")
        else:
            print(False)
            print("At least one element in the list is divisible by your input.")
except ValueError:
    print("Please enter a valid number!")

Quick Explanation of Key Bits

  • all(): This function is perfect for "all elements meet X condition" checks. It iterates through the generator expression and returns True only if every single element satisfies element % input_num != 0.
  • Error Handling: The try-except block catches cases where the user enters something that isn't a number, and the input_num == 0 check prevents division-by-zero crashes.

Feel free to tweak the random number range, switch to float inputs, or adjust the list bounds to match your exact needs!

内容的提问来源于stack exchange,提问作者Linus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:30:12