Posts

Showing posts with the label user input

The next palindrome for given number(s)

 The following program takes user input as number(s) and tells whether the input is a palindrome. If it is not, it will return the next palindrome for that number. ncases = int(input("How many cases: ")) for i in range(ncases): case = input(f"Enter your case {i+1}: ") if case == case[::-1]: print("This is a palindrome") else: pali = case[:] while pali != pali[::-1]: pali = int(pali) + 1 pali = str(pali) if pali == pali[::-1]: print(f"The next palindrome for case {case} is {pali}")

Palindrome or NOT a Palindrome ?

 The following program takes a user input, be it a number or any word, and tells whether the input is a palindrome or not. ''' Whether a given word or number is a palindrome or not? ''' while True: word = input("Enter a word or number:\n") if word == word[::-1]: print(f"{word} is a palindrome") else: print(f"{word} is not a palindrome")

3 Methods of Reversing a User Input List in Python

  # defining empty list uList = [] # defining the number of elements in the list nList = int(input("List of how many elements? ")) print("Now enter the elements of your list one by one.") # taking elements of the list as user input for i in range(0,nList): element = int(input()) uList.append(element) print(f"The original list is {uList}") # taking copy of the list, storing it in variable, then reversing it reverse1 = uList[:] reverse1.reverse() print(f"The first reverse of the original list {uList} is {reverse1}") reverse2 = uList[::-1] print(f"The second reverse of the original list {uList} is {reverse2}") reverse3 = uList[:] for i in range(len(reverse3)//2): reverse3[i], reverse3[len(reverse3)-i-1] = reverse3[len(reverse3)-i-1], reverse3[i] print(f"The third reverse of the original list {uList} is {reverse3}") if reverse1 == reverse2 and reverse2 == reverse3: print("All the three meth

Taking List as user input in Python

The following is one of the ways to take 'list' as user input. # defining empty list uList = [] # defining the number of elements in the list nList = int(input("List of how many elements? ")) print("Now enter the elements of your list one by one.") # taking elements of the list as user input for i in range(0,nList): element = int(input()) uList.append(element) print(f"The original list is {uList}")