#๐Ÿ”’ list index error

41 messages ยท Page 1 of 1 (latest)

coral quail
restive blazeBOT
#

@coral quail

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

deft monolith
coral quail
#

ok 1 sec

#

import random
import art

start = input("Do you want to play Black Jack game? Yes or No\n").lower()
if start == "yes":
response = "yes"
while response == "yes":
print = (art.logo)
deck = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
print = ("Welcome to the Black Jack Game!!")

    player_cards = []
    comp_cards = []

    n=0
    while n < 2:
        player_cards.append(deck[random.randint(0, 13)])
        comp_cards.append(deck[random.randint(0, 13)])           
    print = (f"Your cards are {player_cards}")
    print = (f"One of Computer's cards are{comp_cards[0]}")
restive blazeBOT
#

Hey @coral quail!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
deft monolith
# coral quail import random import art start = input("Do you want to play Black Jack game? Ye...
import random
import art

start = input("Do you want to play Black Jack game? Yes or No\n").lower()
if start == "yes":
    response = "yes"
    while response == "yes":
        print = (art.logo)
        deck = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
        print = ("Welcome to the Black Jack Game!!")

        player_cards = []
        comp_cards = []

        n=0
        while n < 2:
            player_cards.append(deck[random.randint(0, 13)])
            comp_cards.append(deck[random.randint(0, 13)])           
        print = (f"Your cards are {player_cards}")
        print = (f"One of Computer's cards are{comp_cards[0]}")
coral quail
deft monolith
#

List indexing starts from 0

#

So it should be random.randint(0,12)

coral quail
#

13 is not excluded ?

uneven tinsel
#

!e

import random

start = "yes"
if start == "yes":
    response = "yes"
    while response == "yes":
        deck = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
        print = ("Welcome to the Black Jack Game!!")

        player_cards = []
        comp_cards = []

        n=0
        while n < 2:
            player_cards.append(deck[random.randint(0, 13)])
            comp_cards.append(deck[random.randint(0, 13)])           
        print = (f"Your cards are {player_cards}")
        print = (f"One of Computer's cards are{comp_cards[0]}")
restive blazeBOT
deft monolith
coral quail
#

ouuuuu

#

hnnn

#

fkkk

#

okok

#

thanks a lot !

deft monolith
#

nw

uneven tinsel
#

!e

deck = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
print(len(deck))
restive blazeBOT
coral quail
#

yeshh

uneven tinsel
#

well its a list. It counts from 0, so you have to -1

coral quail
#

I got confused between range and randint

#

range does not use the last no.

#

T-T

uneven tinsel
#

Or you could just use choice instead

deft monolith
#

yes, using choice is way better

uneven tinsel
#

!d random.choice

restive blazeBOT
#

random.choice(seq)```
Return a random element from the non-empty sequence *seq*. If *seq* is empty, raises [`IndexError`](https://docs.python.org/3/library/exceptions.html#IndexError).
uneven tinsel
#

!e

import random
deck = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
print(random.choice(deck))
print(random.choice(deck))
print(random.choice(deck))
print(random.choice(deck))
print(random.choice(deck))
restive blazeBOT
deft monolith
#
list = ["rock", "paper", "scissor"]
print(random.choice(list))
#

like that

coral quail
#

okokkk

#

this is way better

#

thanks a lot guys !

restive blazeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.