The T
The T

Reputation: 347

Ignore inputs with more than one-element and then terminate. (Before pressing enter)

In my script, If the input-length is > 1. It outputs NO and then terminates.

This is what I tried.

import re
N = input("Enter your integer N: ")
if len(N) > 1:
    print('no')
    quit()

Take notice that the if statement is not encountered before input, this does not allow me to instantly terminate before pressing enter. I want to instantly terminate the script once I give more than one element.

Output

Enter your integer N: 10
no

Intended results after trying to enter 10. ( See that my script is intended to ignore more than one-element)

Output

    Enter your integer N: 1
    no

With this restriction, the space-complexity will truly be O(1).

Question

Is there some function in python that will restrict the input as I intend?

Upvotes: -1

Views: 178

Answers (1)

MEE
MEE

Reputation: 2412

This should give you a Θ(2) space:

import readchar

x = readchar.readchar()
rest = readchar.readchar()
if rest != '\r':
    print('no')
    quit()

Upvotes: 0

Related Questions