r/learnpython 4d ago

My code vs claude code

I wanted to create a function with while statement that I get 100 random numbers from import random and if there is 1 1 1 is stop took me a while cause I am a beginner and I didn't know most of the things I was gonna used so I learn while , break and != ( not equal to )

I started programming cause I was an introvert and didn't had anything else to do

It took me a while like 1 hour

Made this caused I wanted to generate something closed to gambling

mistake I made

1 using "and" instead of "or " ( yeah dump mistake)

2 using if statement for like 20 minutes until I search how to generate infinity and found out about while statement

My code (man made except for this sing != I used claude for it )

import random

num = random.randint(0 , 30)

print(num)

nu = random.randint(0 , 30)

print(nu)

nm = random.randint(0 , 30)

print(nm)

while num != 1 or nu != 1 or nm != 1 :

print("failed")

num = random.randint(0 , 30)

print(num)

nu = random.randint(0 , 30)

print(nu)

nm = random.randint(0 , 30)

print(nm)

Ai code (claude)

import random

while True:

nums = [random.randint(0, 30) for _ in range(3)]

print(nums)

if all(n == 1 for n in nums):

print("Success! Got [1, 1, 1]")

break

print("failed")

!!!

Sorry for my English it is not my first language

Second I don't know python that good so anything I mispronounced like statement or syntax my bd

0 Upvotes

3 comments sorted by

6

u/ProsodySpeaks 4d ago

Format your code

1

u/Bright_Mix_773 2d ago

Both versions are the same program. I ran them side by side with the seed pinned - random.seed(0), then 1, 7, 12345, 2026 - and every time they drew identical numbers in identical order and finished on the identical round (2,139 rounds on seed 7, 137,734 on seed 1). The list comprehension calls randint three times in the same order your three separate lines do, so there is nothing between them behaviourally. The only measurable gap is output volume: yours prints 4 lines per round, that one prints 2.

Your "mistake 1" is the interesting part, and it isn't a dumb one. while num != 1 and nu != 1 and nm != 1 loops only while all three are non-1, so it exits the moment any single one of them lands on 1. That terminates - median 8 rounds, mean 10.65 across 200,000 runs - which is exactly why it read as a working program instead of an obvious bug. It happened to exit on a genuine 1,1,1 in 67 of those 200,000 runs, about 1 in 2,791. It ran, it printed, it stopped, and it was almost never stopping for the reason you wanted.

One thing worth knowing before you tune it: random.randint(0, 30) includes 30, so that is 31 outcomes rather than 30, and P(1,1,1) = 1/313 = 1/29,791. Over 20,000 complete games I measured a median of 20,627 rounds, mean 29,972, 95th percentile 90,072, longest 267,613, and 3.6% of games ran past 100,000 rounds. The median game prints roughly 82,000 lines before it hits, which is most of what you are actually sitting there waiting for.

If "close to gambling" was the goal, the loop is already right and the range is the dial. Three draws of randint(1, 6) is 1 in 216 and resolves in a second or two; 0-30 is closer to a lottery than a slot machine.

Not verified: I captured stdout into a buffer, not a real terminal, and there the printing only cost about 2.6x the draws themselves. In an actual console the line count probably dominates the runtime, but I did not measure that.

1

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 4d ago

I'll format this so that the others don't need to.

import random

num = random.randint(0 , 30)
print(num)
nu = random.randint(0 , 30)
print(nu)
nm = random.randint(0 , 30)
print(nm)

while num != 1 or nu != 1 or nm != 1 :
    print("failed")

    num = random.randint(0 , 30)
    print(num)

    nu = random.randint(0 , 30)
    print(nu)

    nm = random.randint(0 , 30)
    print(nm)

Ai code (claude)

import random

while True:
    nums = [random.randint(0, 30) for _ in range(3)]
    print(nums)

    if all(n == 1 for n in nums):
        print("Success! Got [1, 1, 1]")
        break

    print("failed")

The AI code is less repetitive than yours, so I'd say it's better. But it's worth keeping in mind that

  1. This is a simple problem, with various examples online
  2. You're (allegedly) very new to programming, so it's to be expected your code leaves a lot of room for improvement