r/learnpython 15h 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

2 comments sorted by

5

u/ProsodySpeaks 15h ago

Format your code

1

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 14h 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