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
thomas2017
1,342 PointsHow to post python code on forum?
Hi, I want to post some python code onto these forums for questions but I can't do copy and paste. I tried to wrap it with ``` but it didn't work. It just broke up my program into pieces. for exam import random
def you_lose(): again = input("""sorry you ran out of chances. type [y] play again [n] to quit.""")
if again.lower() == "y":
game()
else:
print("better luck next time")
def game(): chosen_num = random.randint(1, 10) chances = 5 while chances > 0: guess = int(input("Please enter a guess: ")) if guess == chosen_num: print("you got it right") play_again = input("you wanna play again buddy y/n?") if play_again.lower() == "y": game() else: print("Have a fabulous day")
break
elif guess != chosen_num & chances > 0:
chances -= 1
print("You have {} guess left.".format(chances))
if guess < chosen_num:
print("your number is higher than your guess. ")
elif guess > chosen_num:
print("your number is lower than your guess. ")
else:
you_lose()
game()
1 Answer
Steven Parker
243,199 PointsWhen you bracket your code in back-ticks, they need to be on their own line. You might put the letters "py" after the first set to give your code syntax coloring. So it would look like this:
```py
(your python code would go here)
```
And when you submit it, it looks like this:
def you_lose():
again = input("""sorry you ran out of chances. type [y] play again [n] to quit.""")
if again.lower() == "y":
game()
else:
print("better luck next time")
#... etc.
And if you haven't seen it already, you might find this video on code formatting helpful.