-1

I would like to make (input) function accepts uppercase letters or numbers only in python? I mean how can I force the user to use uppercase letters or numbers only.

Thank you in advance.

khalidmfy
  • 15
  • 1
  • 1
    Please provide enough code so others can better understand or reproduce the problem. – Community Apr 06 '22 at 16:48
  • the `input()` function can accept any characters that can be typed. It is up to you to do the validation on the input string to make sure it works for your use-case. See [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) and please describe what you've tried so far – G. Anderson Apr 06 '22 at 16:50
  • `input` only reads from standard input; Python has no control over what gets *written* to that file by your terminal. – chepner Apr 06 '22 at 17:16

3 Answers3

0

You can use all(char in string.ascii_uppercase for char in data) with a while loop:

import string
data = input('Please enter a fully-uppercased string: ')
while not all(char in string.ascii_uppercase for char in data):
    data = input('Please enter a fully-uppercased string: ')
mara004
  • 1,435
  • 11
  • 24
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
0

You could simply do something like

x = input().upper()

Which would take the input then turn it into uppercase. Though I would suggest adding some code into your post just to make sure this is a viable option

Retraces
  • 24
  • 1
  • 5
  • Are you able to add a code snippet to your main post? It's a very beneficial practice for future questions and further clarifies what you're trying to achieve. You can surround your code snippet with backticks (`) to format it for easy reading. – Retraces Apr 06 '22 at 17:08
0

A skeleton:

import sys
import termios
import fcntl
import os


def getchar():
    fd = sys.stdin.fileno()
    oldatribut = termios.tcgetattr(fd)
    newatribut = termios.tcgetattr(fd)
    newatribut[3] = newatribut[3] & ~termios.ICANON & ~termios.ECHO
    termios.tcsetattr(fd, termios.TCSANOW, newatribut)
    oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
    mychar = None
    try:
        mychar = sys.stdin.read(1)
    except IOError:
        pass
    termios.tcsetattr(fd, termios.TCSAFLUSH, oldatribut)
    fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
    return mychar


def filter():
    my_char = getchar()
    if my_char.isupper() or my_char.isnumeric() or my_char == "\n":
        return my_char
    return ""


def my_input(your_question=""):
    theorem = ""
    print(your_question, end='', flush=True)
    while True:
        mychar = filter()
        if mychar:
            theorem += mychar
            print(mychar, end='', flush=True)
        if mychar == "\n":
            return theorem


my_string = my_input("Only uppercase or digit: ")
print("your answer: " + my_string)

Attention, work only in terminal!

Radek Rojík
  • 104
  • 5