Problems with Randomized Guessing Game - python-3.x

this is my first post here so bear with me. I decided to take an online coding course to help me learn the basics of Python, up until now it's been great. Difficult, but manageable. Well, it appears I've hit a wall. I am trying to write a program that randomly generates a word from a wordlist.txt file then allow the user to guess letters in said word with a set amount of guesses.
'''
import random
def pick_word_from_file():
file = open("wordlist.txt", 'r')
words = file.readlines()
file.close()
word = random.choice(words).strip("\n")
return word
word = pick_word_from_file()
hidden_word = len(word)
num_of_guesses = 5
print('Welcome to Guess a Letter!')
print('Your word has {} letters.'.format(hidden_word))
print('You have {} guesses left!'.format(num_of_guesses))
new_word = []
for i in range(hidden_word):
new_word.append('_')
print(new_word)
while num_of_guesses > 0:
guess = input('Take a guess!')
if guess in word:
num_of_guesses = num_of_guesses - 1
pass
else:
num_of_guesses = num_of_guesses - 1
print('Oof! looks like that letter is not in the word!')
if num_of_guesses == 0:
Print('All out of guesses!')
'''
What I am currently struggling with is replacing the hashed out letter of the randomized word with the correct guess. Every time is a guess is correct it should output that letter in the correct space for the word.

My approach to this problem is storing the indices of each character in a dictionary like this.
>>> word = 'python'
>>> word_loc = {}
>>> for idx, char in enumerate(word):
... word_loc[char] = word_loc.get(char, []) + [idx]
...
>>> word_loc
{'p': [0], 'y': [1], 't': [2], 'h': [3], 'o': [4], 'n': [5]}
Next, update each index accordingly when the guess is correct.
>>> while True:
... guess = input('Take a guess!\n')
... if len(guess) != 1:
... print('Invalid guess!')
... elif guess in word:
... print('correct guess!')
... for idx in word_loc[guess]:
... new_word[idx] = guess
... print(new_word)
... if ''.join(new_word) == word:
... print('Congratulations! You've guessed the word correctly!')
... break
... else:
... num_of_guesses -= 1
... print('Incorrect guess! Remaining attempts = {}'.format(num_of_guesses))
... if num_of_guesses == 0:
... print('You failed to guess the word! Aborting program!')
... break
...
Take a guess!
s
Incorrect guess! Remaining attempts = 4
Take a guess!
r
Incorrect guess! Remaining attempts = 3
Take a guess!
p
correct guess!
['p', '_', '_', '_', '_', '_']
Take a guess!
y
correct guess!
['p', 'y', '_', '_', '_', '_']
Take a guess!
c
Incorrect guess! Remaining attempts = 2
Take a guess!
h
correct guess!
['p', 'y', '_', 'h', '_', '_']
Take a guess!
n
correct guess!
['p', 'y', '_', 'h', '_', 'n']
Take a guess!
s
Incorrect guess! Remaining attempts = 1
Take a guess!
a
Incorrect guess! Remaining attempts = 0
You failed to guess the word! Aborting program!

Related

Is there any way of eliminating the nested loops to prevent time execution error

how to make the code more efficient by using list comprehension or using itertools in python because this program gives timeexecution error for large input datasets.
n=0
k=0
v='AEIOU'
for i in range(0,len(string)):
for j in range(i+1,len(string)+1):
a = string[i:j]
#print(a)
if (a[0] == 'A') or (a[0] == 'E') or (a[0] == 'I') or (a[0] == 'O') or (a[0] == 'U'):
n+= 1
else:
k+=1
if n>k:
print('Kevin'+' '+str(n))
elif n<k:
print('Stuart'+' '+str(k))
else:
print('Draw')
if __name__ == '__main__':
s = input()
minion_game(s)
Please check the question from this link
https://solution.programmingoneonone.com/2020/06/hackerrank-the-minion-game-problem-solution-python.html
I would appreciate it if you please explain the solution to the program as I am totally new to programming.
Basically what you have to do is:
def isVowel(c):
if c in ['A', 'E', 'I', 'O', 'U']:
return True
return False
Kevin=0
Stuart=0
for i in range(len(s)): #s is the input string
a=len(s)-i
if isVowel(s[i]):
Kevin+=a
else :
stuart+=a
#check who has scored more he is the winner.
This works because, suppose for a string BANANA:
B is consonant so, we have to include all the strings starting with B.
B,BA,BAN.... so we will have total of (n-indexOf(B)) numbers of strings = 6-0 = 6 pts for stuart
A is vowel,
all strings with A = n-indexOf(A)=6-1=5 so 5 pts for kevin.
You dont have to explicitly check the numbers of times current substrings appear in the string as you will be iterating over all of them.
for example,
total pts for Kevin =
pts for A at : Index(1) + Index(3) + Index(5)
total pts = (6-1) + (6-3) + (6-5) = 9

A problem about a python game guess the number

I am a very beginner in Python, I want to write a number guessing program in which the user thinks of a number which the computer has to guess.
I wrote this it works but there is this problem:
say my secret number is 13. The computer says 45, I enter 'h', then it says a number in range of (0, 45), for example it says 10, after I enter 'l' I don't want it to say a number over 45 (the number I said was too high!) and vice versa about the low number.
import random
print('Please think of a number between 0 and 100!')
input('''Press Enter once you've thought of one...''')
guess = random.randint(0,100)
print('Is your secret number', guess, '?'
'''\nEnter 'h' to indicate the guess is too high'''
'''\nEnter 'l' to indicate the guess is too low'''
'''\nEnter 'c' to indicate the guess is correct''')
hlc = input()
while hlc <= 'h' or 'l' or 'c':
if hlc == 'h':
guess -= 1
guess = random.randint(0, guess)
print('Is your secret number', guess,'?')
hlc = input()
elif hlc == 'l':
guess += 1
guess = random.randint(guess, 100)
print('Is your secret number', guess,'?')
hlc = input()
elif hlc == 'c':
print('Game over. Your secret number was', guess, '!')
break
You should track the highest and lowest numbers your user has rejected so far. Something like the following:
lowest_guess = 100
highest_guess = 100
while ... :
guess = random.randint(lowest_guess, highest_guess)
...
if hlc == 'h':
highest_guess = guess
elif hlc == 'l':
lowest_guess = guess

How to delete the vowel from a given string

How to delete the vowel from the given string?
letter = 'raeiou'
new_string = []
for i in letter:
new_string.append(i)
for j in new_string:
if j == 'a' or j == 'e' or j == 'i' or j == 'o' or j == 'u':
new_string.remove(j)
final = ''.join(new_string)
print('The string after removing the vowels is {}'.format(final))
expected output r but reo
When you do:
for j in new_string:
...
new_string.remove(...)
you are modifying a list while looping on it (see e.g. strange result when removing item from a list).
You could simply skip vowels when you create new_list in the first place:
for i in letter:
if not i in 'aeiou':
new_string.append(i)
final = ''.join(new_string)
Here is an alternative suggestion:
def func(s):
for c in 'aeiouAEIOU':
s = ''.join(s.split(c))
return s
You don't need two loops for this!
letter = 'raeiou'
new_string = letter
vowels = ('a', 'e', 'i', 'o', 'u')
for i in letter:
if i in vowels:
new_string = new_string.replace(i,"");
print('The string after removing the vowels is {}'.format(new_string))

Pig Latin Program in Python

Write a program in Python 3 that converts a sentence typed in by the user to Pig Latin. Pig Latin has two rules:
If a word begins with a consonant all consonants before the first
vowel are moved to the end of the word and the letters "ay" are then
added to the end. e.g. "coin" becomes "oincay" and "flute" becomes
"uteflay". If a word begins with a vowel then "yay" is added to the
end. e.g."egg" becomes "eggyay" and "oak" becomes "oakyay".
My code works for individual words but does not work for sentence. I have tried entering:
wordList = word.lower().split(" ")
for word in wordList:
but it does not work.
#Pig Latin Program
import sys
VOWELS = ('a', 'e', 'i', 'o', 'u')
def pig_latin(word):
if (word[0] in VOWELS):
return (word +"yay")
else:
for letter in word:
if letter in VOWELS:
return (word[word.index(letter):] + word[:word.index(letter)] + "ay")
return word
word = ""
while True:
word = input("Type in the word or Exit to exit:")
if (word == "exit" or word == "Exit" or word == "EXIT"):
print("Goodbye")
sys.exit()
else:
print(pig_latin(word))
The input sentence: the rain in Spain
The output sentence: ethay ainray inyay ainSpay
So you could do something like this, it returns an iterable of all the pig-ed words and you can join them in the last step. You don't need that last return you have. My guess is the issue you saw was that you are returning in the first loop. you could track the return outside the loop and append to it in the loop and return that also.
import sys
VOWELS = ('a', 'e', 'i', 'o', 'u')
def pig_latin(word):
wordList = word.lower().split(" ")
for word in wordList:
if (word[0] in VOWELS):
yield (word +"yay")
else:
for letter in word:
if letter in VOWELS:
yield (word[word.index(letter):] + word[:word.index(letter)]+ "ay")
break
word = ""
while True:
word = input("Type in the word or Exit to exit:")
if (word == "exit" or word == "Exit" or word == "EXIT"):
print("Goodbye")
sys.exit()
else:
print(' '.join(pig_latin(word)))

I can't find the logic error in this bubble sort code

I'm attempting to do a simple bubble sort code to get familiar with list/string manip & method use, but for some reason, when I attempt to iterate through each value in the list to remove white space and values that are non ints, it skips some. I haven't even gotten to the bubble sorting part..
#test data: 45,5j, f,e,s , , , 45,q,
if __name__ == "__main__":
getList = input("Enter numbers separated by commas:\n").strip()
listOfBubbles = getList.split(',')
print (listOfBubbles)
i = 0
for k in listOfBubbles:
listOfBubbles[i] = k.strip()
print ("i = {0} -- Checking '{1}'".format(i,listOfBubbles[i]))
if listOfBubbles[i] == '' or listOfBubbles[i] == ' ':
del listOfBubbles[i]
i -= 1
else:
try:
listOfBubbles[i] = int(listOfBubbles[i])
except ValueError as ex:
#print ("{0}\nCan only use real numbers, deleting '{1}'".format(ex, listOfBubbles[i]))
print ("deleting '{0}', i -= 1".format(listOfBubbles[i]))
del listOfBubbles[i]
i -= 1
else:
print ("{0} is okay!".format(listOfBubbles[i]))
i += 1
print(repr(listOfBubbles))
Output:
Enter numbers separated by commas:
45,5j, f,e,s , , , 45,q,
['45', '5j', ' f', 'e', 's ', ' ', ' ', ' 45', 'q', '']
i = 0 -- Checking '45'
45 is okay!
i = 1 -- Checking '5j'
deleting '5j', i -= 1
i = 1 -- Checking 'e'
deleting 'e', i -= 1
i = 1 -- Checking ''
i = 1 -- Checking '45'
45 is okay!
i = 2 -- Checking 'q'
deleting 'q', i -= 1
[45, 45, ' ', ' 45', 'q', '']
How about a more pythonic way?
#input
listOfBubbles = ['45', '5j', ' f', 'e', 's ', ' ', ' ', ' 45', 'q', '']
#Copy input, strip leading / trailing spaces. Remove empty items
stripped = [x.strip() for x in listOfBubbles if x.strip()]
# list(filtered) is ['45', '5j', 'f', 'e', 's', '45', 'q']
out = []
for val in filtered:
try:
out.append(int(val))
except:
# don't do anything here, but need pass because python expects at least one line
pass
# out is [45, 45]
Finally, to jump to your correct answer
out.sort()
Update
To clarify pass
>>> for i in range(0,5):
pass
print i
0
1
2
3
4
Never alter the very list you're looping on -- inside the loop for k in listOfBubbles:, you're deleting some items of that very list, and that upsets the internal looping logic. There are many alternative approaches, but the simplest fix is to loop on a copy of the list you want to change: for k in list(listOfBubbles):. There may be more issues, but this is the first one.
Nevermind, fixed it. I change the loop from a for.. to a while..
if __name__ == "__main__":
getList = input("Enter numbers separated by commas:\n").strip()
listOfBubbles = getList.split(',')
print (listOfBubbles)
i = 0
while i < len(listOfBubbles):
listOfBubbles[i] = listOfBubbles[i].strip()
print ("i = {0} -- Checking '{1}'".format(i,listOfBubbles[i]))
if listOfBubbles[i] == '' or listOfBubbles[i] == ' ':
del listOfBubbles[i]
i -= 1
else:
try:
listOfBubbles[i] = int(listOfBubbles[i])
except ValueError as ex:
#print ("{0}\nCan only use real numbers, deleting '{1}'".format(ex, listOfBubbles[i]))
print ("deleting '{0}', i -= 1".format(listOfBubbles[i]))
del listOfBubbles[i]
i -= 1
else:
print ("{0} is okay!".format(listOfBubbles[i]))
i += 1
print(repr(listOfBubbles))
You cannot use an iterator to delete from a list because the length changes.
Instead you must use an index in the for loop (or a while loop).
Once you have removed an item you need to loop through the list again.
psuedo-code:
again:
for i = 0 to list.count - 1
{
if condition then
delete list[i]
goto again;
}
If you're going to delete from a list while iterating through it, traverse the list in reverse order:
for( i = myList.length - 1; i >= 0; i-- ) {
// do something
if( some_condition ) {
myList.deleteItem( i );
}
}
This way you won't skip any list items as shortening the list doesn't affect any future iterations. Of course, the above snippet assumes that the deleteItem method is supported in the list/array class and does the appropriate things.

Resources