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!

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

Python

break issue

i wrote my own number game code but there is a problem in my break point in the try except part if my input is not an int it prints the message endless time if i put a break it send same message 1 time and quit the game couldn't solve how to print 1 time and move on to ask new number without any interruption.

while True :
try : guess == int(guess)
except ValueError: print("{} is not a number".format(guess)) #TH襤S PART! break
else: guesses.append(guess)
if len(guesses) == 5 : print("You are out of guess!") break elif guess != secret_num: print ("{} is Wrong".format(guess)) guess=input("make your guess between 1-100: ") continue elif guess == secret_num: print("Yeah {} is the corret one!".format (guess)) break else: print('You are out of guesses')

You need to format your code using the instructions provided in the markdown cheatsheet in the link right below the comment box. It says:

Code
Wrap your code with 3 backticks (```) on the line before and after. If you specify the language after the first set of backticks, that'll help us with syntax highlighting.

          ```html
          <p>This is code!</p>
          ```

This way we can see how your code is actually formatted, without seeing that no one can tell you what's wrong.

3 Answers

Ismail KO
Ismail KO
1,748 Points
from random import randint
guesses=[]
secret_num = randint(1, 100)
print("i've got  a secret number try to find it")
guess=input("make your guess between 1-100: ")
guesses = []

while True :            
        try :
            guess == int(guess)            
        except ValueError:
            print("{} is not a number".format(guess)) 
            break

        else:
            guesses.append(guess)        
            if len(guesses) == 5 :
                print("You are out of guess!")
                break
            elif guess != secret_num:
                print ("{} is Wrong".format(guess))
                guess=input("make your guess between 1-100: ")
                continue
            elif guess == secret_num:
                print("Yeah {} is the corret one!".format (guess))
                break
else:
        print('You are out of guesses')     

your code is here ,(if you want write code in your comment, link, quotes or others look top of 'Post answer' and click Markdown Cheatsheet see how can do these.)

Look at your first mistake

guess == int(guess)

we uses == for equal (booleans, 1 or 0) you have to use for that =

Your second mistake

            elif guess == secret_num:
                print("Yeah {} is the corret one!".format (guess))

.format (guess) delete space between t and parantheses (format (guess) ---> format(guess)) (be careful for spaces you can add least 4 space (1 tab) and multiple (2,3,4) tab before statement, if you want add statement or something in another statement for ex:

for i in range(1,10)
    for j in range(15,65)
        print("{}, {}".format(i,j))      

)

and your last mistake

            elif guess == secret_num:
                print("Yeah {} is the corret one!".format (guess))
                break
else:
        print('You are out of guesses')     

there is no space before else, you have to add 3 tab space (12 space)

if you need help, do not hesitate and ask me.

thnax for that explanation(harika anlatm覺ss覺n anl覺yorum b覺de bu v覺dyo ser覺s覺n覺n son letter game 2. bolumunde b覺 anda bam gum os sys gb覺 kavramlar g覺r覺o pekde ne yapt覺g覺n覺 anlatm覺yo onlar覺 nerden arast覺rab覺l覺r覺m h覺c b覺 sey anlamad覺m yan覺)

Ismail KO
Ismail KO
1,748 Points

thnax for that explanation(harika anlatm覺ss覺n anl覺yorum b覺de bu v覺dyo ser覺s覺n覺n son letter game 2. bolumunde b覺 anda bam gum os sys gb覺 kavramlar g覺r覺o pekde ne yapt覺g覺n覺 anlatm覺yo onlar覺 nerden arast覺rab覺l覺r覺m h覺c b覺 sey anlamad覺m yan覺)

nothing (os sys kavramlar覺 biraz kar覺覺k oralara kafan覺 imdilik pek takma sen d繹ng羹leri ve listeleri nas覺l kullanabileceine younla 癟羹nk羹 bu iin devam覺nda sana 癟ok laz覺m olacak, dier oynatma listelerine bakarak veya bu linke t覺klayarak da bir eyler bulabilirsin, kitab覺n indirme linkini burada bulabilirsin, ben indirdim fotokopiciden yazd覺rd覺m kitab覺 (kitab ayr覺ca a癟覺k kaynakl覺 olduu i癟in s覺k覺nt覺 olmuyor) ve arada a癟覺p bak覺yorum(kitap biraz a覺rd覺r, o kitab覺 ara癟 olarak kullanman覺 tavsiye ederim, bulabilirsen son bask覺s覺n覺 indirmeye gayret et, kitap biraz kal覺n, kitab覺n ana kayna覺nda , python i癟in bilgisayar覺na bir program indirmek istersen PyCharm program覺n覺 tavsiye ederim (sen community s羹r羹m羹n羹 indir, ben professional olan覺 kullan覺yorum (eitim g繹rd羹羹m kurum site ile anlamal覺 olduu i癟in 繹renci lisans覺na sahibim bununla ilgili bir sorun olursa sor)), onu kurarsan ve sorun oluursa bana dan覺覺rs覺n, ya da yeni soru sorars覺n ve g繹rebilirsem sana d繹nerim, imdilik benden bu kadar 癟al覺maya devam et, ben senden biraz ilerdeyim, daha 繹renecein 癟ok ey var, baar覺lar diliyorum.)

(襤stihza sitesi kapat覺lm覺, Python 3 pdf dosyas覺 bu linkte Python 2 pdf dosyas覺 bu linkte)

but im on python.Didn't understand whats your saying ?

Right I was copying and pasting the example provided in the Markdown Cheatsheet, which is located right below the box you type your comment into. So replace html with python...

Ismail KO
Ismail KO
1,748 Points

Code Wrap your code with 3 backticks (```) on the line before and after. If you specify the language after the first set of backticks, that'll help us with syntax highlighting.

      ```(only write python, html,... without parentheses)
      <p>This is code!</p>
      ```

kodunu bir de bu ekilde yazmay覺 dene, 繹b羹r t羹rl羹 python bolua kar覺 duyarl覺 bir dil olduu i癟in kodunu kontrol etmem zorla覺yor.

Ismail KO
Ismail KO
1,748 Points

i have checked your code and your code seems like has problem and i will try repair, just be careful for how much blanks in your code before write statements, if you need help do not hesitate and ask it to me. (Kodunu kontrol ettim ve bir s覺k覺nt覺 var ve kodunu d羹zelteceim, kodu yazarken doru miktarda ifadeden 繹nce doru miktarda boluk koyduuna emin ol, soraca覺n bir ey olursa bana sorabilirsin)