Why does my program not work (reverse function in python help)? -


i've made program had been set homework in computer science. i've found new, unfamiliar function won't seem work expected.

i'm new forum. p.s. hobby, i'm not here anwsers

def reverse():     s3 = ''     char in reversed(s3):         s3 = s3 + char  def match_char():     global s1     global s2     global s3     s1 = input('what first string? ')     s2 = input('what next string?  ')      reverse(s2)      num_matches = 0     in range(len(s1)):        if s1[i] == s2[i]:                           num_matches = num_matches + 1      return num_matches 

in python, strings immutable. when do:

reverse(s2) 

some things happen inside of reverse, string passed in unchanged.

to fix, first want return function:

def reverse(my_str):     s3 = ''     char in reversed(my_str):         s3 = s3 + char     return s3 

and assign on s2 when call reverse:

s2 = reverse(s2) 

there lots of other ways improve code, that's explanation of "unexpected" results.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -