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

Time:01-17

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.

CodePudding user response:

Answered by Tim Roberts.

requests reads the page as binary bytes, because it doesn't know that it contains words. Do WORDS = response.content.decode().splitlines() to convert it to Unicode. That's probably the root of your guess problem as well, because 'x' is not equal to b'x'

CodePudding user response:

Three things:

  1. What you are seeing with the b'' type string is binary. You have to call the decode() method of the binary to convert to ascii or what-have-you. You can also get the response.text instead of response.content to not have to tangle with binary at all.
  2. Prompt for user input inside the loop. Otherwise you only prompt once and then loop through each letter in your pick.
  3. To see if the guessed a letter correctly you want to see if that letter is in their pick otherwise you are just comparing the user input letter to whichever letter you are on in your iteration, meaning your user would have to spell the word in order to win. So if user in pick_a_list

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].decode('ascii')
pick_as_list=list(pick)
print(pick)
for letter in pick_as_list:
    user=input("Guess a letter: ")
    
    #You may find it helpful to print some variables in this loop to see what's going on. 
    #print(user, pick_as_list)
    if user in pick_as_list:
        print("Right")
    elif user != letter:
        print("False")

Obviously still a lot of spit-and-polish needed to make this a hangman game, but this should get you past the issues are seeing now.

  • Related