0

My aim is to be able to input a large (6 digit) number into a terminal and for it to return the middle two digits, swapped.

My code is as follows:

a = int(input())
b = int(input())
c = int(input())
d = int(input())
e = int(input())
f = int(input())

addition = ((a * 0) + (b * 0) + (d * 10))
addition_two = ((c) + (e * 0) + (f * 0))
print(addition + addition_2)

I'm unsure of how to adjust this in order for it to work correctly.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
DPA
  • 11

2 Answers2

0

I don't understand all the complexity. If you only want the middle two digits (and they're swapped), you can simply use:

n = input("Enter number: ")
ans = n[2:4]
return int(ans) //if you want int type, else ans would suffice as str
Abhinav Mathur
  • 7,791
  • 3
  • 10
  • 24
0

Ask for inputs until it is 6 long and only consists of digits. there is no need to convert anything to a number, you can work with the string directly by slicing it correctly:

while True:
    k = input("6 digit number")
    if not all(c.isdigit() for c in k) or len(k) != 6:
        print("Not a six digit number")
    else:
        break

# string slicing:
#   first 2 letters
#   reversed 4th and 3rd letter
#   remaining letters
print(k[:2] + k[3:1:-1] + k[4:])

See Asking the user for input until they give a valid response and Understanding slice notation

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69