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 trialNigel Chia
909 Pointsi tried to add +1 ,-1 to the secret number and it would say 'close'
import random
secret_num= random.randint(1, 10)
while True:
number= int(input('Give me a number between 1 and 10: '))
if number == secret_num:
print('you got it!, it was {}'.format(secret_num))
break
elif:
number==secret_num-=1
print('close')
elif:
number==secret_num+=1
print('close')
else:
print('Bummer')
2 Answers
Steve Hunter
57,712 PointsHi Nigel,
Using the +=
and -=
operator changes the value held in secret_num
. If you use secret_num + 1
instead, because you aren't assigning anything back into secret_num
, the value held in there doesn't change. The -=
and +=
operators work as an assignment too, they are equivalent to secret_num = secret_num + 1
.
I don't know if that will fix your syntax error, but it will certainly help your code behave as you expect.
Steve.
gabrieL faurE
1,697 Pointshi, you shoud put ur condition inside the elif, like this:
elif number==secretnumber-1:
that way you are not changing your variables and there is no syntax error.
Nigel Chia
909 PointsNigel Chia
909 Pointsbut it would point give me a syntax error on elif:'s :