mercredi 5 août 2020

Generating a New Random Word After a Condition is Met

I am trying to create a Lingo game in Python where the user has 2 minutes to guess as many correct 5 letter words as possible. I use the random class and a text file to generate a random 5 letter word. However, after the user correctly guesses the word, I do not know how to generate a new 5 letter word. I am thinking I generate it inside the while loop for the timer, but I am not sure. I am still learning about python and this is my first time writing a "game" program. Any ideas or suggestions?

import random
from collections import Counter
import time

file = "words.txt" #File with 5 letter words
f = open(file, 'r')
lines = f.readlines()
randomword = random.choice(lines) #generate random 5 letter word from file
numletters = Counter(randomword)
word = [randomword[0], '_', '_', '_', '_']#part of word that is being shown to user
score = 0 #user score
print(randomword[0])
#print(randomword)
timer = 120 #amount of time game runs for
start_time = time.time() #remember when game started

while(time.time() - start_time < timer): #2 minutes is given to guess as many 5 letter words as possible
    for i in range(1,6): #take 5 guesses at what random word is
        guess = input("Guess the word: ")
        for j in range(1,5): #check to see if other 4 letters of random word match the letters in the user guess word
            numguess = Counter(guess)

            if(guess[j] == randomword[j] and word[j] == '_'): #replace empty character with letter of random word if correctly guessed
                word[j] =  randomword[j]
            elif(guess[j] in randomword and word[j] == '_'): #replace empty character with uppercase letter of guessed letter if that letter is in the randomword
                word[j] = guess[j].upper()
            elif(guess[j] in randomword and numguess[guess[j]] > numletters[guess[j]] and word[j] == '_'): #guessed letter gets replaced with '_' if max number of that letter is already in word
                word[j] = '_'
            elif(guess[j] == randomword[j] and word[j] != '_'): #replace uppercase letter with correctly guessed letter in word
                word[j] = randomword[j]
            elif(guess[j] == randomword[j] and guess[j].upper() in word): #upper case letter is guessed in correct spot, but no other letters are correcty guessed
                word[word.index(word[j].upper())] = '_'
            else: #no correct letters are guessed
                word[j] = '_'
       
        answer = word[0] + ' ' + word[1] + ' ' + word[2] + ' ' + word[3] + ' ' + word[4]       
        print(answer)
    
        if(answer == randomword[0] + ' ' + randomword[1] + ' ' + randomword[2] + ' ' + randomword[3] + ' ' + randomword[4]):   
            print("You are correct!")
            score += 100 #user scores 100 points for each correctly guessed word
            break
    if(answer != randomword[0] + ' ' + randomword[1] + ' ' + randomword[2] + ' ' + randomword[3] + ' ' + randomword[4]):
        print("You are incorrect! You get 0 points for this word")
        print("The correct word is: " + randomword.strip())

print("Your final score is " + str(score)) #print final score

Aucun commentaire:

Enregistrer un commentaire