Python Forum

Full Version: programming homework
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
anyone knows why this does not give me the shared letters properly?
#shared_positions = 0
#x = 0
#while True:
#    print('Enter two words of the same lenght')
#    word_1 = (input('Enter the first word: '))
#    word_2 = (input('Enter a second word: '))
#    l_word_1 = len(word_1)
#    l_word_2 = len(word_2)
#    sums = word_1 + word_2
#    if range(l_word_1) == range(l_word_2):
#        break
#    else:
#        print('The words have different sizes, please enter words of the same size.')
#
#for x in range(0, l_word_1):
#    if word_1[x] == word_2[x]:
#        shared_positions += 1 
#    print('They share {} at position {}.' .format(word_1[x], x))
#print('The total share of letters is ', shared_positions)
Please put your code in Python code tags, you can find help here. And remove the hashes, they make the code even more unreadable.
I edited the code to have code tags, although I didn't remove the hashes in spite of them making it harder to read. Is this the actual code you're running? Because if it is, the hashes make everything a comment and that's why it's not working. Otherwise, can you make more explicit what the expected vs actual output is?
The printing the shared positions is outside the if block. It will print all word_1 letters and their positions because it executes at every iteration.

if range(l_word_1) == range(l_word_2) could be like this if l_word_1 == l_word_2
(May-11-2018, 05:39 AM)wavic Wrote: [ -> ]The printing the shared positions is outside the if block. It will print all word_1 letters and their positions because it executes at every iteration.

if range(l_word_1) == range(l_word_2) could be like this if l_word_1 == l_word_2

still does not work..
How does not work? Did you remove all #? See @micseydel's post
I don't know if this is still open, but try moving the line
print('They share {} at position {}.' .format(word_1[x], x))
in to the "if" block