Python: 1. You cannot use lists anywhere in this game – you must use strings onl
ID: 3860070 • Letter: P
Question
Python:
1. You cannot use lists anywhere in this game – you must use strings only
2. YOU CANNOT USE LISTS – YOU MUST USE STRINGS ONLY
3. Read a word from the keyboard then print 100 blank lines (print (" " * 100))
4. Allow the user to enter letters one at a time
5. if the letter has already been guessed, display a message “letter already guessed”
6. After every new letter guessed display the word exposing the letters that have been correctly guessed and display an asterisk (*) for every letter that has not been correctly guessed
7. Once they have guessed all the letters in the word, display a message
8. There is no limit to the number of guesses the user gets
9. You can assume the user will enter one character when asked
10. You cannot use lists anywhere in this game – you must use strings only
Explanation / Answer
Python Program:
def check(letter, guess, word):
"""
Function that checks the letters in the word
"""
# Iterating over string
for i in range(0, len(word)):
# Checking for existence of letters
if word[i] == letter:
# Updating guess string
guess = guess[:i] + letter + guess[i+1:];
# Return updated string
return guess;
def main():
"""
Python program that simulates word Guess game
"""
# Reading words
word = input(" Enter a word: ");
# Printing new line
print(" " * 100);
# String that holds already guessed characters
guessedChars = "";
# Storing word length
wordLen = len(word);
# Forming guess string as a series of astreicks
guess = '*' * wordLen;
# Iterate till entire word is guessed
while 1:
# Reading a letter
letter = input(" Enter a letter: ");
# If already guessed
if letter in guessedChars:
print(" You have already guessed " + letter + " ");
else:
# Checking word
guess = check(letter, guess, word);
# Updating guessed Characters
guessedChars = guessedChars[:] + letter
# Printing updated guess string
print(" So far you have: " + guess);
# If all letters are guessed correctly
if '*' not in guess:
# Printing Message
print(" Correct! ");
return;
# Calling main function
main();
_____________________________________________________________________________________________
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.