Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
ilhan KAYIHAN
6,664 Pointsword guess game loop problem, always get a _ _ _ _
while True:
s=['a','p','p','l','e']
t1= input("Guess a word please:(quit: q) ")
if t1 == 'q':
break
elif t1 in s:
x = 0
k = 1
for t1 in s[len(s)-(len(s)+x)]:
print("_ "*(len(s)-(len(s)+x)) , s[len(s)-(len(s)+x)] , " _ "*(len(s)-k))
x +=1
k +=1
2 Answers
Nathan Smutz
9,492 PointsMy guess is that you want to show the letters that match "apple". So if someone enters "art", you would display "a _ _ _ _". Is that right?
In that case, you'd want a for loop which iterates across s and t1, checking whether the nth letter of s is the same as the nth letter of t1. The thing you want to watch out for is if t1 is shorter than s. So, if t1 is "art", the last letter is at t1[2]. If you try to check what's in t1[3], you'll get an error.
So, you need something like:
response = ""
for i in range(len(s)):
if i < len(t1) and s[i] == t1[i]:
response += s[i]
else:
response += "_"
print(response)
Nathan Smutz
9,492 PointsSince your input statement asks for a whole word, I think your solution is going to look something like my example above.
You're wondering why your original code is doing what it's doing. Just to simplify what you've got: len(s) - (len(s) + x) = len(s) - len(s) + x = 0 + x = x So lets simplify that where we see it:
while True:
s=['a','p','p','l','e']
t1= input("Guess a word please:(quit: q) ")
if t1 == 'q':
break
elif t1 in s:
x = 0
k = 1
for t1 in s[x]:
print("_ "*(x) , s[x] , " _ "*(len(s)-k))
x +=1
k +=1
Lets look at that last piece: Particularly the lines:
for t1 in s[x]:
print("_ "*(x) , s[x] , " _ "*(len(s)-k))
Since x = 0 at this point, s[x] = s[0] = 'a'
So that works the same as:
for t1 in 'a':
Here, python is treats 'a' kind of like a list with one item in it. So, the for loop will only run once. So your output comes from running this one time.
print("_ "*(x) , s[x] , " _ "*(len(s)-k))
As we saw earlier, x = 0 and k= 1. So the above line is equivalent to this:
print("_ "*(0) , s[0] , " _ "*(len(s)-1))
Lets interpret len(s) = 5 too:
print("_ "*(0) , s[0] , " _ "*(5-1))
And we know s[0] = 'a':
print("_ "*(0) , 'a' , " _ "*(4))
Now, "_"*0 is just an empty string ''
And, "_"*4 = '____'
So the line means:
print('', 'a', '____')
Which prints out:
a____
ilhan KAYIHAN
6,664 Pointsilhan KAYIHAN
6,664 PointsFirstly we input a letter. In my loop, if t1 in s it gives always s [0] _ _ _ _. else lose. But I want Thé loop that outputs Thé true letter for Thé correct place. I couldn't Manage to make a loop in apple example; When you input a or p or l it always output: a _ _ _ _.