Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
programming homework
#1
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)
Reply
#2
Please put your code in Python code tags, you can find help here. And remove the hashes, they make the code even more unreadable.
Reply
#3
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?
Reply
#4
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(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..
Reply
#6
How does not work? Did you remove all #? See @micseydel's post
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Verilog HDL Programming to Python Programming? noobcoder 1 2,985 Jul-18-2019, 09:28 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020