Allow User Input Without Moving to New Line - Python 2.7 - python-2.7

How can I let user enter in a value right after a string? For example, I want the user to be able to enter in
a value after this string on the same line "Please enter in the length of the first side: ". However, the
raw_input() function starts at a new line. How can I continue on the same line?

raw_input takes an argument. So just do
raw_input('Please enter in the length of the first side: ')
and the user will be prompted to input right after the colon.

Related

Python 2.7: user input character range checking

Not sure how to accomplish what I want:
I am first asking the user to input a letter ranging from A to H. Once I grab that input, I want do STUFF if the input was valid, but I'm not sure how to verify that the input was valid.
while True:
input= raw_input("Enter letter between A-H: ")
if input "BETWEEN A-H": # (this is where I'm stuck)
# DO STUFF
break
else:
print "Invalid input. Try again"
continue
You can get the Unicode value of the character (with ord(character)) and check if it is in the desired range.

Check if a word in a sentence is alphanumeric and return the alnum word

I've been working on a project and I've to check if the strings entered by user are alphanumeric or not.
For now, I've built my code and there's one function which needs to check if any word is alphanumeric or not.
The program is to let user enter a sentence along with his license number which would be alphanumeric like '221XBCS'. So, if the user enter suppose- 'My license number is 221124521' instead of 221XBCS I want the program to stop.
But my current program is assuming the re.match condition is true always. WHY IS IT SO??
import re
s = input("Please enter here:")
if re.search(r'\bnumber \b',s):
x = (s.split('number ')[1])
y = x.split()
z = y[0]
print(z)
if re.match('^[\w-]+$', z):
print('true')
else:
print('False')
The output looks like this for now:
Please enter here:my license number is 221
is
true
I want my program to grab alnum value from the input. That's all!
I think I understood your condition: the user should enter his license number which should contain only alphabetic characters AND numbers (both):
With built-in functions:
s = input("Please enter here:")
l_numbers = list(filter(lambda w: not w.isdigit() and not w.isalpha() and w.isalnum(), s.strip().split()))
l_number = l_numbers[0] if l_numbers else ''
print(l_number)
Let's say the user entered My license number is 221XBCS thanks.
The output will be:
221XBCS
With regular expressions, it is often valuable to look at the problem the reverse way.
It is, in this case, better to look if the string is NOT a pure numerical than looking if it IS an alphanumerical.
if re.match('[^\d]', z):
print('The string is no a pure numerical')

Does `cin` produce a newline automatically?

Let's consider the following code:
#include<iostream>
int main()
{
std::cout<<"First-";
std::cout <<"-Second:";
int i;
std::cin>>i;
std::cout<<"Third in a new line.";
while(1){}
}
The output when value 4 is given to i is:
First--Second:4
Third in a newline
cout doesn't print any newline. But after I input any value(4) for i a newline is printed. There could be two possible reasons for this:
The Enter key I press after typing a numerical value for i is printed as a newline.
cin automatically generates a newline.
Although the first reason seems more reasonable but the reason, I am thinking 2nd reason could also be true, is because after Third is printed when I press Enter no new line is printed even the program continue to run because of while(1)--which means the console window doesn't print a newline when Enter key is pressed. So it seems like cin automatically prints a newline.
So, why is the newline being generated after I give input to cin? Does cin automatically prints a newline?
The number and newline you entered is printed by the console software. cin won't print anything in my understanding.
Try giving some input via redirect or pipe, and I guess you will see no new line printed.
For example:
$ echo 4 | ./a.out
First--Second:Third in a new line.
where $ is the prompt and echo 4 | ./a.out(Enter) is your input.
Check this out: http://ideone.com/tBj1uS
You can see there that input and output are separated.
stdin:
1
2
stdout:
First--Second:Third in a new line.
Which means, that the newline is produced by the Enter key and is a part of the input, not the output.
If someone will have the same problem, I was able to solve it like this:
string ans1;
getline(cin, ans1);
ans1.erase(remove(ans1.begin(), ans1.end(), '\n'),
ans1.end());
int ans1int = atoi(ans1.c_str());
Basically, it works by deleting all the newline characters in the string, and then ,aking it integer or whatever else you need. Also you will need algorithm library for it
It's not that elegant, but hey, it works!

Check if string has at least one alphanumeric character

I'm an absolute beginner trying to learn string validation. I have a variable about to store user input:
Text_input = raw_input('Type anything: ')
I want to check if Text_input contains at least one alphanumeric character. (If not, the program should print a message such as "Try again!" and ask the user to type again.) So, typing "A#" should pass but "#" should not. Any suggestions?
This worked for me:
Text_input = raw_input('Type anything: ')
if any(char.isalpha() or char.isdigit() for char in Text_input):
print "Input contains at least one alphanumeric character."
else:
print "Input must contain at least one alphanumeric character."

Why must type getline(cin, string) twice?

need your help in getting user inputs.
I want users to type a string that has spaces.
i cant use cin>>variable as the space in between makes the problem go wrongly.
if i use getline(cin,string_variable) it works correctly. but i need to type twice in order to make it work proberly.
cout<<"Enter movie name";
getline(cin, mvName);
getline(cin, mvName);
Is there a better way to get user input than this or is there any other codes to type rather than typing the getline twice? Pls advice.
When switching between formatted input using in >> value and unformatted input, e.g., using std::getline(in, value) you need to make sure that you have consumed any whitespace you are not interest in. In you case there is probably a newline in the buffer from a prior input. Assuming you are nit interested in leading whitespace the easiest approach is to use something like this:
if (std::getline(std::cin >> std::ws, mvName)) {
process(mvName);
}
BTW, you should always check that your input was successful.
I had no issues using:
char mvName[32];
cin.getline(mvName, 32);
And I only had to call it once, again with no issues.
Maybe you just forget to add \n in your prompt message:
cout<<"Enter movie name:\n";
But if you want skip empty lines - do this:
// skip empty lines
while (cin >> mvName && mvName.empty());
// here mvName contains non empty string or it is empty because of error in reading
....
As the question contains no new-line character I suspect you hit enter to move down from the "Enter movie name" question? This would put a blank line into stdin, which the first getline() would read and then second getline() would read your entered text.
To remove the requirement of typing the initial new-line character just add it to the question's string literal:
std::cout<< "Enter movie name:\n";
cout<<"Enter movie name";
getline(cin, mvName);
Works fine!
Maybe you had to use getline(cin, mvName); twice was because you inputted some character into the first getline(cin, mvName); like Space, Enter, etc.