I need help figuring out how to turn a simple user input like
a = input('Enternumber: ')
and if the user was to input say...
hello bob Jeff Lexi Ava
How am I supposed to have the computer turn that into a list like,
hello
bob
Jeff
Lexi
Ava
If someone has the code could they please explain what they are doing. *This is python
Use the split method.
my_string = 'hello bob Jeff Lexi Ava'
print(my_string.split()) # ['hello', 'bob', 'Jeff', 'Lexi', 'Ava']
To print each on a line:
for word in my_string:
print(word)
Related
I am still learning coding so please forgive me if this is basic. I have a set of code that asks the user how many users it wants to input (x) and asks for basic information about all X of those students (first and last name, age). However, when I print it out it all comes out as one long line. I have seen a few ways to print each character to a new line but not each new set of data. I would like to separate it by user. What I currently have is:
Name: Age:
Tiger woods 40, karen woods 33, charlie brown 44
What I would like it:
Name: Age:
Tiger Woods 40
Karen Woods 33
Charlie Brown 44
This is the code I am currently working with:
list.append(firstname)
list.append(lastname)
list.append(age)
print("name: age:")
print(", ".join(map(str, list)))
The simplest way is to use \n in a string to start a new line. For example, if you have this print statement:
print("Hello world!\nHow are you today?")
It would display as:
Hello world!
How are you today?
Thus, you should simply be able to replace this:
print(", ".join(map(str, list)))
with this:
print("\n".join(map(str, list)))
or something of that nature.
Try using a list of touples instead a list of words
list.append((firstname,lastname,age))
edited
sorry, you can access the data through a subindex
you_list[0] #for firstname
you_list[2] #for age
I have two lists
a = ["hi", "hello", "hey"]
b = ["Sam", "dean"]
and a dataframe which contains a column ques
df = pd.DataFrame({'ques':["<input1> This is <input2>", "<input1> Sir, Do you know <input2>?"]})
I want to replace <input1> by elements of list a and <input2> by elements of list b and create a set of unique questions.
So my expected output is:
['hi This is Sam',
'hi This is dean',
'hello This is Sam',
'hello This is dean',
'hey This is Sam',
'hey This is dean',
'hi Sir, Do you know Sam?',
'hi Sir, Do you know dean?',
'hello Sir, Do you know Sam?',
'hello Sir, Do you know dean?',
'hey Sir, Do you know Sam?',
'hey Sir, Do you know dean?']
I am fine with either list or pandas column.
What I tried
from itertools import product
c = list(product(a,b))
ques = []
for q in df['ques']:
for i in c:
temp = q.replace("<input1>", i[0]).replace("<input2>", i[1])
ques.append(temp)
Which giving me expected result but my data is too large, so I am looking for more efficient solution.
You can do this with a combination of product and replace:
dfs = [
df.replace({'ques': {'<input1>': x, '<input2>': y}}, regex=True)
for x, y in itertools.product(a, b)
]
pd.concat(dfs, ignore_index=True)
ques
0 hi This is Sam
1 hi Sir, Do you know Sam?
2 hi This is dean
3 hi Sir, Do you know dean?
4 hello This is Sam
5 hello Sir, Do you know Sam?
6 hello This is dean
7 hello Sir, Do you know dean?
8 hey This is Sam
9 hey Sir, Do you know Sam?
10 hey This is dean
11 hey Sir, Do you know dean?
i need to translate from one to another language . what did i do wrong ?
language={}
language = {"Bounjour" : 'Hello',
"Comment allez vous?" : 'How are you?',
"Aurevoir" : 'Good Bye'
#User input
print 'Bounjour, Comment Allez vous, Aurevoir'
phrase = raw_input('Please enter a phrase to translate: ')
#result
print "Your sentence in English: ",
for phrase in language:
translates = language[words]
print translates
I see three errors:
The user's input is saved in a variable named phrase, but then the for loop uses that same variable as its iterator, so the user input is discarded.
words is not defined anywhere.
translates is not defined anywhere.
But beyond those errors, you don't even need a loop; just print language[phrase].
I have two separate lists of users (a person's name) and email addresses. How do I modify this
message = """To:
Hey,
How is the weather?"""
to print without changing
print(message)
Basically I'm looking for it to print out something similar to this without modifying the print(message).
To: bob#gmail.com
Hey, Bob
How is the weather?
I'm sorry if this sounds dumb, but I feel like I'm just beating my head against a wall here and cannot figure it out.
You can use str.format() as a means of string interpolation.
email = "bob#gmail.com"
name = "Bob"
message = """To: {}
From: {}
How's the weather""".format(email, name)
print(message)
The above code will output:
To: bob#gmail.com
From: Bob
How's the weather
Here is one way of achieving the desired result
email = "bob#gmail.com"
user = "Bob"
message = """To: {} \nFrom: {} \nHow is the weather?""".format(email, name)
print(message)
To: bob#gmail.com
From: Bob
How is the weather?
But I think you may be interested in the input function that creates a text box and you fill it. Here is how it works.
input('Do you want to write a message? : ')
input('Tell us who is receiving the message: ')
input('Now write your message in this box: ')
Here are the answers that I put in ['Yes', 'John', 'Hi John, this is Samuel Nde.']. The output is below.
Do you want to write a message? : Yes
Tell us who is receiving the message: John
Now write your message in this box: Hi John, This is Samuel Nde.
'Hi John, This is Samuel Nde.'
I have a large text in which three people talking.
I read that text to a string variable in python.
Text is like
JOHN: hello
MIKE: hello john
SARAH: hello guys
Imagine a long talk between 3 people. I want to split the texts into lists like
john = []
mike = []
sarah = []
and I want the list john to contain every sentence john said.
Can anyone help me with the code I need?
See if this is enough to get you started.
for line in text:
if line.startswith('JOHN'):
john.append(line)
elif line.startswith('MIKE'):
mike.append(line)
elif line.startswith('SARAH'):
sarah.append(line)