Python game Jotto
This problem is about a version of the game Jotto. The computer chooses a random five-letter word with no repeat letters. The player gets several turns to try to guess the computer’s word. On each turn, the player guesses a five-letter word and is told the number of letters that their guess has in common with the computer’s word.
from random import choice
enter_lenght = eval(input('Enter number of letters: '))
wordlist = [line.strip() for line in open('C:/Users/ZM/Desktop/Documents/New folder/wordlist.txt')]
word = ''
K = []
for i in wordlist:
if len(i) == enter_lenght:
K.append(i)
word = choice(K)
d = {}
d1 = {}
c = 0
for i in word:
if i in d1:
d1[i] += 1
else:
d1[i] = 1
print(word)
t = 0
while t <= enter_lenght:
user_word = input('Enter word: ')
for i in user_word:
if i in d:
d[i] += 1
else:
d[i] = 1
for i in d:
for j in d1:
if i == j:
c += d1[j]
if user_word == word:
break
print('There is ', c, ' same letters!')
t += 1
d = {}
c = 0
print('You have ', 5 - t, ' more attempts!')
if word == user_word:
print('You found exact word!!!', user_word)
else:
print('More luck next time!')
enter_lenght = eval(input('Enter number of letters: '))
wordlist = [line.strip() for line in open('C:/Users/ZM/Desktop/Documents/New folder/wordlist.txt')]
word = ''
K = []
for i in wordlist:
if len(i) == enter_lenght:
K.append(i)
word = choice(K)
d = {}
d1 = {}
c = 0
for i in word:
if i in d1:
d1[i] += 1
else:
d1[i] = 1
print(word)
t = 0
while t <= enter_lenght:
user_word = input('Enter word: ')
for i in user_word:
if i in d:
d[i] += 1
else:
d[i] = 1
for i in d:
for j in d1:
if i == j:
c += d1[j]
if user_word == word:
break
print('There is ', c, ' same letters!')
t += 1
d = {}
c = 0
print('You have ', 5 - t, ' more attempts!')
if word == user_word:
print('You found exact word!!!', user_word)
else:
print('More luck next time!')
SOLUTION:
Enter number of letters: 9
authority
Enter word: autharuty
There is 7 same letters!
You have 4 more attempts!
Enter word: auyredfik
There is 5 same letters!
You have 3 more attempts!
Enter word: authority
You found exact word!!! authority
Коментари
Постави коментар