Reputation: 23
I'm working on a code problem where first I have to take input integer N and then take input of string of length N in python3.
Upvotes: 0
Views: 7311
Reputation: 11
# Please use the below line
print(len( input("please type something: ")))
Here Input can be a character or a number.
It will take the input -> (input("please type something: ")
print the input count-> print(len( input("please type something: ")))
Upvotes: 1
Reputation: 1598
You just need to use input to get the user input. You can put this inside a while-loop, so it keeps asking until a valid input is given. For the string you use input as well, there you could keep asking if the string given is not the right length, or just truncate the input.
# Ask for a number
while True:
try:
n = int(input("Enter number: "))
break
except ValueError:
print("Invalid input")
# Just truncate the input
value = input(f"Enter string of length {n}: ")[:n]
# Keep asking until right length
while True:
value = input(f"Enter string of length {n}: ")
if len(value) == n:
print("Valid answer")
break
print("Invalid input")
Upvotes: 0