Creating a list of names base on user input() - python

I've been trying to find a way to transform users' name inputs into a python list, so I could later process the information. However, since I'm not very experienced, I can't find a way to do such a thing. When I write the code below, the print() output, is only the second letter of the first name, while I actually intend it to be the second name on the list. So, my question is, how can I make it so that each different name is a different element in the list?
# Creates a list of the players
players = input("Insert players\n")
list(players)
print(players[1])

Use split() to turn a string into a list of words:
players = input("Insert players\n").split()
print(players[1]) # prints the second player name in the list
By default split() splits on whitespace, but it takes an optional argument that allows you to split on other strings (e.g. split(",") to split on commas).

Related

Looping through substrings of a string against a dictionary

Please be gentle - I am new to this and English is not my first language. For a school project, the assignment is to create a program that allows the user to input text for a ticket and the result for who it should be routed to. Below is the code I have so far. It works fine for single word keywords, but not for keywords with two or more words. I don't necessarily need the answer as much as a push in the right direction. We are not supposed to use things we haven't learned yet so that limits me to some very basic functionality - lists, dictionaries, simple loops, etc.
If multiple keywords are in the input, only the keyword found at the highest position in the table is used for routing.
The program needs to use keywords as case insensitive.
If there is no keyword present, it should be routed to "Next Available Technician"
The goal is to have output that is formatted exactly as such:
Also I think I am suppose to use the .find() function, but I am not sure how it would be implemented.
Loop over the techRouting dictionary. Check if the keyword is in the text, and if it is, use the corresponding routed to value.
c = ''
for keyword in techRouting:
if keyword in ticket_text:
c = keyword
break
There is a couple of issues here:
The way of extracting keywords from input
ticketText = ticketText.split(" ")
gives you a list, consisting of a parts, that had whitespace " " inbetween.
In other words, ticketText breaks to a separate words, and none
of words contains a space.
In [61]: ticketText = "Password doesn't work"
...: ticketKeywords = ticketText.split(" ")
...: print(ticketKeywords)
results to
['Password', "doesn't", 'work']
Keywords from list should also be lowercased before comparison. As well, as the input.
You probably should not split the input into keywords at all. Just use keyword in inputText to check, if inputText contains the whole keyword as a substring.

I need to ask the user to enter a quote. Split the quote into individual words by the spaces. Display each word on a new line

quote=input("Enter a quote ")
split=quote.split(quote)
for count in range(0,(split)+1):
print(split)
I've tried to do this but gave me the error:
for count in range(0,(split)+1):
TypeError: can only concatenate list (not "int") to list
You are receiving the error because your split variable is a list and you are adding + 1 (which is an integer) to the list which you cannot do in Python, hence a TypeError is thrown because the two types are not compatible with the + operator and Python doesn't know what to do.
Fixing the error
There are a few issues with the code that lead to that error being thrown and some minor logical issues:
You need to make sure you are splitting the string by spaces, not by the string itself.
You also need to get the length of the list of words in the string in your for loop.
In the loop you need to make sure you are outputting each word, not the whole list
See the code below for more details:
quote=input("Enter a quote ")
# Make sure to split by " " character
split=quote.split(" ")
# Make sure to get the length of the list of words in the split variable
for count in range(0, len(split)):
# Print each word, not the whole array
print(split[count])
Hope that helps ;)

How can i use the "list" function and get all items in a single index on python?

So i'm working on this really long program, and i want it to save an input inside of a new list, for that i have tried doing:
thing=list(input("say something")) #hello
print(thing)
#[h,e,l,l,o]
how can i arrange it to get [hello] instead?
Offhand, I'd say the easiest would be to initialize thing with an empty list and then append the user's input to it:
thing = []
thing.append(input("say something: "))
Use:
thing = [input("say something")]
In your version "hello" is treated as an iterable, which all Python strings are. A list then gets created with individual characters as items from that iterable (see docs on list()). If you want a list with the whole string as the only item, you have to do it using the square bracket syntax.

Python: joining multiple lines into a single line/string and appending it to a list

Basically I have a text file, I am reading it line by line. I want to merge some lines together (a part of the text) in to a single string and add it as an element to a list.
These parts of the text that I want to combine start with the letters "gi" and end with ">". I can successfully isolate this part of the text but I am having trouble manipulating with it in any way, i would like it to be a single variable, acting like a individual entity. So far it is only adding single lines to the list.
def lines(File):
dataFile = open(File)
list =[]
for letters in dataFile:
start = letters.find("gi") + 2
end = letters.find(">", start)
unit = letters[start:end]
list.append(unit)
return list
This is an example:
https://www.dropbox.com/s/1cwv2spfcpp0q0s/pythonmafft.txt?dl=0
So every entry that is in the file I would like to manipulate as a single string and be able to append it to a list. Every entry is seperated by a few empty lines.
First off, don't use list as a variable name. list is a builtin and you override it each time you assign the same name elsewhere in your code. Try to use more descriptive names in general and you'll easily avoid this pitfall.
There is an easier way to do what you're asking, since '>gi' (in the example you gave) is placed together. You can simply use split and it'll give you the units (without '>gi').
def lines(File):
dataFile = open(File)
wordlist = dataFile.read().split('>gi')
return wordlist

python idle how to create username?

Write a function called getUsername which takes two input parameters, firstname (string) and surname (string), and both returns and prints a username made up of the first character of the firstname and the first four characters of the surname. Assume that the given parameters always have at least four characters.
First, you will want to make a function. (Please note any syntax I use will be for V2.7)
def makeUsername(firstName,lastName):
Next, I would suggest a string to store the username that you will make. You only want the first character and then the first 4. Note the first and last names will be whatever you name the parameters.
x = firstName[0] + lastName[:4]
Finally, printing the string and returning the string.
print x
return x
Then, when you call the function it will look something like this:
makeUsername('John', 'Smith')
If you have any more questions, just ask!
This is how you build the string: firstname[0] + surname[:4]

Categories