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 trialMiguel de Luis Espinosa
41,279 PointsWhat do you think of my solution?
This is my code
import random
myNumber = random.randint(1,10)
guess = int(input("Guess my number "))
maxTries = 5
tries = 1
while tries <= maxTries:
if guess == myNumber:
print("You won")
break
else:
if guess < myNumber:
guess = int(input("\n Sorry wrong number. My number is Higher. You have tried to guess my number {} times \n Guess my number again:".format(tries)))
else:
guess = int(input("\n Sorry wrong number. My number is Lower. You have tried to guess my number {} times \n Guess my number again:".format(tries)))
tries += 1
else:
print("You lost, loser!")
3 Answers
Kenneth Love
Treehouse Guest TeacherWhat if I guess 'a'
instead of a number?
Also, PEP 8 discourages variable names likeThis
and recommends names like_this
. It's not a required style, but it'll make it easier for you to work with other Python developers in the future.
nvcxnvkldsjaklfds
Courses Plus Student 5,041 PointsSolution is good. But my solution would be
import random
myNumber = random.randint(1,10)
guess = int(input("Guess my number "))
maxTries = 5
tries = 1
while tries <= maxTries:
if guess == myNumber:
print("You won")
break
else:
if guess < myNumber:
guess = int(input("\n Sorry wrong number. My number is Higher. You have tried to guess my number {} times \n Guess my number again:".format(tries)))
else:
guess = int(input("\n Sorry wrong number. My number is Lower. You have tried to guess my number {} times \n Guess my number again:".format(tries)))
tries += 1
if tries > 6:
print("You lost, loser!")
The reason I go with if loop rather than your else loop. while loop condition fail can't be mapped to else branch.
Mostly that is the reason I went with new if loop to check the tries against 6.
I hope it helps
Josué Sotelo
538 PointsI added Ken's request to your code:
import random
myNumber = random.randint(1,10)
maxTries = 5
tries = 1
while True:
try:
guess = int(input("Guess my number >"))
except ValueError:
print("Ops! Please input a number")
continue
else:
break
while tries <= maxTries:
if guess == myNumber:
print("You won")
break
else:
if guess < myNumber:
guess = int(input("\n Sorry wrong number. My number is Higher. You have tried to guess my number {} times \n Guess my number again:".format(tries)))
else:
guess = int(input("\n Sorry wrong number. My number is Lower. You have tried to guess my number {} times \n Guess my number again:".format(tries)))
tries += 1
if tries > 6:
print("You lost, loser!")
Maxim Kontsevitch
25,447 PointsPretty nice, Its Working and Its functional. Great job!