Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How can i restrict the number of characters in an input?
#1
Hello, world!

So, i wanna restrict the number of characters in an input, for exemple:

num1 = int(input("Type your age: ")) #here i want the user can only type 3 or < characters
Quote
#2
If I were going to do this, I'd check the length of the return value of input() (or the size of num1) and then re-prompt the user on rejection. If you are really serious about limiting the number of typed characters... I'm not as sure. I'd look at the curses module, or consider a GUI of some kind (likely a web server, like Flask, or Kivy since it would be easier to get working on Android). Other options would be platform-specific, which you might be fine with but I wouldn't be sure how to go about it.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Find Average of User Input Defined number of Scores DustinKlent 1 207 Oct-25-2019, 12:40 AM
Last Post: Larz60+
  Restrict / Limit variable size mln4python 4 422 Aug-13-2019, 07:17 AM
Last Post: mln4python
  Split Column Text by Number of Characters cgoldstein 3 504 Mar-11-2019, 01:45 PM
Last Post: perfringo
  How to format characters that are given by user input with user input i1928 1 524 Feb-28-2019, 03:25 AM
Last Post: ichabod801
  Counting number of characters in a string Drone4four 1 828 Aug-16-2018, 02:33 PM
Last Post: ichabod801
  Regex: How to say 'any number of characters of any type until x'? JoeB 2 825 Jan-24-2018, 03:30 PM
Last Post: Mekire
  The number of occurrences of statistical characters RobertW 3 1,850 Jan-18-2018, 10:10 PM
Last Post: nilamo
  Number of characters ejstik 9 1,157 Jan-11-2018, 12:20 PM
Last Post: Larz60+
  Guess Random Number Why i m not able to enter input Nithya Thiyagarajan 6 3,697 Jan-07-2018, 04:26 AM
Last Post: squenson
  Beginner question: help ensuring input is a number ycrad 2 1,066 Dec-23-2017, 10:49 PM
Last Post: snippsat

Forum Jump:


Users browsing this thread: 1 Guest(s)