I'm still learning the ropes with my programming and have run into an issue that I can't seem to solve after searching for the answers
I have a while loop that is checking if a dictionary is not empty, or if the user entered a specific character (escape character).
The program works as expected if the user enters the escape character on the first prompt but seems to not function correctly if they first enter something else and then try it.
Can anyone help?
hand = {'p':1, 'y':1, 't':1, 'h':1, 'o':1, 'n':1}
def play_hand(hand):
print hand
word = raw_input('Enter word, or a "." to indicate that you are finished: ')
while any(hand) is True or word not in '.':
if word == '.':
break
elif (word == 'toy' or word == 'python') and (word != '.'):
print '"'+ word +'"', 'earned', 'points.'
play_hand(hand)
else:
print 'Invalid word, Please try again.'
return word, play_hand(hand)
else:
print 'Ending'
return
So as suggested I restructured the code and removed the recursion from the inner IF ELSE statements. It did the trick!
Thanks for the help.
Here's the sample from above done in the same fashion. You can now enter the valid word and also exit using the escape character.
hand = {'p':1, 'y':1, 't':1, 'h':1, 'o':1, 'n':1}
def play_hand(hand):
while any(hand) is True:
print hand
word = raw_input('Enter word, or a "." to indicate that you are finished: ')
if word == '.':
print 'Goodbye!'
break
else:
if not (word == 'toy'):
print 'Invalid word, Please try again.'
else:
print '"'+ word +'"', 'earned', 'points.'
hand = {'p':1, 'h':1, 'n':1}
Related
For the get_letter_from_user function, while using the while loop for validation, it keeps repeating the invalid input; I want to make sure that it is a single letter and lower case, and I want to make sure that it doesn't equal the second parameter of the function. I'm not sure what I'm doing wrong, though. (and how to get gud at coding if u have tips)
def get_text_from_user(prompt):
return input(prompt).lower()
def get_letter_from_user(prompt, not_allowed):
not_allowed = ''
allowed = input(prompt).lower()
while not allowed == not_allowed or allowed.isalpha() or len(allowed) > 1:
allowed = str(input('Invalid letter, try again:'))
return allowed
def main():
text = get_text_from_user("Enter some text: ")
ltr1 = get_letter_from_user("Enter a letter: ", '')
ltr2 = get_letter_from_user("Enter another letter: ", ltr1)
new_text = text.replace(ltr1,ltr2)
print("The new text is", new_text)
if __name__ == "__main__":
main()
Suggestion for the function:
def get_letter_from_user(prompt, not_allowed):
allowed = input(prompt).lower()
while allowed == not_allowed or len(allowed) > 1:
print('not_allowed:',not_allowed)
allowed = str(input('Invalid letter, try again:'))
return allowed
ltr1 = get_letter_from_user("Enter a letter: ", '')
ltr2 = get_letter_from_user("Enter another letter: ", ltr1)
Sample output:
Enter a letter: d
Enter another letter: d
not_allowed: d
Invalid letter, try again:d
not_allowed: d
Invalid letter, try again:a
To replace a letter or sequence of letters in a string, you might want to take a look at the string.replace() function:
text = input('Enter some text: ')
find = input('Enter a letter to replace: ')
replace_with = input(f'Enter a letter to replace \'{find}\' with: ')
replaced = text.replace(find, reolace_with)
print('The new text is:', replaced)
To add another little detail because you asked how to get better at coding:
I would never make a function with a parameter that is immediately changed to an empty string. Like:
def get_letter_from_user(prompt, not_allowed):
not_allowed = ''
Rather use a default value like this:
def get_letter_from_user(prompt, not_allowed=''):
...
EDIT: Completely fixed now.
EDIT: Okay so now I got it to change '... --- ...' to 'sos' (yippee!) But for some reason, it gives me a KeyError when I input '... --- ... / ... --- ...'.
This should give me 'sos sos' but I get a KeyError for ' '.
I think this is because after the '/', there's a space which the program sees as a trigger to call the dictionary value of the current key. Problem being, the current key at that time is blank since the previous character was the '/'. How would I be able to fix this?
Thanks.
I'm pretty new to programming with python (well, programming in general, really...) and today I had the bright idea of making a morse code converter.
I have the "plain text to morse code" working flawlessly, but the "morse code to plain text"?
Not so much.
The problem is that when I run the program, it doesn't give me anything, it just breaks its loop (like I told it to) without anything coming back to me.
If you guys could help me out, I'd very much appreciate it.
Oh also, 'decoding_dict' is the dictionary which I made which correlates morse code values to plain text.
For example,
decoding_dict = {'...' : 's' , '---' : 'o'}
And so on and so forth.
def decode(text):
text += ' '
#I have it set up to trigger when there's a space, hence this.
global key
global decoded_text
#I thought maybe this would fix it, it didn't. :(
key = ''
decoded_text = ''
for i in text:
if i == '.':
key += i #This adds a '.' to the key to reference later.
continue
elif i == '-':
key += i #See above comment.
continue
elif i == ' ':
decoded_text += decoding_dict[key]
text = text[(len(key) + 1) :]
key = '' #Calls the value of the key, cuts out the used text, and resets key.
continue
elif i == '/':
decoded_text += decoding_dict['/']
continue #In morse code, a '/' is a ' ' and that's in the dict.
elif text == '':
print "Result: " + decoded_text
break #This is basically the end of the loop
else:
print "Error, please try again."
break
Now when I run it with '... --- ...' , it goes back to the beginning and doesn't print anything.
(By beginning, I mean the menu I made beforehand.)
According to your code try something like this:-
def decode(text):
text += '$'
#I have it set up to trigger when there's a space, hence this.
global key
global decoded_text
#I thought maybe this would fix it, it didn't. :(
key = ''
decoded_text = ''
for i in text:
if i == '.':
key += i #This adds a '.' to the key to reference later.
continue
elif i == '-':
key += i #See above comment.
continue
elif i == ' ':
decoded_text += decoding_dict[key]
text = text[(len(key) + 1) :]
key = '' #Calls the value of the key, cuts out the used text, and resets key.
continue
elif i == '/':
decoded_text += decoding_dict['/']
continue #In morse code, a '/' is a ' ' and that's in the dict.
elif text == '$':
print "Result: " + decoded_text
break #This is basically the end of the loop
else:
print "Error, please try again."
break
According to My Suggestion Try This:-
def decode(text):
error = False
#I have it set up to trigger when there's a space, hence this.
global key
global decoded_text
#I thought maybe this would fix it, it didn't. :(
key = ''
decoded_text = ''
for i in text:
if i == '.':
key += i #This adds a '.' to the key to reference later.
continue
elif i == '-':
key += i #See above comment.
continue
elif i == ' ':
decoded_text += decoding_dict[key]
text = text[(len(key) + 1) :]
key = '' #Calls the value of the key, cuts out the used text, and resets key.
continue
elif i == '/':
decoded_text += decoding_dict['/']
continue #In morse code, a '/' is a ' ' and that's in the dict.
else:
print "Error, please try again."
error = True
break
else:
If not error:
print "Result: " + decoded_text
Last else is used with for instead of if else
Assuming space ' ' is a separator in the input you specify, you may use this:
decoding_dict = {'...' : 's' , '---' : 'o'}
my_text = '... --- ...'
print(''.join(list(map(lambda x: decoding_dict.get(x, None),
Output:
sos
As for the code above, there are couple of issues:
modifying 'text' while iterating over it
checking if text == '', which never happens. Hence, the result is never printed.
Try this:
def decode(text):
text += ' '
#I have it set up to trigger when there's a space, hence this.
global key
global decoded_text
#I thought maybe this would fix it, it didn't. :(
key = ''
decoded_text = ''
for i in text:
if i == '.':
key += i #This adds a '.' to the key to reference later.
continue
elif i == '-':
key += i #See above comment.
continue
elif i == ' ':
decoded_text += decoding_dict[key]
key = '' #Calls the value of the key, cuts out the used text, and resets key.
continue
elif i == '/':
decoded_text += decoding_dict['/']
continue #In morse code, a '/' is a ' ' and that's in the dict.
else:
print("Error, please try again.")
break
print("Result: " + decoded_text)
call: decode(my_text)
Output:
Result: sos
The for loop only considers the initial value of text. Your changes to text in the loop have no effect, because text is a string, and strings are immutable, so your modifications don't modify the original object that you're iterating over. The result is that your loop iterates over all of the original text, completing the loop without ever entering either of your break conditions.
Just get rid of the if text == '' check and move the "print results" line outside the for loop. You don't need to "cut out the used text", because by the time you get to the point where you want to do that, you've already iterated past the entire key. If you just pick up where you left off, you'll begin parsing the next Morse letter on the next loop iteration anyway.
Thank you #Idor I am making some progress but I am not 100% there yet. Right now my code looks as following:
def easy_game(easy_text, parts_of_speech1):
replaced = []
easy_text = easy_text.split()
i = 0
for word in easy_text:
replacement = word_in_pos_easy(word, parts_of_speech1)
if replacement != None:
user_input = raw_input("Type in: " + replacement + " ")
word = word.replace(replacement, user_input)
while word != solutions[i]:
print "Sorry, you are wrong"
user_input = raw_input("Type in: " + replacement + " ")
print i
i = i + 1
print i
replaced.append(word)
else:
replaced.append(word)
replaced = " ".join(replaced)
print
#time.sleep(1)
print "Ok, lets see your results. Does it make sense?"
print
#time.sleep(1)
return replaced
print
#time.sleep(1)
print easy_game(easy_text, parts_of_speech1)
You can see I added the while loop. I also added an index and for troubleshooting I added print i to see what the program is doing. It still confuses me a bit or doesn't work as I would expect it. But being a newbie to programming my expectations are probably wrong. Here's what's happening:
When you enter the correct answer the program continues to question 2 and also increases i by 1
This works from beginning to end if you enter everything correctly
When you enter the wrong answer you are prompted to enter it again. Good!
However the user then gets stuck in this very question although i has been increased to the right value.
I don't really understand why the user would be stuck at this point when i has been increased, i.e. we would check at the right position in the list for the correct answer.
This is the full code of the game. I can successfully run it on my Mac but see the above behavior. Any thoughts on this by any chance? thanks in advance!
parts_of_speech1 = ["Word1", "Word2", "Word3", "Word4"]
# The following is the text for the easy text..
easy_text = "Python is a Word1 language that provides constructs intended to enable clear programs on both small and large scale. Python implementation was started in December Word2 by Guido von Rossum. The most simple Word3 in Python is Word4 and normally used at the beginning to tell Python to write 'Hello World' on the screen."
solutions = ["programming", "1989", "function", "print"]
# Checks if a word in parts_of_speech is a substring of the word passed in.
def word_in_pos_easy(word, parts_of_speech1):
for pos in parts_of_speech1:
if pos in word:
return pos
return None
# Plays a full game of mad_libs. A player is prompted to replace words in the easy text,
# which appear in parts_of_speech with their own words.
def easy_game(easy_text, parts_of_speech1):
replaced = []
easy_text = easy_text.split()
i = 0
for word in easy_text:
replacement = word_in_pos_easy(word, parts_of_speech1)
if replacement != None:
user_input = raw_input("Type in: " + replacement + " ")
word = word.replace(replacement, user_input)
while word != solutions[i]:
print "Sorry, you are wrong"
user_input = raw_input("Type in: " + replacement + " ")
print i
i = i + 1
print i
replaced.append(word)
else:
replaced.append(word)
replaced = " ".join(replaced)
print
#time.sleep(1)
print "Ok, lets see your results. Does it make sense?"
print
#time.sleep(1)
return replaced
print
#time.sleep(1)
print easy_game(easy_text, parts_of_speech1)
I am building out a quiz based on raw_input using several different list operations. I also want to validate the user input against a list before moving on to the next question in the quiz.
The function currently looks like this:
def play_game(ml_string, parts_of_speech):
replaced = []
ml_string = ml_string.split()
for word in ml_string:
replacement = word_in_pos(word, parts_of_speech)
if replacement != None:
user_input = raw_input("Type in a: " + replacement + " ")
word = word.replace(replacement, user_input)
if word != solution_list1[0]:
print "Sorry, you are wrong. Try again!"
replaced.append(word)
else:
replaced.append(word)
replaced = " ".join(replaced)
return replaced
In Line 9 I am checking against the List containing the solution words. Whereas the validation itself works the function just continues to the next question but I need it to repeat the question until getting the correct answer. I tried to reposition the different lines but simply can't get my head around it at this point in time. Where or how do I need to place the validation of the user input correctly to prompt the user for the same question again?
It seems to me that what you are looking for is a while loop.
Instead of:
if word != solution_list1[0]:
print "Sorry, you are wrong. Try again!"
Try:
while word != solution_list1[0]:
print "Sorry, you are wrong. Try again!"
user_input = raw_input("Type in a: " + replacement + " ") # ask the user again
word = word.replace(replacement, user_input)
This way the user will have to answer the question again (raw_input) until he gets it right.
Homework exercise:
Checking whether a text is a palindrome should also ignore punctuation, spaces and case. For example, "Rise to vote, sir." is also a palindrome but our current program doesn't say it is. Can you improve the above program to recognize this palindrome?
origin code:
def reverse(text):
return text[::-1]
def is_palindrome(text):
return text == reverse(text)
something = input('Enter text: ')
if (is_palindrome(something)):
print("Yes, it is a palindrome")
else:
print("No, it is not a palindrome")
my try:
import re
def reverse(text):
global words
words = text.split()
return words[::-1]
def is_palindrome(text):
return words==reverse(text)
something = input('Enter text: ')
if (is_palindrome(something)):
print("Yes, it is a palindrome")
else:
print("No, it is not a palindrome")
Error:
Enter text: jfldj
Traceback (most recent call last):
File "/Users/apple/PycharmProjects/Problem Solving/user_input.py", line 13, in <module>
print("Yes, it is a palindrome")
File "/Users/apple/PycharmProjects/Problem Solving/user_input.py", line 10, in is_palindrome
NameError: name 'words' is not defined
How should I change my code?
Latest code:
import string
def remove_punctuations(word):
return "".join(i.lower() for i in word if i not in string.ascii_letters)
def reverse(text):
return text[::-1]
def is_palindrome(text):
text = remove_punctuations(text)
return text == reverse(text)
something = input('Enter text: ')
if (is_palindrome(something)):
print("Yes, it is a palindrome"
else:
print("No, it is not a palindrome")
No matter what I input, output is Yes.
Enter text: hggjkgkkkk
Yes, it is a palindrome
What's wrong?
To ignore the punctuations, spaces and case of the given text you need to define a function remove_punctuations() which takes a word as parameter and returns a word with all lower case characters, remove punctuation marks and removed spaces.
To remove the unwanted characters we need to iterate over the given text, if the current character falls in strings.ascii_letters , then generate the character converting it to lower caps using str.lower() method. Finally using "".join() method to concatenate the generated str elements.
import string
def remove_punctuations(word):
return "".join(i.lower() for i in word if i in string.ascii_letters)
def reverse(text):
return text[::-1]
def is_palindrome(text):
text = remove_punctuations(text)
return text==reverse(text)
something = "Rise to vote, sir."
if (is_palindrome(something)):
print("Yes, it is a palindrome")
else:
print("No, it is not a palindrome")
Since the hint says to use a tuple with forbidden punctuation marks, I created the following variant:
forbidden = (' ', ',', "'", '?', '!', '.', '’')
def reverse(text):
return text[::-1]
def cleaning(text):
clean_text = ''
for item in text:
if item not in forbidden:
clean_text += item
return clean_text
def is_palindrome(text):
lower_text = cleaning(text.lower())
return lower_text == reverse(lower_text)
example = input('Enter something: ')
if is_palindrome(example):
print("Yes, it is a palindrome")
else:
print("No, it is not a palindrome")
The cleaning function checks each character for belonging to a tuple of forbidden characters, if not, then concatenates it to the clean_text string
I started studying for python 2 days before so that is what i come up with.
It is not so much advanced but works like a charm. :D
It is pretty straight forward what i do there. I just make a tuple with the "legal" letters (abc=). Then I define a function that 1st change all letters to lower case and then checks every character in the string with the "legal" letters. Then after this "filtering" the rawtext contains only the "legal" ones. Then the 2nd function just reverses the results of the 1st one. Compare and da da..!
# Palindrome recognize
abc='abcdefghijklmnopqrstuvwxyz'
def rawtext(text):
rawtext=''
text=text.lower()
for j in text[::1]:
for i in abc[::1]:
if j==i:
rawtext=rawtext+j
return rawtext
def reverse(text):
rev= rawtext(text)[::-1]
return rev
text=str(input('Write text:'))
if reverse(text)==rawtext:
print('The text is palindrome')
else:
print('The text is not a palindrome')
from itertools import izip_longest
def is_palindrome(s):
l = len(s)
fi = (i for i in xrange(l) if s[i].isalpha())
bi = (i for i in xrange(l-1, -1, -1) if s[i].isalpha())
for f, b in izip_longest(fi, bi):
if f >= b: return True
if s[f].lower() != s[b].lower(): return False
return True
Hope that helps
I am having trouble replacing lists on python, my code is below.
def words_open():
global words
wordsfile = open("words.txt","r")
words_list = wordsfile.readlines()
words = []
for i in range(len(words_list)):
words.append(words_list[i].strip())
return words_list
return words
def replace_symbol_for_letter(letter,symbol):
print([s.replace(symbol,letter) for s in words])
return words
def enter_pairing():
correct_symbol = False
while correct_symbol == False:
symbol = input("Please enter a symbol: ")
if symbol not in symbols_list:
correct_symbol = False
elif symbol in symbols_list:
correct_symbol = True
correct_letter = False
while correct_letter == False:
letter = input("Please enter a letter: ")
letter = letter.upper()
if letter not in alphabet:
correct_letter = False
elif letter in alphabet:
correct_letter = True
current_pairings.append(symbol and letter)
replace_symbol_for_letter(letter,symbol)
return letter
return symbol
The code runs fine, without syntax errors, however I am having trouble replacing the 'words' list.
When I run the code this happens:
The words are:
#+/084&"
#3*#%#+
8%203:
,1$&
!-*%
.#7&33&
#*#71%
&-&641'2
#))85
9&330*
Please enter a symbol: #
Please enter a letter: A
['A+/084&"', 'A3*A%A+', '8%203:', ',1$&', '!-*%', '.A7&33&', 'A*A71%', "&-&641'2", 'A))85', '9&330*']
Please enter a symbol: +
Please enter a letter: b
['#B/084&"', '#3*#%#B', '8%203:', ',1$&', '!-*%', '.#7&33&', '#*#71%', "&-&641'2", '#))85', '9&330*']
As you can see the symbol is replaced to the letter, which works fine, however as soon as I enter a new symbol/letter pairing it deletes the previous replacement and the replaces it again.
Thanks!
In your code you don't modify the words list in replace_symbol_for_letter
print([s.replace(symbol,letter) for s in words])
This does not modify words list it just creates a new list having the pairing changed but words does not change
Replace it by this:
words = [s.replace(symbol,letter) for s in words]
print words
This should make it.
By the way: global vars are evil :)
You forgot to close your file avec reading it, you should consider to use the with statement to open your file, it will close it automaticly after your actions on it.
def words_open():
with open('words.txt', 'r') as file:
words_list = file.readlines()
words = [line.split() for line in words_list]
return words_list, words