Jump to content

I don't know why but the condition is not executed

n=0
dice=[]
for i in range(100):
    b=random.randint(1,6)
    dice.append(b)
print(dice)
for i in dice:
    if dice == 6:
        n=n+1
print(n)

outpu

[1, 2, 1, 1, 3, 6, 4, 1, 1, 6, 1, 5, 4, 4, 6, 6, 3, 4, 3, 3, 3, 6, 3, 6, 5, 6, 2, 2, 5, 3, 5, 4, 2, 2, 6, 5, 4, 4, 2, 4, 4, 4, 6, 1, 6, 5, 1, 3, 5, 1, 1, 3, 5, 2, 5, 2, 4, 1, 5, 1, 2, 1, 2, 6, 2, 2, 6, 6, 1, 2, 5, 5, 5, 1, 1, 1, 3, 4, 5, 5, 5, 2, 6, 5, 6, 5, 2, 3, 1, 1, 4, 6, 5, 5, 1, 4, 2, 6, 2, 2]
0

pls help

I'm running code in jupyter notebook

Link to comment
https://linustechtips.com/topic/1479424-is-there-anything-wrong-with-my-code/
Share on other sites

Link to post
Share on other sites

you are asking if the list 'dice' is equal to 6 not one of the items in the list 'i'

 

here is the conditional thats actually happening

 

if [1, 2, 1, 1, 3, 6, 4, 1] == 6

 

If your question is answered, mark it so.  | It's probably just coil whine, and it is probably just fine |   LTT Movie Club!

Read the docs. If they don't exist, write them. | Professional Thread Derailer

Desktop: i7-8700K, RTX 2080, 16G 3200Mhz, EndeavourOS(host), win10 (VFIO), Fedora(VFIO)

Server: ryzen 9 5900x, GTX 970, 64G 3200Mhz, Unraid.

 

Link to post
Share on other sites

5 minutes ago, pedram_vi said:

I don't know why but the condition is not executed

n=0
dice=[]
for i in range(100):
    b=random.randint(1,6)
    dice.append(b)
print(dice)
for i in dice:
    if dice == 6:
        n=n+1
print(n)

outpu

[1, 2, 1, 1, 3, 6, 4, 1, 1, 6, 1, 5, 4, 4, 6, 6, 3, 4, 3, 3, 3, 6, 3, 6, 5, 6, 2, 2, 5, 3, 5, 4, 2, 2, 6, 5, 4, 4, 2, 4, 4, 4, 6, 1, 6, 5, 1, 3, 5, 1, 1, 3, 5, 2, 5, 2, 4, 1, 5, 1, 2, 1, 2, 6, 2, 2, 6, 6, 1, 2, 5, 5, 5, 1, 1, 1, 3, 4, 5, 5, 5, 2, 6, 5, 6, 5, 2, 3, 1, 1, 4, 6, 5, 5, 1, 4, 2, 6, 2, 2]
0

pls help

I'm running code in jupyter notebook

you're comparing dice instead of i to the value 6...

for i in dice:
    if i == 6:
        n=n+1

 

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

8 minutes ago, _Omega_ said:

Im only familiar with java, but shouldn't it be  "if dice(i) == 6"?

in python the loop syntax is "for x in y:" like a foreach loop.

edit: i didnt finish explaining, im a dummy. the x var becomes the item in the list at the current iterations loop.

If your question is answered, mark it so.  | It's probably just coil whine, and it is probably just fine |   LTT Movie Club!

Read the docs. If they don't exist, write them. | Professional Thread Derailer

Desktop: i7-8700K, RTX 2080, 16G 3200Mhz, EndeavourOS(host), win10 (VFIO), Fedora(VFIO)

Server: ryzen 9 5900x, GTX 970, 64G 3200Mhz, Unraid.

 

Link to post
Share on other sites

6 minutes ago, Takumidesh said:

you are asking if the list 'dice' is equal to 6 not one of the items in the list 'i'

 

here is the conditional thats actually happening

 

if [1, 2, 1, 1, 3, 6, 4, 1] == 6

 

Thanks you solving my problem

Link to post
Share on other sites

  • 4 weeks later...

if dice==6 line will never run because dice is a list, not an int

 

try this code instead:

 

n=0
dice=[]
for i in range(100):
    b=random.randint(1,6)
    dice.append(b)
print(dice)
for i in dice:
    if i == 6:
        n=n+1
print(n)

 

hey! i know to use a computer

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×