Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my program not quit after I input "q"?

Tags:

python-3.x

I just started learning to code in python and here is my first program. Why doesn't quit program when I enter the letter "q".

print("bu program sıcaklığa göre suyun durumunu gösterir")

while True:
    
   s=input("lütfen sıcaklığı giriniz.çıkmak içn q ya bas")
    
    
   if s=="q":
        print("çıkılıyor")

        break
    


   try:

        s_int=int(s)
        
   except ValueError:

        print("Lütfen sadece sayı giriniz")
        raise

    
   if s_int<0:
        
        print("Durum Buzdur")

   elif s_int>=100:

        print("Durum Buhar")

   else:

        print("Durum su")
            
like image 254
Ozgur Alptekın Avatar asked Dec 07 '25 02:12

Ozgur Alptekın


1 Answers

s=int(input("lütfen sıcaklığı giriniz"))

It's not possible for s to be equal to an alphabetical character "q", if you are immediately converting your input to an int (an int is a number).

If anything your program should immediately jump to line print("Lütfen sadece sayı giriniz") because attempting to convert a "q" to an integer raises a ValueError.

To fix this, you could first compare the raw input to "q". If it's not equal, then the next step you try converting it to an int.

s=input("lütfen sıcaklığı giriniz")

if s=="q":
    print("çıkılıyor")
    break

try:
    s_int = int(s)
except ValueError:
    print("Please enter a valid integer or 'q'")
    raise

if s_int<0:
    print("Durum Buzdur")
elif s_int>=100:
    print("Durum Buhar")
else:
    print("Durum su")
like image 140
14 revs, 12 users 16% Avatar answered Dec 12 '25 10:12

14 revs, 12 users 16%



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!