+ 3

Why is this countdown going to the negative

https://code.sololearn.com/cemhJY2KwrVa/?ref=app

3rd Mar 2018, 6:17 AM
Benedict 47
Benedict 47 - avatar
8 Answers
+ 7
0 is < 100 so this block will be executed. if == 0.... should be at the beginning of the loop
3rd Mar 2018, 6:31 AM
Oma Falk
Oma Falk - avatar
+ 15
because you have a logical error in the second if where num_1 < 100 where it is always true as num_1 is decreasing as: -1 < 100 is true, then will print it -2 < 100 is true, then will print it -3 < 100 is true, then will print it and so on... so to break this if you must add a condition to this statment num_1 > 0 that is : num_1 < 100 and num_1 > 0: below the error was corrected try it: num=input("") num_1=int(num) while True: #countdown beggins# if num_1>100: print("number too big") break elif num_1<100 and num_1>0: print(num_1) num_1=num_1-1 elif num_1==0: print("end of countdown") break
3rd Mar 2018, 6:37 AM
Mohammad Dakdouk
Mohammad Dakdouk - avatar
+ 10
Here's an edited version [edit: modified to allow entry of 100 and to prevent endless loop if negative] num = int(input()) while True: # countdown begins if num > 100: print("number too big") break if num < 0: print("number can't be positive") break elif num == 0: print("end of countdown") break else: print(num) num -= 1
4th Mar 2018, 4:47 PM
David Ashton
David Ashton - avatar
+ 6
Because your cycle is infinite. Basically within the condition "elif num_1 <100" you are giving way to the program showing negative numbers. After that condition you have one that will never be evaluated (elif num_1 == 0) since the condition you have above will always be executed. You have three ways to solve your error: 1. Move the last elif inside the previous block: while True: if num_1> 100: print ("number too big") break elif num_1 <100: print (num_1) num_1 = num_1-1 if num_1 == 0: print ("end of countdown") break 2. Change the last elif to an if: while True: #countdown beggins # if num_1> 100: print ("number too big") break elif num_1 <100: print (num_1) num_1 = num_1-1 if num_1 == 0: print ("end of countdown") break 3. use logical operators in the second condition, in such a way that you verify that the number is greater than 0 and less than 100 while True: #countdown beggins # if num_1> 100: print ("number too big") break elif num_1> 0 and num_1 <100: print (num_1) num_1 = num_1-1 elif num_1 == 0: print ("end of countdown") break
3rd Mar 2018, 6:40 AM
Mickel
Mickel - avatar
+ 6
shorten the prog... could make a nice quiz
3rd Mar 2018, 6:52 AM
Oma Falk
Oma Falk - avatar
+ 3
Hi Benedict 47, You should interchange the positions of the 2 elifs. The loop never ends because negative numbers are still less than 100 so it never goes to the second elif. It should be like this: num=input("") num_1=int(num) while True: #countdown beggins# if num_1>100: print("number too big") break elif num_1==0: print("end of countdown") break elif num_1<100: print(num_1) num_1=num_1-1
3rd Mar 2018, 6:36 AM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
+ 2
oooh thanks a lot I've seen
3rd Mar 2018, 6:32 AM
Benedict 47
Benedict 47 - avatar
0
Đ»ĐŸŃ…
5th Mar 2018, 4:41 AM
Karidakuri