This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 4 years ago.
When i am tying to take user input in python then it is taking input in next line but I want ti to take input in same line. How to achieve that?
I am taking input like this
print("Enter your name:",end=" ")
It is showing on console as
Enter your name:
Ankit
but I want it as
Enter your name:Ankit
You need to use the input method:
response = input("Enter your name:")
(or raw_input for python 2)
By using the input() method
Just type,
userinput = input("Enter your name: ")
If you are using Python 2.x:
response = raw_input("Enter your name:")
If you are using Python 3.x:
response = input("Enter your name:")
Alternate solution:
For python 2.x:
print("Enter your name:"),
response = raw_input()
For python 3.x:
print("Enter your name:", end="")
response = input()
Related
I have a txt file with names of people.
I open it and want to get only the names with the length the user entered, using the filter and lambda functions.
The problem is that the list I get is empty [].
names_file = open('names.txt').read().split()
user_choice = input("Enter name length: ")
print(list(filter(lambda c : len(c) == user_choice, names_file)))
What is the problem ?
See this line
user_choice = input("Enter name length: ")
You are taking a string input. If you want to take an integer input you need to write int(input()). I hope this will solve the problem.
user_choice = int(input("Enter name length: "))
should fix it.
This question already has answers here:
Python: "Print" and "Input" in one line [duplicate]
(3 answers)
Closed 2 years ago.
Completely new to coding and this site, so I know this is going to seem like a silly or extremely obvious question. I've tried looking on the site for answers, but I don't know if the answers already given would work on this, or I simply don't know how to input the answers given into my code.
I want the "years old" print to appear right after you input the age, but on the same line.
name = input ("Hello, my name is ")
age = input("I am ")
print ("years old")
job = input("I am a ")
Maybe you want something like this:
name = input("Enter your name : ")
age = input("Enter your age : ")
print(f"My name is {name} and I am {age} years old")
Try something like
name = input ("Name: ")
age = input("Age: ")
print ("Hello, My name is {}, I am {} years old".format(name, age))
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 attempting to accept user input and check the string for non-alphabet values. My problem is if they enter a bad value, how do I query them again and start the loop over? See below
name = str(input("Enter name:"))
for i in name:
if not i.isalpha():
name = str(input("Enter name:")
**line to start iterating from the beginning with new entry.**
Just trying to verify users only enter letters. If the check fails they enter the name again and it starts over. Thanks in advance!
You can do something like this:
correct = False
while correct == False:
name = str(input("Enter name:"))
for i in name:
if not i.isalpha():
correct = False
break
else:
correct = True
You can see below an example code:
while True:
number_found = False
name = str(input("Enter name:"))
for i in name:
print("Check {} character".format(i))
if i.isdigit():
print("{} is number. Try again.".format(i))
number_found = True
break # Break the for loop when you find the first non-alpha. You can reduce the run-time with this solution.
if not number_found:
break
print("Correct input {}".format(name))
Output:
>>> python3 test.py # Success case
Enter name:test
Check t character
Check e character
Check s character
Check t character
Correct input test
>>> python3 test.py # Failed case
Enter name:test555
Check t character
Check e character
Check s character
Check t character
Check 5 character
5 is number. Try again.
Enter name:
I have a simple Python question that I'm having brain freeze on. This code snippet works. But when I substitue "258 494-3929" with phoneNumber, I get the following error below:
# Compare phone number
phone_pattern = '^\d{3} ?\d{3}-\d{4}$'
# phoneNumber = str(input("Please enter a phone number: "))
if re.search(phone_pattern, "258 494-3929"):
print "Pattern matches"
else:
print "Pattern doesn't match!"
Pattern does not match
Please enter a phone number: 258 494-3929
Traceback (most recent call last):
File "pattern_match.py", line 16, in <module>
phoneNumber = str(input("Please enter a phone number: "))
File "<string>", line 1
258 494-3929
^
SyntaxError: invalid syntax
C:\Users\Developer\Documents\PythonDemo>
By the way, I did import re and tried using rstrip in case of the \n
What else could I be missing?
You should use raw_input instead of input, and you don't have to call str, because this function returns a string itself:
phoneNumber = raw_input("Please enter a phone number: ")
In Python version 2.x, input() does two things:
Reads a string of data. (You want this.)
Then it evaluates the string of data as if it were a Python expression. (This part is causing the error.)
The function raw_input() is better in this situation because it does #1 above but not #2.
If you change:
input("Please enter a phone number: ")
to read:
raw_input("Please enter a phone number: ")
you'll eliminate the error of the phone number not being a valid Python expression.
The input() function has tripped up so many people learning Python that starting with Python versions 3.x, the designers of the language removed the extra evaluation step. This makes input() in versions 3.x behave the same as raw_input() in versions 2.x.
See also a helpful wikibooks article.
The input() function actually evaluates the input that's typed into it:
>>> print str(input("input: "))
input: 258238
258238
>>> print str(input("input: "))
input: 3**3 + 4
31
It's trying to evaluate '258 494-3929' which is invalid Python.
Use sys.stdin.readline().strip() to do your read.
input() calls eval(raw_input(prompt)), so you want phoneNumber = raw_input("Please enter a phone number: ").strip()
See also http://docs.python.org/library/functions.html#input and http://docs.python.org/library/functions.html#raw_input
I am very new to Python programming (15 minutes) I wanted to make a simple program that would take an input and then print it back out. This is how my code looks.
Number = raw_input("Enter a number")
print Number
How can I make it so a new line follows. I read about using \n but when I tried:
Number = raw_input("Enter a number")\n
print Number
It didn't work.
Put it inside of the quotes:
Number = raw_input("Enter a number\n")
\n is a control character, sort of like a key on the keyboard that you cannot press.
You could also use triple quotes and make a multi-line string:
Number = raw_input("""Enter a number
""")
If you want the input to be on its own line then you could also just
print "Enter a number"
Number = raw_input()
I do this:
print("What is your name?")
name = input("")
print("Hello" , name + "!")
So when I run it and type Bob the whole thing would look like:
What is your name?
Bob
Hello Bob!
# use the print function to ask the question:
print("What is your name?")
# assign the variable name to the input function. It will show in a new line.
your_name = input("")
# repeat for any other variables as needed
It will also work with: your_name = input("What is your name?\n")
in python 3:
#!/usr/bin/python3.7
'''
Read list of numbers and print it
'''
def enter_num():
i = input("Enter the numbers \n")
for a in range(len(i)):
print i[a]
if __name__ == "__main__":
enter_num()
In the python3 this is the following way to take input from user:
For the string:
s=input()
For the integer:
x=int(input())
Taking more than one integer value in the same line (like array):
a=list(map(int,input().split()))