#π Chess Opening Picker Not Working
29 messages Β· Page 1 of 1 (latest)
@white wadi
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.
import random
whiteopening = ["Vienna", "King's Indian Defense", "London", "Italian"]
blackopening = ["Scandi or King's Indian", "Caro or Slav", "French or Queens Pawn"]
whiterandom = random.randint(0,4)
blackrandom = random.randint(0,3)
if input("White"):
print(whiteopening[whiterandom])
if input("Black"):
print(blackopening[blackrandom])```
for some reason, this just generates "Black" and then "White" instead of waiting for input.
!e
import random
whiteopening = ["Vienna", "King's Indian Defense", "London", "Italian"]
blackopening = ["Scandi or King's Indian", "Caro or Slav", "French or Queens Pawn"]
whiterandom = random.randint(0,4)
blackrandom = random.randint(0,3)
print(whiteopening[whiterandom])
print(blackopening[blackrandom])
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | King's Indian Defense
002 | Caro or Slav
I do intend it to respond to input
so if I end up in a white game, I type "White" into terminal, and it spits out a white one specifically
right, I'm running it locally with input and it's working, except for a different error
what's the error?
that's not how it works. input(x) doesn't check to see if the user enters x. it prints x, and then returns whatever text the user enters.
oh mbmb
can you think of how you can change it to fix this?
lemmie check something really quick
import random
whiteopening = ["Vienna", "King's Indian Defense", "London", "Italian"]
blackopening = ["Scandi or King's Indian", "Caro or Slav", "French or Queens Pawn"]
whiterandom = random.randint(0,4)
blackrandom = random.randint(0,3)
putting = input()
if putting == "White":
print(whiteopening[whiterandom])
if putting == "Black":
print(blackopening[blackrandom])```
this worked!
import random
whiteopening = ["Vienna", "King's Indian Defense", "London", "Italian"]
blackopening = ["Scandi or King's Indian", "Caro or Slav", "French or Queens Pawn"]
whiterandom = random.randint(0,4)
blackrandom = random.randint(0,3)
putting = input("White or Black? Use proper capitalization!\n")
if putting == "White":
print(whiteopening[whiterandom])
if putting == "Black":
print(blackopening[blackrandom])```
fancied it up a little
import random
whiteopening = ["Vienna", "King's Indian Defense", "London", "Italian"]
blackopening = ["Scandi or King's Indian", "Caro or Slav", "French or Queens Pawn"]
while True:
putting = input().strip().lower()
if putting == "white":
openings = whiteopening
elif putting == "black":
opening = blackopening
else:
continue
i = random.randint(0, len(opening))
print(opening[i])
break
This also reprompts if user inputs something different
You're welcome π€
Don't do this
whiterandom = random.randint(0, 4)
instead do this
whiterandom = random.randint(0, len(whiteopening))
So that when you add another opening to the list ..
.. you don't have to remember to update anything else
i = random.randint(0, len(opening))
opening[i]
orrandom.choice(opening)
This help channel has been closed. 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.