-1

I am new to strings and I have am trying to swap characters based on their indices

If either index is invalid (i.e., something besides 0, 1, 2, ... , len(x) - 1), the function should return None.

x =0
i = 0
j = 0


def swap(x, i, j):
    x = (input("Enter a string: "))
    i = int(input("first number swap: "))
    j = int(input("Second number swapped: "))
    for ch in range(i, j):
        print(x[i:j])
    if ch not in range(i, j):
        print("None")



swap(x, i, j)

I tried using this function but I get a repeat based on input j, minus the letters excluded based on input i

#input for x = sloth
#input for i = 4
#input for j = 1 
#will equal = lot x3

Instead I want the character at index 4 to be switched with character at index 1, but if it is not in the range of those characters, then the program should return 'none'

can anyone show me what I am doing wrong?

  • 2
    You are not doing any swapping here. You shouldn't be checking `ch`, you should be checking that `i` and `j` are within `range(len(x))`. You should not be doing the inputs as part of the function. Your function accepts `x`, `i`, and `j` as parameters. Do the inputs outside the function, and pass the values in. And your `for`/`print` loop does exactly what you say. It prints the selected part of the string once for each number in the range. – Tim Roberts Apr 04 '22 at 18:47
  • [How to step through Python code to help debug issues?](https://stackoverflow.com/questions/4929251/how-to-step-through-python-code-to-help-debug-issues) If you are using an IDE **now** is a good time to learn its debugging features Or the built-in [Python debugger](https://docs.python.org/3/library/pdb.html). Printing *stuff* at strategic points in your program can help you trace what is or isn't happening. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – wwii Apr 04 '22 at 19:00

2 Answers2

2

If the function is supposed to take x, i, and j as arguments, you should use those arguments, not call input() inside the function. If you want to use input() to get the arguments, do that before you call the function.

Since strings are immutable, you can't simply swap the characters, so I suggest building a new string via slicing:

def swap(x, i, j):
    i, j = sorted((i, j))
    try:
        return x[:i] + x[j] + x[i+1:j] + x[i] + x[j+1:]
    except IndexError:
        return None

print(swap(
    input("Enter a string: "),
    int(input("first number swap: ")),
    int(input("Second number swapped: "))
))
Enter a string: abcdefg
first number swap: 2
Second number swapped: 4
abedcfg

Another option might be to convert x to a mutable sequence (like a list) so you can do the swap, and then join it back into a string to return it:

def swap(x, i, j):
    y = list(x)
    try:
        y[i], y[j] = y[j], y[i]
        return ''.join(y)
    except IndexError:
        return None
Samwise
  • 68,105
  • 3
  • 30
  • 44
-1
x = input('Enter your string: ')
i = int(input('Enter the index of the first letter: '))
j = int(input('Enter the 2nd index of the next letter: ')  

def swap(x, i, j):
i, j = sorted((i, j))
if i and j in range(len(x)):
    return x[:i] + x[j] + x[i+1:j] + x[i] + x[j+1:]
else:
    return None    

Use len() in the range in order to determine how many indexes are present in the input. Use i and j in an if statement along with the range in order to fix the range. Finally, use return along with the statement that swaps the input i and j.