I was making a simple Hangman game in Python.
However, I have some problem in the following piece of code
newword=""
def update(word,guessword,ch):
for i in range(len(guessword)-1):
if ch==word[i]:
print(i)
if i==0:
newword=ch + guessword[1:]
else:
newword=guessword[0:i-1] + ch + guessword[i+1]
return newword
This update function is called when a match is found and is used to update guessword.
Here guessword is a string containing "_" for every unmatched position. The string word hold the original word. ch is the letter which is found in the original word.
Use range(len(guessword)):. The -1 is not needed, range already excludes the upper bound.
Use guessword[i+1:] at the end - you forgot the :. Otherwise it's just one character.
That should fix your existing code. To make it better, I suggest making guessword a list of characters so that you can modify it, then the whole thing will become much simpler. Also use enumerate instead of range.
Related
So this game is hangman and the list wordFinder holds as many underscores as the letters in the word you are trying to guess. When you guess correctly, it should remove the underscore respective to the letter you guessed, so that it can then insert it back into it's place. This works fine usually, apart from when you guess a letter that comes after another letter in the word that you have already guessed. This causes the program to remove the wrong underscore, and it shifts the letters to the left.
ex) for bear: _ e _ _ => guess=a => removes the first underscore, => e _ a _
here is the section of code for this adding and removing:
for i in range(len(word)):
if guess==word[i]:
print(i)
found=found+1
print("Going to remove",i," which is ",wordFinder[i])
wordFinder.remove(wordFinder[i])
print(wordFinder)
if i==(len(word)-1):
print("last letter so add on end")
wordFinder.append(guess)
else:
print("Inserting at ",i)
wordFinder.insert(i,guess)
else:
print("Wrong guess!")
pass
where word is the word you are guessing, found is just a counter for how many letters you have got
wordFinder is ["","","",""] in the case of bear
note: when i change these underscores to different characters eg (, . -) the problem doesnt happen
I tried everything - but the note above is interesting.
I don't know why this is happening but any help would be really appreciated
The core of the problem you have experienced is the method you use to remove an item from a list:
wordFinder.remove(wordFinder[i])
In case there are multiple occurrences of the same character in wordFinder the first found (.remove(value) searches in the list for value and if it finds one removes it from the list) one will be removed. In case i addresses not the first occurrence of value then the wrong character will be removed from the list. If you use only different characters than the right one will be found as there is no ambiguity involved if more than one occurrence of same value is present in the list.
This above answers the question: I don't know why is this happening?
You can fix the problem with ambiguity removing the character at index i using .pop(i):
wordFinder.pop(i)
instead.
I'm trying to code hangman for a class project, and I'm sure there are easier ways, but I can't make it look too advanced because I'm still a beginner. I'm trying to break down a word then put it into a list using ljust to make the word big enough to fit in the list, but then I need to remove the spaces from the list and I have no clue how.
It is not clear what you are asking. However, it seems like you want to turn a phrase into a list of characters. The following will turn a string into a list of characters after removing white spaces.
answer = "guess this"
answer = answer.ljust(15)
print(answer)
answer = answer.replace(" ", "") # remove spaces
print(answer)
characters = list(answer)
print(characters)
You don't need to change the list size in python. You can add to this list or remove from this list and python will handle the size change.
I need to create a box with parameters that prints any input the user puts in. I figured that the box should be the length of the string, but I'm stuck with empty code, because I don't know where to start.
It should look like this:
I agree with Daniel Goldfarb comments. Don't look for help without trying.
If you still couldn't get how to do that, then only read my remaining comment.
Just print :
str = string entered
len(str) = string length
+-(len(str) * '-')-+
| str |
+-(len(str) * '-')-+
So hopefully you can learn, don't want to just write the code for you. Basically break it into steps. First you need to accept user input. If you don't know how to do that, try googling, "python accept user input from stdin" or here is one of the results from that search: https://www.pythonforbeginners.com/basics/getting-user-input-from-the-keyboard
Then, as you mentioned, you need the length of the string that was input. You can get that with the len function. Then do the math: It looks like you want "|" and two spaces on each side of the string, giving the length plus 6 ("| " on either side). This new length is what you should make the "+---+" strings. Use the print() function to print out each line. I really don't want to say much more than that because you should exercise your brain to figure it out. If you have a question on how to generate "+---+" of the appropriate length (appropriate number of "-" characters) you can use string concatenation and a loop, or just use the python string constructor (hint: google "construct python string of len repeat characters"). HTH.
One more thing, after looking at your code, in addition to my comment about printing the string itself within the box, I see some minor logic errors in your code (for example, why are you subtracting 2 from the width). THE POINT i want to me here is, if you ware going to break this into multiple small functions (a bit overkill here, but definitely a good idea if you are just learning as it teaches you an important skill) then YOU SHOULD TEST EACH FUNCTION individually to make sure it does what you think and expect it to do. I think you will see your logic errors that way.
Here is the solution, but I recommend to try it out by yourself, breakdown the problem into smaller pieces and start from there.
def format(word):
#It declares all the necessary variables
borders =[]
result = []
# First part of the result--> it gives the two spaces and the "wall"
result.append("| ")
# Second part of the result (the word)
for letter in word:
result.append(letter)
# Third part of the result--> Ends the format
result.append(" |")
#Transforms the list to a string
result = "".join(result)
borders.append("+")
borders.append("--"+"-"*len(word)+"--")
borders.append("+")
borders="".join(borders)
print(borders)
print(result)
print(borders)
sentence = input("Enter a word: ")
format(sentence)
I'm new to Python, and I've found this solution. Maybe is not the best solution, but it works!
test = input()
print("+-", end='')
for i in test:
print("-", end='')
print("-+")
print("| " + test + " |")
print("+-", end='')
for i in test:
print("-", end='')
print("-+")
print "YOU HAVE CHOSEN TO REARRANGE YOUR THE WORD THAT YOU ARE ABOUT TO ENTER..."
word = raw_input ("FIRSTLY YOU MUST ENTER A WORD TO BE REARRANGED, ENTER IT HERE:")
character_save = word[1]
def anagram(word):
if len(word)>1:
print str.replace('a','b')
word = str.replace(word[1],word[3])
word= str.replace(word[3], character_save,1)
print word
anagram(word)
I tried to fix this on numerous occasions, the problem with the first time was that it would just replicate characters instead of replacing the positions, the second time I tried to store the position that I was going to replace in a variable but now it mentions that I have only one argument given (when it should be 2).
Would it be easier to do this with a list instead of a string?
The replace message that you are using is called on the string that you want to replace and not on the str type itself.
In your case that is the word parameter that you are providing.
So if you replace the instances of str.replace with word.replace your code will run. However, it doesn't create an anagram yet. The algorithm is still lacking.
I am a beginner in python. I came across this question in codewars.
Jaden is known for some of his philosophy that he delivers via Twitter. When writing on Twitter, he is known for almost always capitalizing every word.
Your task is to convert strings to how they would be written by Jaden Smith. The strings are actual quotes from Jaden Smith, but they are not capitalized in the same way he originally typed them.
Example :
Not Jaden-Cased: "How can mirrors be real if our eyes aren't real"
Jaden-Cased: "How Can Mirrors Be Real If Our Eyes Aren't Real"
This is my attempt (I am supposed to code using a function)
def toJadenCase(string):
l = len(string)
for i in range(0,l):
if string[i] == ' ':
y = string[i]
string[i+1] = chr(int(y)-32)
return srting
s = raw_input()
print toJadenCase(s)
When run, the following errors showed up
How can mirrors be real if our eyes aren't real (this is the input string)
Traceback (most recent call last):
File "jaden_smith.py", line 9, in <module>
print toJadenCase(s)
File "jaden_smith.py", line 6, in toJadenCase
string[i+1] = chr(int(y)-32)
ValueError: invalid literal for int() with base 10: ''
I couldn't understand these errors even after google-ing it. Any help would be appreciated. I would also be great if other errors in my code are highlighted and a better code is suggested.
Thanks in advance :D
As Goodies points out, string should not be used as a variable name
Following the Zen of Python, this is technically a function that does exactly what you're trying to achieve:
def toJadenCase(quote):
return quote.title()
Edit:
Revised version to deal with apostrophes:
import string
def toJadenCase(quote):
return string.capwords(quote)
First you have to understand that strings are immutable, so you cannot set a single character inside a string, but build a new string from the old one and replace the old one (this can be usually done still in one pass so it's not a big complication).
Second, for most of these kind of operations, it is much better to use the methods of the string object itself, rather than redo everything from scratch.
Said that, there is still some complication with the question, but a function that does what you want is in the module string:
import string
s="How can mirrors be real if our eyes aren't real"
newstring=string.capwords(s)
If you prefer (why?!) a DIY solution (using string methods):
newstring=' '.join([ss.capitalize() for ss in s.split()])
Note that using split without argument splits the string on any whitespace (e.g. tabs etc.), that I think is the desired behavior.
If you want to do this without using a function that already exists, this is how I would do it and I'll explain everything:
Assuming you get a string with ONLY text based words and all words start with a character*
def toJadenCase(string):
words = string.strip().split()
# This first strips all empty spaces around the words in the text and then splits the string by spaces (default) otherwise you can add a character inside split in order to split it at the character. This returns a list of words in the sentence.
li = [] # initialize empty list
for word in words:
word = chr(ord(word[0])-32) + word[1:]
# So there's a couple of things going on here.
# I could use .upper() to upper case something (like word[0].upper() + word[1:]
# in order to get it but I wanted to do it without the use of that.
# That being said, ord just figures out the ascii number and subtracting
# 32 makes it uppercase. chr changes it back to a string.
# Then it can be concatenated to the rest of the word.
# Strings can be treated as lists in python so word[0] and word[1:] works
Also, word[1:] just means from the 1st index to the end.
li.append(word) # this appends the word to the list
return ' '.join(li) # this joins all of the words in the list with a space
Now, if you want something a lot more concise (you can use .capitalize()):
def toJadenCaseShort(string):
return ' '.join([x.capitalize() for x in string.strip().split()])
which returns:
>>> abc("hello my friends")
'Hello My Friends'
Basically what it does is it uses list comprehension to strip and then split the words, capitalizes them, and then joins them with spaces!
Of course, you could just use string.title() as mark s. says but what's the fun in that? :)
Here is the answer that passed for me
import string
def toJadenCase(str):
quote = string.capwords(str)
return quote #Do not use print(quote) as it adds spaces
def toJadenCase(str):
quote = string.capwords(str)
return quote #Do not use print(quote) as it adds spaces