+ 3
Why is this countdown going to the negative
8 Answers
+ 7
0 is < 100 so this block will be executed.
if == 0.... should be at the beginning of the loop
+ 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
+ 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
+ 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
+ 6
shorten the prog...
could make a nice quiz
+ 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
+ 2
oooh thanks a lot I've seen
0
лох