How Does the Code Prints 1 2 6 24 as Output and Not 24 6 2 1

Factorial number printing

a, b = 0, 1
while a < 19:
if a: print b,
a, b = a + 1, b * (a+1)

Use a in-line if statement to check if a is 1. If it's 1 then you know that you already went through the loop at least once.

How would you print number 1,2,3,4,5,6,7,8,18,19,20,21,22,23,24 in python?

A better way to print the above sequence is through a while loop:

max_num = 25
i = 1
while i < max_num:
if i == 9:
print(18)
i == 19
else:
print(i)
i += 1

The Sum of Consecutive Numbers

One way that came to my mind is concatenating values for each iteration:

limit = int(input("Limit:"))
base = 0
num = 1
num_total = 0
calculation = 'The consecutive sum: '
while base < limit:
calculation += f"{num} + "
base += num
num += 1

print(f"{calculation[:-3]} = {base}")
print(base)

#> Limit:18
## The consecutive sum: 1 + 2 + 3 + 4 + 5 + 6 = 21
## 21

The other way is printing value on each iteration without new line in the end (but you have additional + sign in the end here):

limit = int(input("Limit:"))
base = 0
num = 1
num_total = 0
print('The consecutive sum: ', end='')
while base < limit:
print(f"{num} + ", end='')
base += num
num += 1

print(f"= {base}")
print(base)

#> Limit:18
## The consecutive sum: 1 + 2 + 3 + 4 + 5 + 6 + = 21
## 21

Received a label value of 1 which is outside the valid range of [0, 1) - Python, Keras

Range [0, 1) means every number between 0 and 1, excluding 1. So 1 is not a value in the range [0, 1).

I am not 100% sure, but the issue could be due to your choice of loss function. For a binary classification, binary_crossentropy should be a better choice.



Related Topics



Leave a reply



Submit