ShashwatRinahit
Want to join the course? Start your journey here:
https://100daysofpython.dev/
41 messages ยท Page 1 of 1 (latest)
Want to join the course? Start your journey here:
https://100daysofpython.dev/
@coral quail
Remember to:
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Can you paste the code here and explain the problem
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]}")
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.
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]}")
list index out of range error is coming
The issue is that your list has 13 entires and you're taking value upto 13 in random.randint()
List indexing starts from 0
So it should be random.randint(0,12)
13 is not excluded ?
!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]}")
:x: Your 3.12 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File "/home/main.py", line 16, in <module>
003 | comp_cards.append(deck[random.randint(0, 13)])
004 | ~~~~^^^^^^^^^^^^^^^^^^^^^^^
005 | IndexError: list index out of range
No, random.randint() dosen't exclude the last value
the range(0, 13) does
Silly confusion I used to struggle with when I started
nw
!e
deck = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
print(len(deck))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
13
yeshh
well its a list. It counts from 0, so you have to -1
Or you could just use choice instead
yes, using choice is way better
!d random.choice
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).
!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))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 3
002 | 4
003 | 3
004 | 5
005 | 11
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.