How to select certain characters in a string in Python? - python

My name is Shaun. I am 13 years old and trying to learn python.
I am trying to make a program that finds vowels in an input and then prints how many vowels there are in the input the user gives.
Here is the code:
s = (input('Enter a string: ')) # Let the user give an input (has to be a string)
Vwl = [] # Create an array where we will append the values when the program finds a vowel or several vowels
for i in s: # Create a loop to check for each letter in s
count_a = 0 # Create a variable to count how many vowels in a
count_e = 0 # Create a variable to count how many vowels in e
count_i = 0 # Create a variable to count how many vowels in i
count_o = 0 # Create a variable to count how many vowels in o
count_u = 0 # Create a variable to count how many vowels in u
The function below is pretty long to explain, so summary of the function below is to find a vowel in s (the input) and make one of the counters, if not some or all, increase by 1. For the sake of learning, we append the vowels in the array Vwl. Then, it prints out Vwl and how many letters there are in the list by using len.
if s.find("a" or "A") != -1:
count_a = count_a + 1
Vwl.append('a')
elif s.find("e" or "E") != -1:
count_e = count_e + 1
Vwl.append("e")
elif s.find("i" or "I") != -1:
count_i = count_i + 1
Vwl.append("i")
elif s.find("o" or "O") != -1:
count_o = count_o + 1
Vwl.append("o")
elif s.find("u" or "U") != -1:
count_u = count_u + 1
Vwl.append("u")
print(Vwl)
print(f"How many vowels in the sentence: {len(Vwl)}")
For some odd reason however, my program first finds the first vowel it sees, and converts the whole string into the first vowel it finds. Then it prints down the wrong amount of vowels based on the array of vowels in the array Vwls
Could someone please help me?

The reason your code only prints out the first vowel is because the if statements you coded are not inside a loop, that part of the code runs once and then it finishes, so it only manages to find one vowel.
Here are couple ways you can do what you are trying to do:
Way 1: Here is if you just want to count the vowels:
s = input()
vowel_counter = 0
for letter in s:
if letter in "aeiou":
vowel_counter+=1
print(f"How many vowels in the sentence: {vowel_counter}")
Way 2: Use a python dictionary to keep track of how many of each vowel you have
s = input()
vowel_dict = {}
for letter in s:
if letter in "aeiou":
if letter not in vowel_dict:
vowel_dict[letter]=0
vowel_dict[letter]+=1
print(f"How many vowels in the sentence: {sum(vowel_dict.values())}")
print(vowel_dict)

Related

Compare two strings, count letters in the right position, then count letters contained in word but in wrong position

I'm working on a game where the computer picks a random word from a list. The user then guess the word from input. The application shall then return +1 for every letter in the correct position and +1 for any letter in the word but not in the correct position. It should only count the letter in the right position once and should not be duplicated when it checks if the letter is in the word. This is my code so far:
def comparison(self) :
word_letters = []
guess_letters = []
correct_position = 0
correct_letter = 0
for l in self.guess :
guess_letters.append(l)
for l in self.word :
word_letters.append(l)
i = 0
for l in word_letters :
if l == guess_letters[i] :
guess_letters.pop(i)
correct_position += 1
i -= 1
i += 1
for l in guess_letters :
if l in word_letters :
correct_letter += 1
print(
f"{correct_position} letters in correct position, {correct_letter} correct letters in wrong position.")
This works decently, but encounters problems if the randomly chosen word has duplicate letters. Also I'm interested in finding a way to solve this that isn't as cluttered as the above code.
I've found some solutions through googling, but they only seem to tackle either checking for positions or checking if a letter is contained in a word, and I've had a hard time trying to combine the two without ending up with duplicate +1s.
If I understood your description correctly, this function should do the job:
def comparison(self):
correct_position = 0
correct_letter = 0
for i,l in enumerate(self.guess):
if l==self.word[i]:
correct_position+=1
elif l in self.word:
correct_letter+=1
print(
f"{correct_position} letters in correct position, {correct_letter} correct letters in wrong position.")

Why can't I append something to a list? [closed]

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
I am currently having a bug with my code, and I need help fixing it.
Basically, when I try and append something to a list, it only works for certain arguments.
Here is a snipet(NOT the full code):
elif sentencearray[x] == " ":
lengthlist.append(len(longestword))
wordlist.append(str(*longestword))
longestword.clear()
When I try and run my code, wordlist.append(str(*longestword)) works, but lengthlist.append(len(longestword)) doesn't. All the variables are defined. It's just that when I try and print lengthlist, it turns out empty, even though I JUST appended something in it. I'm pretty sure the lines are tabbed the same. Can somebody PLEASE explain why this is happening?
P.S if it's some stupid mistake, I'm fairly new at python, so I might make some of these mistakes here and there.
Here is the full code:
# Online Python - IDE, Editor, Compiler, Interpreter
sentence = input("enter sentence here: ")
sentencearray = list(sentence)
sentencelength = len(sentencearray)
x = 0
y = 0
counter = 0
numvowels = 0
numletters = 0
numuppercase = 0
vowels = ['a','e','i','o','u','A','E','I','O','U']
uppercaseletters = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
letters = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z', 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
valuelist = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
knownletters = []
longestword = []
wordlist = []
lengthlist = []
knownletters.append(sentencearray[x])
for x in range(0,len(sentencearray)):
for y in range(0,10):
if sentencearray[x] == vowels[y]:
numvowels += 1
for x in range(0,len(sentencearray)):
for y in range(0,26):
if sentencearray[x] == uppercaseletters[y]:
numuppercase += 1
for x in range(0,len(sentencearray)):
for y in range(0,52):
if sentencearray[x]==letters[y]:
longestword.append(sentencearray[x])
for z in range(0,len(knownletters)):
if sentencearray[x] != knownletters[z]:
counter += 1
if counter == len(knownletters):
numletters += 1
knownletters.append(sentencearray[x])
counter = 0
elif sentencearray[x] == " ":
lengthlist.append(len(longestword))
wordlist.append(str(*longestword))
longestword.clear()
for a in range(0,len(sentencearray)):
for b in range(0,51):
if sentencearray[a] == letters[b]:
x += 1
valuelist[b] = valuelist[b] + 1
print("# of vowels:" + str(numvowels))
print("# of uppercase letters: "+ str(numuppercase))
print("# of unique letters: " + str(numletters+1))
print("# of times the most frequent letter appears: "+ str(max(valuelist)))
print(longestword)
print(len(longestword))
print(lengthlist)
print(wordlist(max(lengthlist)))
print(knownletters)
print(x)
You can also check it out at:
https://www.online-python.com/5TYKzmJO9g
I know this isn't what you asked for exactly... but there are so many Python features you're missing out on I couldn't resist putting together a 'more pythonic' example.
It's not entirely complete (I don't have your spec) and specifically it will treat numbers and special characters as 'letters', there are a number of ways you could exclude them.
vowels = ['A','E','I','O','U']
raw_sentence = input("Enter Sentence Here")
char_usage = {}
uppercase_count = 0
vowel_count =0
for char in raw_sentence:
#Take an uppercase copy of our character for later
upper_char = char.upper()
#instead of `valuelist`, we maintain a dictionary of character:occurance
if upper_char in char_usage:
char_usage[upper_char]+=1
else:
char_usage[upper_char]=1
#Instead of checking against a list, if a character == character.upper() it's upper case
if upper_char==char:
uppercase_count +=1
#use 'in' to check if a character is a vowel
if char.upper() in vowels:
vowel_count +=1
print("Number of Vowels",vowel_count)
print("Number of Uppercase Letters",uppercase_count)
#the largest value in our char_usag dictionary answers this question:
print("# times most frequently used letter appears",max(char_usage.values()))
#and the length of the dictionary answers the next:
print("Unique Letters", len(char_usage))
#use 'split' to split a string into words by white-space
sentence = raw_sentence.split() #convert string into a list of words
#Print the longest word:
print("Longest Word:", max(sentence, key=len))

Can't Break Out of While Loop

I wrote the following python code. It takes a list of English words I found on the internet and makes them a list so that I can use them for hangman. Well my problem is that every time I run this program and successfully guess the word, it doesn't break out of the while loop. It just keeps going. I can't figure out why for the life of me. Anyone have any clue as to why it isn't printing the final message to the winner?
import random
words = []
lettersGuessed = []
isGuessed = 0
wordFile = open(r'C:\Users\Sarah\PycharmProjects\hangman\words.txt')
for word in wordFile:
words.append(word.rstrip(wordFile.readline()))
mysteryWord = random.choice(words)
while len(mysteryWord) <= 1:
mysteryWord = random.choice(words)
for letter in mysteryWord:
print("?", end = "")
print("\n")
def isWon():
#win conditions
count = 0
for letter in mysteryWord:
if letter in lettersGuessed:
count += 1
if count == len(mysteryWord):
isGuessed = 1
count = 0
while isGuessed == 0:
guess = input("Guess a letter \n")
if guess.upper() or guess.lower() in mysteryWord:
lettersGuessed.append(guess)
for letter in mysteryWord:
if letter in lettersGuessed:
print(letter, end ='')
else:
print("?", end = '')
print("\n")
count = 0
isWon()
if isGuessed == 1:
break
print("Congratulations, you correctly guessed ", mysteryWord)
isGuessed in your top-level code and isGuessed in isWon function are two different variables. A function is a separate namespace (else a function using a variable with common name like i would wreak havoc in other code).
This can be solved by a global declaration, but it's a very bad style. Same applies to variables like mysteryWord and lettersGuessed.
Instead, you should return the value from the isWon function:
def isWon(mysteryWord, lettersGuessed):
# do your counting...
return isGuessed
# main code
victory = False
while not victory:
# ...
victory = isWon(mysteryWord, lettersGuessed)
# you don't even need the if ... break statement
BTW your check for all letters being guessed can be made a one-liner:
def isWon(mysteryWord, lettersGuessed):
return set(lettersGuessed) == set(mysteryWord)
The immediate issue is that isWon() is not setting isGuessed regardless of the input. If you guess with the string "foo" then
lettersGuessed.append(guess)
will make lettersGuessed a list with one item, which is a string. I think what you were trying to do was
lettersGuessed.extend(list(guess))
Which will add each letter in guess to the lettersGuessed list.
Two points also worth mentioning:
isWon() will consider the game won if you guess an anagram of the word in in questions e.g. "oof" will be considered a correct solution if the word is "foo"
words.append(word.rstrip(wordFile.readline())) is reading every even line of the input file, and adding it to the words list after removing any characters it has in common with the following word. You want to do words.append(word.strip()) instead.
It's all about scope. isGuessed used in isWon() is define in the local scope. If you want to affect isGuessed declared in the global scope you will have to either pass it to isWon() as a parameter or use the global keyword before modifying isGuessed. See below:
def isWon():
#win conditions
count = 0
for letter in mysteryWord:
if letter in lettersGuessed:
count += 1
if count == len(mysteryWord):
global isGuessed
isGuessed = 1
Output with this change:
python3 test.py
?????
Guess a letter
1
?????
Guess a letter
2
?????
Guess a letter
3
????3
Guess a letter
4t
????3
Guess a letter
t
t??t3
Guess a letter
e
te?t3
Guess a letter
s
test3
Congratulations, you correctly guessed test3
You're trying to communicate using the global variable is_guessed, but the isWon function doesn't gave a global is_guessed line, so it is setting a variable local to isWon named is_guessed rather than the global variable.
My suggestion would be, rather than adding global is_guessed to isWon(), to return either True or False from isWon() (based on whether or not the user has won) and use that to end the loop.
Here is an alternative version of your code:
import random
words = []
with open(r'C:\Users\Sarah\PycharmProjects\hangman\words.txt') as wordFile:
for word in wordFile: # assuming one word per line
words.append(word.strip()) # iterating a file reads one line per iteration
mysteryWord = random.choice(words)
mysteryLetters = set(mysteryWord.lower())
lettersGuessed = set()
def isWon():
return mysteryLetters == (lettersGuessed & mysteryLetters)
while not isWon():
for letter in mysteryWord:
if letter in lettersGuessed:
print(letter, end ='')
else:
print("?", end = '')
print()
guess = input("Guess a letter \n")[:1].lower()
if guess in mysteryWord:
lettersGuessed.add(guess)
print("Congratulations, you correctly guessed ", mysteryWord)
Well, I know that my answer is a little late but here's my solution:
#!/usr/bin/env python3
import random
word_file_name = "/usr/share/dict/canadian-english"
with open(word_file_name) as word_file:
# I'm assuming your input file has one word per line and that
# you want to keep only words that has more than one letter
words = [word.rstrip() for word in word_file if len(word) > 1]
mystery_word = random.choice(words)
# Using sets helps to remove duplicate letters and eases membership tests
letters_mystery = set(mystery_word.upper())
letters_guessed = set()
not_guessed = True
while not_guessed:
# We create a list with all the letters found or not
letters_to_show = [letter if letter.upper() in letters_guessed else "?"
for letter in mystery_word]
# We join them before printing them
print("".join(letters_to_show), "\n")
guess_received = input("Guess a letter :")
if guess_received.strip():
# We only keep the first letter received
guess_kept = guess_received[0].upper()
if guess_kept in letters_mystery:
letters_guessed.add(guess_kept)
# We determine if we need to continue
not_guessed = letters_guessed != letters_mystery
print("Congratulations, you correctly guessed", mystery_word)
Key points:
A list comprehension is used to put the words in a list
Sets are used to keep a group of letters without duplicates.
A conditional expression is used to choose if a letter will be shown or or a ?
Truth testing can be used to simplify the verification of certain information
You are using expressions like isGuessed == 1 like in C where True equals 1 and False equals 0. In Python, a variable can be a boolean. You can use it directly in a if statement

How to print words that only cointain letters from a list?

Hello I have recently been trying to create a progam in Python 3 which will read a text file wich contains 23005 words, the user will then enter a string of 9 characters which the program will use to create words and compare them to the ones in the text file.
I want to print words which contains between 4-9 letters and that also contains the letter in the middle of my list. For example if the user enters the string "anitsksem" then the fifth letter "s" must be present in the word.
Here is how far I have gotten on my own:
# Open selected file & read
filen = open("svenskaOrdUTF-8.txt", "r")
# Read all rows and store them in a list
wordList = filen.readlines()
# Close File
filen.close()
# letterList index
i = 0
# List of letters that user will input
letterList = []
# List of words that are our correct answers
solvedList = []
# User inputs 9 letters that will be stored in our letterList
string = input(str("Ange Nio Bokstäver: "))
userInput = False
# Checks if user input is correct
while userInput == False:
# if the string is equal to 9 letters
# insert letter into our letterList.
# also set userInput to True
if len(string) == 9:
userInput = True
for char in string:
letterList.insert(i, char)
i += 1
# If string not equal to 9 ask user for a new input
elif len(string) != 9:
print("Du har inte angivit nio bokstäver")
string = input(str("Ange Nio Bokstäver: "))
# For each word in wordList
# and for each char within that word
# check if said word contains a letter from our letterList
# if it does and meets the requirements to be a correct answer
# add said word to our solvedList
for word in wordList:
for char in word:
if char in letterList:
if len(word) >= 4 and len(word) <= 9 and letterList[4] in word:
print("Char:", word)
solvedList.append(word)
The issue that I run into is that instead of printing words which only contain letters from my letterList, it prints out words which contains at least one letter from my letterList. This also mean that some words are printed out multiple time, for example if the words contains multiple letters from letterList.
I've been trying to solve these problems for a while but I just can't seem to figure it out. I Have also tried using permutations to create all possible combinations of the letters in my list and then comparing them to my wordlist, however I felt that solution was to slow given the number of combinations which must be created.
# For each word in wordList
# and for each char within that word
# check if said word contains a letter from our letterList
# if it does and meets the requirements to be a correct answer
# add said word to our solvedList
for word in wordList:
for char in word:
if char in letterList:
if len(word) >= 4 and len(word) <= 9 and letterList[4] in word:
print("Char:", word)
solvedList.append(word)
Also since I'm kinda to new to python, if you have any general tips to share, I would really appreciate it.
You get multiple words mainly because you iterate through each character in a given word and if that character is in the letterList you append and print it.
Instead, iterate on a word basis and not on a character basis while also using the with context managers to automatically close files:
with open('american-english') as f:
for w in f:
w = w.strip()
cond = all(i in letterList for i in w) and letterList[4] in w
if 9 > len(w) >= 4 and cond:
print(w)
Here cond is used to trim down the if statement, all(..) is used to check if every character in the word is in letterList, w.strip() is to remove any redundant white-space.
Additionally, to populate your letterList when the input is 9 letters, don't use insert. Instead, just supply the string to list and the list will be created in a similar, but noticeably faster, fashion:
This:
if len(string) == 9:
userInput = True
for char in string:
letterList.insert(i, char)
i += 1
Can be written as:
if len(string) == 9:
userInput = True
letterList = list(string)
With these changes, the initial open and readlines are not needed, neither is the initialization of letterList.
You can try this logic:
for word in wordList:
# if not a valid work skip - moving this check out side the inner for-each will improve performance
if len(word) < 4 or len(word) > 9 or letterList[4] not in word:
continue
# find the number of matching words
match_count = 0
for char in word:
if char in letterList:
match_count += 1
# check if total number of match is equal to the word count
if match_count == len(word):
print("Char:", word)
solvedList.append(word)
You can use lambda functions to get this done.
I am just putting up a POC here leave it to you to convert it into complete solution.
filen = open("test.text", "r")
word_list = filen.read().split()
print("Enter your string")
search_letter = raw_input()[4]
solved_list = [ word for word in word_list if len(word) >= 4 and len(word) <= 9 and search_letter in word]
print solved_list

Testing string against a set of vowels - Python

This is a module in my program:
def runVowels():
# explains what this program does
print "This program will count how many vowels and consonants are"
print "in a string."
# get the string to be analyzed from user
stringToCount = input("Please enter a string: ")
# convert string to all lowercase letters
stringToCount.lower()
# sets the index count to it's first number
index = 0
# a set of lowercase vowels each element will be tested against
vowelSet = set(['a','e','i','o','u'])
# sets the vowel count to 0
vowels = 0
# sets the consonant count to 0
consonants = 0
# sets the loop to run as many times as there are characters
# in the string
while index < len(stringToCount):
# if an element in the string is in the vowels
if stringToCount[index] in vowels:
# then add 1 to the vowel count
vowels += 1
index += 1
# otherwise, add 1 to the consonant count
elif stringToCount[index] != vowels:
consonants += 1
index += 1
# any other entry is invalid
else:
print "Your entry should only include letters."
getSelection()
# prints results
print "In your string, there are:"
print " " + str(vowels) + " vowels"
print " " + str(consonants) + " consonants"
# runs the main menu again
getSelection()
However, when I test this program, I get this error:
line 28, in runVowels
stringToCount = input("Please enter a string: ")
File "<string>", line 1
PupEman dABest
^
SyntaxError: unexpected EOF while parsing
I tried adding a + 1 to the "while index < len(stringToCount)" but that didn't help either. I'm pretty new to python and I don't really understand what's wrong with my code. Any help would be appreciated.
I researched this error, all I found out was that EOF stands for end of file. This didn't help at all with resolving my problem. Also, I understand that sometimes the error isn't necessarily where python says the error is, so I double-checked my code and nothing seemed wrong in my eyes. Am I doing this the round-about way by creating a set to test the string elements against? Is there a simpler way to test if string elements are in a set?
Question resolved. Thank you to all!
Looks like you're using Python 2. Use raw_input(...) instead of input(...). The input() function will evaluate what you have typed as a Python expression, which is the reason you've got a SyntaxError.
As suggested use raw_input. Also you don't need to do this:
while index < len(stringToCount):
# if an element in the string is in the vowels
if stringToCount[index] in vowels:
# then add 1 to the vowel count
vowels += 1
index += 1
# otherwise, add 1 to the consonant count
elif stringToCount[index] != vowels:
consonants += 1
index += 1
# any other entry is invalid
else:
print "Your entry should only include letters."
getSelection()
Strings in Python are iterable, so you can just do something like this:
for character in stringToCount:
if character in vowelSet : # Careful with variable names, one is a list and one an integer, same for consonants.
vowels += 1
elif character in consonantsSet: # Need this, if something is not in vowels it could be a number.
consonants += 1
else:
print "Your entry should only include letters."
This should do just fine. Using a while is not necessary here, and very non-Pythonic imho. Use the advantage of using a nice language like Python when you can to make your life easier ;)
You can count the vowels like so:
>>> st='Testing string against a set of vowels - Python'
>>> sum(1 for c in st if c.lower() in 'aeiou')
12
You can do something similar for consonants:
>>> sum(1 for c in st if c.lower() in 'bcdfghjklmnpqrstvwxyz')
26
Also,
if stringToCount[index] in vowels:
should read
if stringToCount[index] in vowelSet:
Here's another way you could solve the same thing:
def count_vowels_consonants(s):
return (sum(1 for c in s if c.lower() in "aeiou"),
sum(1 for c in s if c.lower() in "bcdfghjklmnpqrstvwxyz"))
To wit:
>>> count_vowels_consonants("aeiou aeiou yyy")
(10, 3)
>>> count_vowels_consonants("hello there")
(4, 6)
Python truly is grand.
The errors in your file run as follows (plus some suggestions):
stringToCount = input("Please enter a string: ")
This should be raw_input if you want what the user typed in as a string.
stringToCount.lower()
The .lower() method returns a new string with its letters lowered. It doesn't modify the original:
>>> a = "HELLO"
>>> a.lower()
"hello"
>>> a
"HELLO"
vowelSet = set(['a','e','i','o','u'])
Here you could just as easily do:
vowelSet = set("aeiou")
Note you also don't strictly need a set but it is indeed more efficient in general.
# sets the vowel count to 0
vowels = 0
# sets the consonant count to 0
consonants = 0
Please, you don't need comments for such simple statements.
index = 0
while index < len(stringToCount):
You usually don't need to use a while loop like this in python. Note that all you use index for is to get the corresponding character in stringToCount. Should instead be:
for c in stringToCount:
Now instead of:
if stringToCount[index] in vowels:
vowels += 1
index += 1
You just do:
if c in vowels:
vowels += 1
elif stringToCount[index] != vowels:
consonants += 1
index += 1
# any other entry is invalid
Not quite right. You're checking that a character doesn't equal a set. Maybe you meant:
elif c not in vowels:
consonants += 1
But then there'd be no else case... Got to fix your logic here.
print "In your string, there are:"
print " " + str(vowels) + " vowels"
print " " + str(consonants) + " consonants"
The above is more pythonically written as:
print "In your string, there are: %s vowels %s consonants" % (
vowels, consonants)
# runs the main menu again
getSelection()
Not sure why you're calling that there - why not call getSelection() from whatever calls runVowel()?
Hope that helped! Enjoy learning this great language.
Bah, all that code is so slow ;). Clearly the fastest solution is:
slen = len(StringToCount)
vowels = slen - len(StringToCount.translate(None, 'aeiou'))
consonants = slen - vowels
...note that I don't claim it's the clearest... just the fastest :)

Categories