Stop input function in python [duplicate] - python

This question already has answers here:
raw_input without pressing enter
(11 answers)
Closed 8 years ago.
I would like to stop the user to type in too many characters in the input function so it just stops the input() when too many characters are put in but the characters previously typed in stay. In this case I wouln't want to check if this is after the user has pressed enter but I would like to interrupt the function. Is this somehow possible?

Might be possible with a loop and msvcrt. Here is an example:
while len(string) < 10:
#Whatever length you want instead of 10
string += msvcrt.getch()

I don't believe this is possible, but if you're concerned about a maximum length (for a database value or something similar) you can use slicing to address excessively long entries:
maxlen = 256
userval = input("Enter value (max 256):")[:maxlen]

Related

How to run a statement again and again on a case but on different occurrences [duplicate]

This question already has answers here:
Replace characters in string from dictionary mapping
(2 answers)
Closed 2 years ago.
Hey guys I am making an encoding system in which each letter gets converted into predefined gibberish.
For example, 'a' has already been set as 'ashgahsjahjs'.
But using if a in data: print("ashgahsjahjs") executes this for one time only, if there are more than one A in the word, it would not print them with gibberish.
Using a while loop does not work either as it keeps printing indefinitely, so is there a way to print the gibberish each time there is a new occurrence of a letter.
you could try indexing the string.
your_string = "are you an apple?"
for i in range(len(your_string)):
if "a" == your_string[i]:
print("Found a at position {pos}".format(pos=i))
else:
print("Nope")

Python 3: go back to start of if statement if string longer than [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 3 years ago.
I'm very new to python and code in general and I'm wondering how i can do something like this:
User enters string, computer checks if string is longer then 10 characters and if it is it asks for the user to enter a new string. This is what i have right now.
usernamelength = (len(username))
if usernamelength > 10:
return
else:
print("Hello, %s. Placeholder text." %username)
Sorry if i'm missing out something obvious, as i said earlier I'm new to this. Any help appreciated :)
This is a good place for a while loop instead of that if statement:
while usernamelength > 10:
# ask for the username again
# usernamelength = len(newUsername)
That way you'll just continue to prompt for the username until you're given something over 10 chars.

How to take multiple inputs from a user in Python [duplicate]

This question already has answers here:
How to read user input until EOF?
(4 answers)
How do I read from stdin?
(25 answers)
Closed 17 days ago.
I'm trying to create a program where python will take multiple inputs until the user enters "END". Each input must be on a different line. I'm still new and learning python/programming in general so is there any way to do this? and how would I turn the inputs into a list?
What a demo might look like -
What are your hobbies?
painting
hiking
drawing
END
You can read input with the input() function. You want to read till 'END' is entered by the user. So, read the input and check whether it is 'END'. If not append it to the list else exit the loop.
In codes you can do like this.
inputs = [] # list to store the inputs
while True: # looping forever
data = input('Enter the data : ') # read the data from user to variable data
if data == 'END': # if END is read then exit the loop
break
else:
inputs.append(data) # otherwise, append the input to the list
print(inputs) # display the list.
Hope this helps.!!

How to re-prompt user input after incorrect type of data is given in Python 3.6 [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 5 years ago.
This question has not been asked in relation to Python 3.6 .
I need a solution to re-prompt a specific user input question in a series of input questions if data is given as anything else but an integer or float in this case.
Lets say they input the correct float data for the first question, but then lets say that input a string character into the second question. That outputs a "ValueError: could not convert string to float: ".
Is there a way using looping or another method to re-prompt the SECOND input question that they failed to put integer/float data in? Furthermore, can you re-prompt only the second question instead of having to start over and re-prompt to the FIRST question?
counter = 0
counter += float(input("What is number 1?"))
counter += float(input("What is number 2?"))
counter += float(input("What is number 3?"))
print(counter)
EDIT: I did read the posted articles containing 9 answers which is similar, but non of them dealt with multiple input questions one after another. The provided answered were helpful, but I still don't quite get how to re-prompt the 2nd or 3rd question after an incorrect data type is entered. In summary: I would like the program to re-prompt the question that failed rather than having the user have to begin at question 1.
Something like this pseudo-code would work:
for q in questions:
while True:
ask_question
if question_result_validated:
break

How to code Python to accept only integers? [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 8 years ago.
I'm working on a Python 3.4 coding assignment that needs an integer input from the user. I am having trouble figuring out how to make the program accept only integer values; for instance, if the user inputs a float (i.e. "9.5"), the program will output, "That's not an integer! Try again."
Simple, use raw_input to get the string input, then call .isdigit() on it to see if it is an int. If it is, cast it to an int, then check it is within the valid range. Stick it all in a while loop so it keeps being called until a valid number is input, and you're all set.

Categories