0

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.

2
  • n = int(input() and then s = input()[:n] Commented Jul 13, 2019 at 4:31
  • If the string is not length N, should there be an error or not? Commented Jul 13, 2019 at 4:38

2 Answers 2

1
# 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: ")))

Sign up to request clarification or add additional context in comments.

Comments

0

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")

1 Comment

Thank you very much for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.