The problem is that it will show some underscores to represent the word to be guessed, then the player is supposed to enter a letter they think is in the word. Then the code should return the underscores, this time replacing the correct underscore with its corresponding letter. The problem is it doesnt do the last step
import random
import time
from typing import List
word = ['dog','cat','bird','chair']
a = list(random.choice(word))
for letter in a:
e = print('_',end = '')
time.sleep(.25)
big = str(e)
count = big.count('_')
while int(count) > int(0):
def fun(self):
e.replace(a.index(big),dog)
for letter in a:
print('_',end = '')
time.sleep(.25)
dog = input('\ninput letter\n')
if dog in a:
print(fun(dog))
else:
for letter in a:
print('_',end = '')
time.sleep(.25)
import random
import time
from typing import List
word = ['dog','cat','bird','chair']
a = list(random.choice(word))
for letter in a:
e = print('_',end = '')
time.sleep(.25)
big = str(e)
count = big.count('_')
while int(count) > int(0):
def fun(self):
e.replace(a.index(big),dog)
for letter in a:
print('_',end = '')
time.sleep(.25)
dog = input('\ninput letter\n')
if dog in a:
print(fun(dog))
else:
for letter in a:
print('_',end = '')
time.sleep(.25)
Here is the entirety of the code I have if needed. Thanks.
CodePudding user response:
Many problems.
e
isNone
becauseprint()
returnsNone
replace
replaces a character/substring for character/substring, it does not take indexstr
types are immutable so this type of thing is probably best done with alist
Solution:
import random
import time
from typing import List
word = ['dog','cat','bird','chair']
chosen = random.choice(word)
print(chosen)
def display(big):
for letter in big:
print(letter,end = '')
time.sleep(.25)
def fun(char):
i = chosen.index(char)
big[i] = char
display(big)
big = ["_" for letter in chosen]
count = big.count('_')
display(big)
while count > 0:
char = input('\ninput letter\n')
if char in chosen:
fun(char)
count -= 1
else:
display(big)
CodePudding user response:
Your code has too many problems
- You are assigning print function into a value, which returns
None
- You are defining a function inside a while loop
- You can't pass integer/index as parameter in replace method
- After getting its first input, it just runs the last if-else statements and the code ends there
self
is not used inside the function
Your code should actually be like:
import random
word = ['dog','cat','bird','chair']
a = list(random.choice(word))
b = ['_']*len(a)
print(''.join(b))
while b != a:
inp = input('Input a letter: ')[0]
for i,j in enumerate(a):
if (a[i]==inp and b[i]=='_'):
b[i] = j
print(''.join(b))
b = ''.join(b)
print('The answer is', b, '\nCongrats! You found it!')
Tell me if its not working...