First off I am a noob to python (if you couldn’t tell) and a noob to StackOverflow.
I’ve seen similar programs but I can’t find ones that quite give me much help. In short, the assignment I’m doing is to create a program that takes a word from the user and proceeds to guess random letters.
Currently, my new_letter function doesn’t work I’ve tried printing in several places so I know that it just doesn’t get through the function.
I was trying to come up with a counter so that the guessed letter would replace the * in computers_word in the right spot that corresponds with the same place in actual_word.
import random #I just have this as a placeholder so guessed_letter exists guessed_letter = 0 #list of all letters alphabet = ['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'] generateletter = True #this function is supposed to randomly picks letters and only move on if that letter is not one in the guessed list def new_letter(): while generateletter == True: n = random.randint(0, 25) guessed_letter = alphabet[n] for g in guessed: if guessed_letter == g: continue generateletter == False guessed.append(guessed_letter) print('I guess '+str(guessed_letter)) generateletter == False return guessed_letter #This functions just updates the player (and me) def display_status(): print(actual_word) print(computers_word) print(guessed) playagain = True guessagain = True #Outer loops allow for variables to be reset if the user wants to play again after the word has been guessed while playagain == True: #new word is entered by the user user_word = input('Please choose a word and I will try to guess it: ') #resets lists guessed = [] actual_word = [] computers_word = [] for letter in user_word: #Append users word to actual_word as a list actual_word.append(letter) #Appends *'s the same length as the user's word to computers_word computers_word.append('*') counter = 0 #computer gameplay begins while guessagain == True: if computers_word != actual_word: display_status()
^ Okay as of now the program gets to here and just stops, I don’t get any errors it’s just the new_letter function isn’t returning anything.
guessed_letter = new_letter() print(guessed_letter) for letter in user_word: if guessed_letter == letter: computers_word.append(counter, guessed_letter) #I realize that the way these counter works is wrong. The concept is so that the star replaced corresponds with the same position in the actual_word list counter = counter + 1 else: continue elif computers_word == actual_word: display_status user_choice = input('I guessed the word! Would you like to play again? ') if user_choice == 'Yes' or user_choice == 'yes': guessagain == False playagain == True else: guessagain == False playagain == False
Advertisement
Answer
Okay. So, to start off as @Uvar pointed out, the assignment and comparison has been mixed up at several places. Additionally, the code could run for very long duration because of the while loop and indiscriminate picking of values between 0-25. The code doesn’t eliminate previously guessed digits in future guesses. The modified code (with comments) is as follows:
import random import string guessed_letter = 0 # list of all letters # string.ascii returns 'abcdefghijklmnopqrstuvwxyz' alphabet = list(string.ascii_lowercase) alphabet.extend(list(string.ascii_uppercase)) generateletter = True # this function is susposed to randomly picks letters and only move # on if that letter is not one in the guessed list def new_letter(generateletter, alphabet): while generateletter: # Using random.choice to pick values guessed_letter = random.choice(alphabet) if guessed_letter not in guessed: for g in guessed: if guessed_letter == g: continue generateletter = False guessed.append(guessed_letter) # To prevent picking the same values again, remove them alphabet.remove(guessed_letter) print('I guess '+str(guessed_letter)) generateletter = False return guessed_letter def display_status(actual_word, computers_word, guessed): print("Actual Word", actual_word) print("Computers Word:", computers_word) print("Guessed:", guessed) playagain = True guessagain = True # 'while playagain == True' and 'while playagain' evaluate the same thing while playagain: user_word = input('Please choose a word and I will try to guess it: ') guessed = [] actual_word = [] computers_word = [] for letter in user_word: actual_word.append(letter) computers_word.append('*') while guessagain: if computers_word != actual_word: display_status(actual_word, computers_word, guessed) guessed_letter = new_letter(generateletter, alphabet) print("Guessed Letter:", guessed_letter) # Used enumerate here to get position too # Additionally, used actual_word for i,letter in enumerate(actual_word): if guessed_letter == letter: # Insert at exactly the required position computers_word[i] = guessed_letter else: continue elif computers_word == actual_word: display_status user_choice = input('I guessed the word! Would you like to play again? ') if user_choice == 'Yes' or user_choice == 'yes': guessagain = False playagain = True else: guessagain = False playagain = False