lundi 16 janvier 2023

How to pick a string from a list and convert it to a list - Python

What i want to do is make a hang man game, this is the first phase. I want to pick a word from that online list of words.

I print it to verify it's working.

Till the print line every thing works as intended.

After that there is 2 things:

1- The word is printed like this:b'covered'

Why is there a b? and How to remove it?

2- When i guess a letter it always give false even if the letter is in the word, like this:

b'covered'

Guess a letter: o

False

False

False

False

False

False

False

How can I fix that ?

import requests
import random
word_site = "https://www.mit.edu/~ecprice/wordlist.10000"
rand=random.randint(0,10000)
response = requests.get(word_site)
WORDS = response.content.splitlines()
pick=WORDS[rand]
pick_as_list=list(pick)
print(pick)
user=input("Guess a letter: ")
for letter in pick_as_list:
    if user == letter:
        print("Right")
    elif user != letter:
        print("False")

This code works fine with a given a list not one imported from a site:

rand=random.randint(0,2)
word_list = ["aardvark", "baboon", "camel"]
pick=word_list[rand]
pick_as_list=list(pick)
user=input("Guess a letter: ")
for letter in pick_as_list:
    if user == letter:
        print("Right")
    elif user != letter:
        print("False")

I want to make it work but with a huge list of words.




Aucun commentaire:

Enregistrer un commentaire