Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I dont understand why this is happening.. It is not happening with anyone else I know.... Here is my code so far
# creating a string which will be our main sentence
string = input("Please enter a sentence.\n")
str(string)
# creates a list and then splits the string up and puts all the parts into a list
stringlist = []
string.lower()
string = string.split()
stringlist.append(string)
# prints the list to check for any errors during splitting
print(stringlist)
find = input("Which word would you like to find?\n")
while find in stringlist:
index = stringlist.index(find)
stringlist(index) = ""
indexpositions.append(str(index + 1))
What I am trying to do is to find a word in a sentence and find all the indexes of it.
You're using the wrong parentheses to index stringlist. You should do stringlist[index]=""
List of problems I could find
In Python, you need to use [] access the elements of a list. So you need to change your code to
stringlist[index] = ""
indexpositions.append(str[index + 1])
Apart from that,
str(string)
...
string.lower()
are NOOPs. First line simply converts the string to a string object and discards it immediately and the second simply converts the string to lower case string and returns a new string object and that is also ignored. Probably you meant
string = str(string)
...
string = string.lower()
Also, the str(string) part is not necessary, because input function returns a string object only.
Another problem is, string.find, returns -1 if the item is not found in the list. In Python sequences can have negative indexes. So, you may want to be aware of that case as well.
So your code can be written like this
stringlist = input("Please enter a sentence.\n").lower().split()
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I am wanting to rearrange strings in python, but don understand what functions would be useful in doing so? Can someone help me understand how do this: remove the first letter of a word and place that letter at the end of the word. Then you append "IE" to the end of the word. I would appreciate it, if someone can link to the functions that might be used, so I can learn how they work.
EDIT: I have tried to make this work with a phrase, but I am having issues with getting it to the ie to go at the end of the words. For example HankTIE ouYIE would be the output of the input Thank You.
Here is what I have:
string = input("Please input a word: ")
def silly_encrypter(string):
words = string.split()
for words in string:
first_letter= words[1:] + words[0]
ie_end = first_letter + "IE"
print (ie_end)
silly_encrypter(string)
As other users pointed out, I strongly suggest you to read Python tutorial, which is very friendly and contains a lot of examples that you can try out in your python console.
Having said that, you could take advantage of string indexing plus concatenation to accomplish the things you want (Both things are mentioned in the tutorial):
remove the first letter of a word and place that letter at the end of the word:
s = "myString"
first_letter_at_the_end = s[1:] + s[0]
# If you print `first_at_the_end` you'll get: 'yStringm'
then append "IE" at the end
ie_at_the_end = first_letter_at_the_end + "IE"
# If you print `ie_at_the_end` you'll get: 'yStringmIE'
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I decided to take on some programming projects and decided to follow this guide:
https://www.dreamincode.net/forums/topic/78802-martyr2s-mega-project-ideas-list/
I started with the first project, which is to reverse a string, pretty simple stuff as you can find out how to do that in the documentation. IE.
txt = "Hello World"[::-1]
print(txt)
dlroW olleH
s = "health" [::-1]
print(s)
htlaeh
And So on... There's two questions really. In this guide i'm not to sure if they just wanted you to reverse the string. It seems like they want Input? For the user to Enter the String.
In this case i thought it might be something like this:
string = Input("Enter a string")
print(s [::-1])
How ever this did not work, Not to sure how to implement this in Python 3.8.2. Could someone let me know and how they did it that would be great.
Thank you.
Your code has a space between s and the indices you want. You also have two variable names, s and string. Choose one. Finally, Input must be lowercase. Change it to this:
string = input("Enter a string")
print(string[::-1])
Your print statement needs some modifications.
Here it goes-
print(string[: :-1])
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
The following is my code:
string1 = (input("What is your name?")) #creates a string, stores the name in it
first1 = string1[:1]
string2 = (input("What is your last name?"))
first3 = string2[:3]
from random import randint
sentence = "".join((str(randint(0,9)), first1.lower(), first3.upper()))
print (sentence)
sentence = "".join((str(randint(0,9)), first1.lower(), first3.upper()))
print (sentence)
It works, but I am having some trouble. I need to loop this 5 times - but it doesn't work for some reason!
P.S. Python 3!
You are creating a tuple called sentence, rather than a string
If you change that line to this:
sentence = "".join((str(randint(0,9)), first1.lower(), first3.upper()))
It will create a string that has no gaps, like so when printed:
What is your name?First
What is your last name?Last
5fLAS
You are creating a list, not a string so it seems logical that you get issues when trying to print it...
To append to a string you can do that :
sentence = str(randint(0,9))+(first1.lower())+(first3.upper())
In Python, you don't give a type to your variables, it depends of what you put INTO them.
In Python, elements between parenthesis "()" are considered as TUPLE (a type similar to a list, but that cant be modified), "[]" stands for a list, "{}" stands for a dictionnary. So you must NOT put parts of a string between parenthesis and separated with commas or you will turn them into a list of words. Instead you can use "+" that means to Python that you are making a string concatenation. Also note that i casted your random number into string. If you give 3 strings separated by "+" Python will automatically detect sentence as a String. Wp you've created a string !
If you have a list and want to get rid of the spaces you can also use :
"".join(yourlist)
However i don't recommend it here, you would be creating a list and using a join for nothing since you can create a string from the begining.
Edit: As for the loop, we can't answer if you don't copy paste the code of the loop and the error, it could be a million things.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
i'm trying to check if the input is in a set. I get the set from a words.dat file. the set is correct, but the else statement always gets executed. This is my code:
import fileinput
def words2array(words):
woorden = set([])
for line in fileinput.input(words):
woorden.add(line)
return woorden
def krijgInput():
input = raw_input().upper()
input += "/n"
return input
woordSet = words2array("words.dat")
input = krijgInput()
if 'input' in woordSet:
print "Ja"
else:
print "Nee"
print input
print woordSet
This is a part of the set, I can't post the full set because it has around 80k-120k words.
'SPOUT\n', 'BLASTOMA\n', 'HINDGUT\n', 'FORGOER\n', 'LEOPARDS\n', 'SPECULUM\n', 'KEROSENES\n', 'CARIED\n', 'SOUBISES\n', 'ANIMALS\n', 'DISSERTS\n', 'OMISSIVE\n', 'VIREMIC\n', 'FLUSTERING\n', 'PLENUMS\n', 'VACUITY\n',
this is a little bit from words.dat:
AARDVARK
AARDVARKS
AARDWOLF
AARDWOLVES
AAS
AASVOGEL
AASVOGELS
ABA
ABACA
ABACAS
ABACI
ABACK
ABACUS
ABACUSES
ABAFT
You are checking for the literal string 'input', not the variable input, which you managed to use properly several times.
Also, don't use input as a variable name, as it masks the built-in.
And compare /n to 'BLASTOMA\n' - / is different from \. Use \n.
And here's an easier way to do this:
with open('words.dat') as f:
if raw_input().upper()+'\n' in set(f):
print 'Ja'
else:
print 'Nee'
Note that that depends on having an "empty" line at the end of the file. If the last line is just the last word without a newline, use if raw_input().upper() in set(map(str.strip, f)) instead.
You need to strip the "\n"s: from the lines that you retrieve from the file:
for line in fileinput.input(words):
woorden.add(line.strip('\n')) # strip
& change
if 'input' in woordSet: # check if string in set, wrong
to
if input in woordSet: # check if input in set
& as #grc mentioned in his comment below, in order for this to work, you need to drop this line
input += "/n"
so as to get the neat set of elements that you want.
you are trying to search "input" as string in word set.
Please change below lines as:
input = krijgInput()
if input in woordSet:
Here you will search user input from set.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm probably missing something very basic here, but here goes:
I'm using Python 2.7 and regex to identify digits within a string.
In the console, I type in:
>>> newstr = 'NukeNews/File_132.txt'
>>> int(re.findall(r'\d+',newstr)[0])
132
Which is what I expect.
However, in the script I'm running, I have the strings stored in a dictionary, linedict. I'm running this script:
news_id=[]
for line in line_vec:
print linedict[line]
newstr= linedict[line]
id_int = re.findall('r\d+',newstr)
print id_int
news_id.append(id_int)
It's a long list, but the output looks like:
NukeNews/File_132.txt
[]
So - the correct string is registered, but it's not matching on anything.
I was calling the first item in the list earlier (to match the console input of int(re.findall(r'\d+',newstr)[0]), but the script is telling me that the regex didn't find any instances of the digits in the string. I would expect this to return:
NukeNews/File_132.txt
['132']
Any idea why it's not working as expected? When I try running re.match(r'/d+',newstr) I also get an empty group (following the groups example on https://docs.python.org/2/library/re.html).
Edit: As pointed out, this is a case of not being careful with 'r' and r'*'. I'm just going to leave this up in case anyone else googling "why does my regex work in console but not in script" forgets to check this typo, like I did.
You've got your r inside the quotes so instead of getting a "raw string" you're getting a string with an 'r' in it ...
id_int = re.findall('r\d+',newstr)
# ^
# should be:
id_int = re.findall(r'\d+',newstr)
your "console" version also only takes the first of the found matches compared to your "script" version which appends the entire list.