How do I compare user input to an uppercase letter string in a list?

Nick

First of all, please pardon my English. It's my second language.

I'm trying to complete writing a hangman game.

Here's my rough code:

WORDS_LIST = ["I do not know"]

answer = list(random.choice(WORDS_LIST))

for char in answer:
    if (char.isalpha()):
        list1.append(char)
    else:
        list1.append("")

for char in list1:
    if char == "":
        guess_box.append(" ")
    else:
        guess_box.append("_")

print(' '.join(guess))

player_choice = input("\nguess a letter: ")

for item in range(0, len(ANSWER)):
    if ANSWER[item] == player_choice:
        guess_box[item] = player_choice
    elif ANSWER[item][0] == player_choice[0]:
        guess_box[item] = player_choice.upper()

As you can see this print out the pattern

_  _ _  _ _ _  _ _ _ _

I have a problem with the first capital letter. I'm not sure how should I compare user input with a single capital letter in a list. Please help me figure this out, anyone?

Example Output:

I do not know
Error - Syntactical Remorse

As the comments send you can use char.lower() but I saw some other things that could help you so here are some more tips and tidbits.

First you can create your lists using list comprehension as it is shorter and fairly easy to read.

import random

WORDS_LIST = ["I do not know"]

# A list of characters
answer = list(random.choice(WORDS_LIST))

# A set of all the letters (converted to lowercase) that are contained 
# in the answer. sets are significantly faster to search through.
letters = {char.lower() for char in answer if char.isalpha()}
guessed_letters = set()

# Note the syntax used above and here. This is called list
# comprehension.
# This is the same as:
# guess_box = []
# for char in answer:
#     if char.isalpha():
#         guess_box.append("_")
#     else:
#         guess_box.append(char) # Just use the space.
guess_box = ["_" if char.isalpha() else char for char in answer]

Then you can handle your input logic:

print(' '.join(guess_box))
player_choice = input("\nguess a letter: ")

while player_choice:
    print(f"You chose: {player_choice}")
    # Convert user input to lower case
    player_choice = player_choice.lower()
    # If this letter was guessed before:
    if player_choice in guessed_letters:
        print("You guessed this letter already.")
    else:
        guessed_letters.add(player_choice)

        # Check if this choice is in our word
        if player_choice in letters:
            # If we have this letter then change
            # the guess_box
            for index, val in enumerate(answer):
                # Of the user input
                if val.lower() == player_choice:
                    guess_box[index] = val
        else: # The letter is wrong
            print("That letter didn't fit")

    print(' '.join(guess_box))
    player_choice = input("\nguess a letter: ")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I add multiple lines of txt into an array and compare each letter to a user-input letter?

How do I make a specific letter of a string uppercase in Julia?

How to compare user inputted string to a character(single letter) of a word in a list?

How do I compare user input to a variable?

How do I replace a certain string in a list from user input?

How do I only accept capital letter as user input?

How do I get my (if "input" in list) to check every letter of the input and not just the first letter of input?

C++ How can I compare a user input to a list of string with strict ordering?

How to check if a text input string on change has an Uppercase letter Javascript

How do I check if a std::string, containing utf8 text, starts with an uppercase letter in Windows?

How do i check string to contain only letter(Uppercase & Lowercase) and at least one number only

How to compare user input with string?

How do I convert user input into a list?

How do i output a user input string

How do I compare user input to a preset character to see if they are the same?

How can I test if a letter in a string is uppercase or lowercase using JavaScript?

How do I count the words that start with an uppercase letter in java?

How do I detect any uppercase letter in an Excel cell?

How do I obtain a string from a list and compare it to another string?

how do I compare a input string “BBBB” with the schools in the above ArrayList?

How do i make the user input a number, and if the user inputs a letter it returns an error - VB.net

How do I add user input to a list and total up that list?

How do I compare the length of a string to an integer within the same list?

Trying to create a guess my letter code. How do I incorporate the case where the input char from the user equals my letter?

compare user input with a string

How do i take the input quote from a user and shuffle it then put every letter separate in a table in JavaScript

How do i scan each character in a user input and compare it to the second integer they input?

How to separate ABAP string by Capital Letter (Uppercase)?

How to randomly return an Uppercase letter in a String?