3

I have been looking around to see if I could find something that could help, but nowhere has an answer for what I'm looking for. I have a Hangman game I'm doing for a final project in one of my classes, and all I need is to make it so if a word has a capital letter, you can input a lowercase letter for it. This is the code.

import random
import urllib.request

wp = "http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content- 
type=text/plain"
response = urllib.request.urlopen(wp)
long_txt = response.read().decode()
words = long_txt.splitlines()

###########
# Methods #
###########
def Run():

  dashes1 = "-" * len(word) 
  dashes2 = "-" * len(word2)
  used_letter = []
  dashes = dashes1 + " " + dashes2

  #dashes ="-" * len(secretWord)
  guessesLeft = 6


  while guessesLeft > -1 and not dashes == secretWord:

    print(used_letter)
    print(dashes)
    print (str(guessesLeft))



    guess = input("Guess:")
    used_letter.append(guess)


    if len(guess) != 1:
      print ("Your guess must have exactly one character!")

    elif guess in secretWord:
      print ("That letter is in the secret word!")
      dashes = updateDashes(secretWord, dashes, guess)


    else:
      print ("That letter is not in the secret word!")
      guessesLeft -= 1

  if guessesLeft < 0:
    print ("You lose. The word was: " + str(secretWord))
    print(dashes)

  else:
    print ("Congrats! You win! The word was: " + str(secretWord))
    print(dashes)

 def updateDashes(secret, cur_dash, rec_guess):
  result = ""

  for i in range(len(secret)):
    if secret[i] == rec_guess:
      result = result + rec_guess    

    else:

      result = result + cur_dash[i]

  return result

########
# Main #
########
word = random.choice(words)
word2 = random.choice(words)
#print(word)
#print(word2)
secretWord = word + " " + word2 # can comment out the + word2 to do one 
word or add more above to create and combine more words will have to adjust 
abouve in Run()

splitw = ([secretWord[i:i+1] for i in range(0, len(secretWord), 1)])

print (splitw)
Run()

any bit of help is appreciated. The website I'm using has a bunch of words that are being used for the words randomly generated. Some are capitalized, and I need to figure out how to let the input of a letter, say a capital A, accept a lowercase a and count for it.

Shade
  • 33
  • 2
  • Possible duplicate of [How to convert string to lowercase in Python](https://stackoverflow.com/questions/6797984/how-to-convert-string-to-lowercase-in-python) – UnholySheep May 24 '18 at 13:20
  • You should reduce the code that you post to the question at hand. Your code should be just enough to demonstrate the need to compare your example letter `A`. – quamrana May 24 '18 at 13:21
  • hi, have you tried to do guess.lower() or guess.upper() and the same with secretWord. you could check 'if guess.lower() == secretWord.lower(): ' – Zapho Oxx May 24 '18 at 13:23
  • Sorry about that. First time on here, I wanted to post it all to see if maybe somebody knew where I should put it in the code, since it can be strict where you put it. – Shade May 24 '18 at 13:23
  • @ZaphoOxx yes, I have, and I've gotten no results. It either tells me that it takes no arguments, or it does nothing to the words or the input – Shade May 24 '18 at 13:24

4 Answers4

1

you could compare after you converted everything to lowercase. e.g. you could do

secretWord = word.lower() + " " + word2.lower() # that should make your secret all lowercase

for the input you should do the same:

guess = input("Guess:").lower()

after that it should not matter if it is upper or lower case. it should always match if the letter is the correct one. hope that helps

Zapho Oxx
  • 275
  • 1
  • 16
0

Simply check everything in lowercase:

[...]
elif guess.lower() in secretWord.lower():
[...]

and so on.

Mike S
  • 180
  • 1
  • 15
0

I would just change this line:

while guessesLeft > -1 and not dashes == secretWord:

to:

while guessesLeft > -1 and not dashes.lower() == secretWord.lower():

This way you are always comparing lower-case representations of the user's input to the lower-case representation of your secretWord. Since this is the main loop for your game, you want to break out of this as soon as the user's guess matches your word regardless of case. Then later in your code, you will still check whether they had any guesses left, and print out their answer and the secret word as before.

No other changes required, I think.

Engineero
  • 12,340
  • 5
  • 53
  • 75
  • Thank yo. I've been messing with this code for a few weeks. I overlooked this. My apologies. – Shade May 24 '18 at 13:30
  • Nothing to apologize for! But when you get the reputation to do so, go ahead and mark any answer that worked for you as "accepted". – Engineero May 24 '18 at 13:30
0

You could just force all comparisons to be made in the same Case, such as lowercase.

Let’s say that your word is "Bacon". and someone enters "o".
That will be a match because quite obviously “o” equals “o” so you can cross that letter off the list of letters left to guess.

But in the case that someone enters “b” then b is NOT equal to “B”.
So why not just force all letters to be compared using the same case?

So your comparison will be like

    elif guess.Lower() in secretWord.Lower()

My python is rusty as hell, but the idea here should do what you want to do

jasttim
  • 723
  • 8
  • 19