How to take multiple inputs in a single line in Python? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 days ago.
This post was edited and submitted for review 5 days ago.
Improve this question
I want to be able to take input from the user in a single line and assign it to multiple variables in Python. Specifically, I want the user to input their name and their profession on the same line, and for the text "I am a" to be printed only after the user has entered their name.
Here's an example of the desired input/output format:
Input prompt 1: "My Name is "
""" wait for the input after the first prompt after that is submitted
print prompt for the second input and wait for the second input"""
Input prompt 2: "I am a "
Output:
Name: John Doe # whatever name the user enters
Profession: software developer # whatever profession the user enters
How can I achieve this in Python without the inputs moving to the next line before both are entered?
I have tried using a blank space as a separator but when one input is taken from the user, the next one moves to the next line.
print("My name is ",end='')
name = str(input())
print(" I am a ",end='')
profession = str(input())
This obviously couldn't be achieved using input().split() method because I want the text "I am a" to be printed only after input 1 is submitted

Related

Use try/raise/except statements [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 days ago.
Improve this question
Code project: Registration form
The form has a name field, which should be more than 3 characters long.
Any name that has less than 4 characters is invalid.
Complete the code to take the name as input, and raise an exception if the name is invalid, outputting "Invalid Name". Output "Account Created" if the name is valid.
Sample Input
abc
Sample Output
Invalid Name
please help me out to complete the code.
try:
name = input()
raise ValueError("Invalid Name!")
except:
print("Invalid Name")

Is it possible to let a user input any number of words one per line using the input function? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 months ago.
Improve this question
I was wondering is it possible to let a user enter any number of words one per line using the input function. So for example, let's say I have the following prompt:
Enter any number of words, one per line, or a blank line to stop:
and the output is something like this:
hello
my
name
is
dave
What would the code for this look like?
simply :
while True:
x = input()
if x == '':
break

It's a question from me as I am a beginner in programming ? I want to ask a simple question related to Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
The question is, i executed a program which told me to input a name. Now whenever I input a number as the input instead of letters or a string. The number gets taken as my name. I want the program to tell me to enter a name and not a number whenever I type in a number as my name instead of a string. Can you please help me with this problem in Python. I want a simple code and should not contain any import functions as I don't know about it ! Only simple begineers code please.
Here's my code:
name = input("Enter a name : ")
print("Hello", name)
Whenever I enter a number as input for example 5, then it prints Hello 5 but I want it to print Please Enter A Valid Name whenever I input a number. Please Help !
One very simple solution is to use the built-in function isnumeric(), which returns true if all characters in the string are numeric.
name = input("Enter a name : ")
if name.isnumeric():
print("That's not a name!")
else:
print("Hello", name)
Another approach is to use isalpha(), which returns true if all characters in the string are letters.
name = input("Enter a name : ")
if name.isalpha():
print("Hello", name)
else:
print("That's not a name!)
Be careful though, as this will reject some strings that are still names, such as "John Smith" or "Jean-Pierre"
Once you have a string, you can use regular expressions to find out if it contains undesirable characters, as per the following transcript:
>>> import re
>>> name = input("Name? ")
Name? Pax
>>> if re.search("[0-9]", name):
... print("Has digits")
...
>>> name = input("Name? ")
Name? Bobby27
>>> if re.search("[0-9]", name):
... print("Has digits")
...
Has digits
This particular case checks whether any digits exist in the string but you can make the regular expression arbitrarily complex if you want more targeted checks to be done.

Where would I put the .title function in order for the final output to have the words beginning with a capital letter? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I know this is probably a simple problem but as a beginner I thought I would ask. I have created a simple Python script to run through terminal where, after being asked three questions, the user will be given an output with it all in. I would like to add to this by capitalising all words answered and I know I could use the .title function but im not sure where to put it. any help would be much appreciated.
#ask user for age
name = input('What is your name?: ')
print(name)
#ask user age
age = input('How old are you?: ')
print(age)
#ask user for city
city = input('What city were you born in?: ')
print(city)
#ask user what they enjoy
hobby = input('What do you enjoy doing in your spare time?: ')
print(hobby)
#create output text
string = 'Your name is {} and you are {} years old. you were born in {} and you enjoy {}'
output = string.format(name, age, city, hobby)
#print output to screen
print(output)
You will put it in this line:
output = string.format(name.title(), age.title(), city.title(), hobby.title())
This is pretty straight forward since this is what you add to the final output.
You could also add it straight after asking for input like this:
my_name = input('Enter your name:').title()

User input into array - python 3 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hi there i am trying to put a user input into an array, so far i have:
#Decrypt string
def decrypt():
print("Please enter code to be decrypted.")
text = input(">>>")
print("Please enter your key used to encrypt the data.")
key = int(input(">>>"))
#Put input into array
#????
I am tring to get the input and put it in an array so that it can be referenced using
chr(text[1])
To convert it into plain text from ascii (Basic encryption and decryption).
I have found a few posts on this but they are outdated (for python2 etc...).
Thanks!
If you just want to have an indexable list to store user inputs as they come in, you can use the built-in list class and its append method:
keys = list();
texts = list();
def decrypt():
print("Please enter code to be decrypted.")
text = input(">>>")
print("Please enter your key used to encrypt the data.")
key = int(input(">>>"))
texts.append(text)
keys.append(key)
Now, texts[n] will return the nth text value entered by your user.

Categories