This is my first time posting here. Sorry if it's not the correct way. In the following code, there are three loops iterations. I can understand why. But I need it to be only 2. Is there a way to do this?
weeks_x = 0
weeks_year = 52
count = 0
while weeks_x < weeks_year:
weeks_x = weeks_x + 25
count = count + 1
Adding a print() statement can be useful in debugging:
weeks_x = 0
weeks_year = 52
count = 0
while weeks_x < weeks_year:
print('weeks_x:', weeks_x, '- weeks_year', weeks_year)
weeks_x = weeks_x + 25
count =+ 1
output:
weeks_x: 0 - weeks_year 52
weeks_x: 25 - weeks_year 52
weeks_x: 50 - weeks_year 52
If you specifically needed two loop iterations:
weeks_x = 0
weeks_year = 52
count = 0
while count < 2:
print('weeks_x:', weeks_x, '- weeks_year', weeks_year)
weeks_x = weeks_x + 25
count =+ 1
count variable is counting the number of loops.
Alternatively, weeks_x * 2 will give the same result as the loop - otherwise if there is functionality that requires this I suggest looking at for loops with range().
Try debugging your code with simple print statements
weeks_x = 0
weeks_year = 52
count = 0
while weeks_x < weeks_year:
weeks_x = weeks_x + 25
count = count + 1
print(f'Iteration Number {count} and weeks_x is {weeks_x}')
The output makes it crystal why the loop runs for 3 times
Iteration Number 1 and weeks_x is 25
Iteration Number 2 and weeks_x is 50
Iteration Number 3 and weeks_x is 75
Since only after the 3rd iteration the value of weeks_x is enough to break this condition weeks_x < 52 it goes for 3 turns
You can limit the number of times the loop runs
weeks_x = 0
weeks_year = 52
count = 0
# while weeks_x < weeks_year: # REMOVE THIS
while count < 2: # ADD THIS
weeks_x = weeks_x + 25
count = count + 1
print(f'Iteration Number {count} and weeks_x is {weeks_x}')
Initializing before while
weeks_x = 0
weeks_year = 52
count = 0
weeks_x += 25
while weeks_x < weeks_year:
count += 1
print('Count {}'.format(count), 'Week Value {}'.format(weeks_x), sep='-->', end='\n')
weeks_x += 25
Output:-
Count 1-->Week Value 25
Count 2-->Week Value 50
Related
Assume I have the following dataframe:
Time Flag1
0 0
10 0
30 0
50 1
70 1
90 0
110 0
My goal is to identify if within any window that time is less than lets the number in the row plus 35, then if any element of flag is 1 then that row would be 1. For example consider the above example:
The first element of time is 0 then 0 + 35 = 35 then in the window of values less than 35 (which is Time =0, 10, 30) all the flag1 values are 0 therefore the first row will be assigned to 0 and so on. Then the next window will be 10 + 35 = 45 and still will include (0,10,30) and the flag is still 0. So the complete output is:
Time Flag1 Output
0 0 0
10 0 0
30 0 1
50 1 1
70 1 1
90 1 1
110 1 1
To implement this type of problem, I thought I can use two for loops like this:
Output = []
for ii in range(Data.shape[0]):
count =0
th = Data.loc[ii,'Time'] + 35
for jj in range(ii,Data.shape[0]):
if (Data.loc[jj,'Time'] < th and Data.loc[jj,'Flag1'] == 1):
count = 1
break
output.append(count)
However this looks tedious. since the inner for loop should go for continue for the entire length of data. Also I am not sure if this method checks the boundary cases for out of bound index when we are reaching to end of the dataframe. I appreciate if someone can comment on something easier than this. This is like a sliding window operation only comparing number to a threshold.
Edit: I do not want to compare two consecutive rows only. I want if for example 30 + 35 = 65 then as long as time is less than 65 then if flag1 is 1 then output is 1.
The second example:
Time Flag1 Output
0 0 0
30 0 1
40 0 1
60 1 1
90 1 1
140 1 1
200 1 1
350 1 1
Assuming a window k rows before and k rows after as mentioned in my comment:
import pandas as pd
Data = pd.DataFrame([[0,0], [10,0], [30,0], [50,1], [70,1], [90,1], [110,1]],
columns=['Time', 'Flag1'])
k = 1 # size of window: up to k rows before and up to k rows after
n = len(Data)
output = [0]*n
for i in range(n):
th = Data['Time'][i] + 35
j0 = max(0, i - k)
j1 = min(i + k + 1, n) # the +1 is because range is non-inclusive of end
output[i] = int(any((Data['Time'][j0 : j1] < th) & (Data['Flag1'][j0 : j1] > 0)))
Data['output'] = output
print(Data)
gives the same output as the original example. And you can change the size of the window my modifying k.
Of course, if the idea is to check any row afterward, then just use j1 = n in my example.
import pandas as pd
Data = pd.DataFrame([[0,0],[10,0],[30,0],[50,1],[70,1],[90,1],[110,1]],columns=['Time','Flag1'])
output = Data.index.map(lambda x: 1 if any((Data.Time[x+1:]<Data.Time[x]+35)*(Data.Flag1[x+1:]==1)) else 0).values
output[-1] = Data.Flag1.values[-1]
Data['output'] = output
print(Data)
# show
Time Flag1 output
0 0 0
30 0 1
40 0 1
50 1 1
70 1 1
90 1 1
110 1 1
i can't understand why the output of my code concatnates the digits instead of showing their sum:
#Get a number, and show the number of digits and the sum of the digits.
num = int(input('Enter a number: '))
j = 0
i = 1
k = 0
while i < num:
i = i*10
j += 1
k += (num - k)%i
print (f' The number has {j} digit(s), and the sum is: {k}')
Follow the code. Let's say num = 432:
i = 1 * 10 = 10
j = 0 + 1 = 1
k = 0 + (432 - 0)%10 = 2
---
i = 10 * 10 = 100
j = 1 + 1 = 2
k = 2 + (432 - 2)%100 = 2 + 32 = 34
---
i = 100 * 10 = 1000
j = 2 + 1 = 3
k = 34 + (432 - 34)%1000 = 34 + 398 = 432
This algorithm is most definitely not adding every digit. There are several ways to do what you intend in python. One way is inputting the number as a string and summing every digit casting them as integers inside a generator:
num = input('Enter a number: ')
total = sum(int(digit) for digit in num)
print(total)
If you want the number to be an integer since the beginning, you can also do this:
num = int(input('Enter a number: '))
total = 0
while num > 0:
digit = num%10
total += digit
num /= 10 # num //= 10 in python 3
print(total)
counter = 10
numbers = int(input("Enter a number between 10 and 99: "))
column = int(input("How many columns would you like? "))
for num in range(10, numbers):
for col in range(column):
counter += 1
print(num + 1, end= ' ')
print()
Trying to count from 10 to the input value provided and columns provided in Python but not getting it. I basically want like 5 numbers on top, 5 on bottom etc.
counter = 10
numbers = int(input("Enter a number between 10 and 99: "))
column = int(input("How many columns would you like? "))
output_string = ""
col_counter = 0
while (counter <= numbers):
output_string += str(counter)+" "
counter += 1
col_counter += 1
if(col_counter == column):
print(output_string)
output_string=""
col_counter = 0
print(output_string)
This should do it just fine
do you want something like this?
>>> n = 30 # numbers
>>> c = 3 # columns
>>> for i in range(10, n+1):
... print(i, end='\t')
... if (i - 10) % c == 0:
... print()
... else:
... print()
...
10 11 12
13 14 15
16 17 18
19 20 21
22 23 24
25 26 27
28 29 30
>>>
So, I have 3 variables the total of which should be 120. I have done the verification of that. The variables should be equal to 0 or 20 or 40 or 60 or 80 or 100 or 120. I have done the verification for that as well but now what I need is for my program to check if variable 1 is 100 and variable 2 is 0 or 20 and variable 3 is 0 or 20 in terms to output a message. Then I need to check if variable 1 is 40 or 60 or 80 variable 2 is 0 or 20 or 40 or 60 or 80 and variable 3 is 0 or 20 or 40 or 60 in terms to output a different message.
if pass_credit + defer_credit + fail_credit != 120:
print("Total incorrect!")
elif pass_credit == 120 and defer_credit == 0 fail_credit == 0:
print("Progress")
break
elif pass_credit == 100 and defer_credit == 0 or 20 and fail_credit == 0 or 20:
print("Progress - module trailer")
break
That's what I have so far
Thanks in advance
the semantics of this line is wrong:
elif pass_credit == 100 and defer_credit == 0 or 20 and fail_credit == 0 or 20:
Either write
elif pass_credit == 100 and (defer_credit == 0 or defer_credit == 20) and (fail_credit == 0 or fail_credit == 20):
or write
elif pass_credit == 100 and defer_credit in (0, 20) and fail_credit in (0, 20):
If you want to use a general message for all the good combinations (with the values), use:
a, b, c = 80, 40, 0
message = "The values are: "
for i, var in enumerate([a, b, c]):
message += "Variable " + str(i + 1) + " is: " + str(var) + ", "
print(message[:-2] if a + b + c == 120 else "Total incorrect!")
Output:
The values are: Variable 1 is: 80, Variable 2 is: 40, Variable 3 is: 0
If you want a different message for every possible combination, continue your elifs to cover all options (with #gelonida's correction for the syntex). No smart way to do that.
I need to create a program that does a digital countdown from a set time. It needs to be printed so it reads Hours:minutes:seconds.
import time
count=int(input("Enter your start point"))
count2=int(input("Enter your start point"))
count3=int(input("Enter your start point"))
while count and count and count3 >0:
time.sleep(1)
print(count,+":",+":",+count3)
count -=1
count2 -=1
count3 -=1
import time
hours = 1
minutes = 0
seconds = 4
while not hours==minutes==seconds==0:
print str(hours)+":"+str(minutes)+":"+str(seconds)
time.sleep(1)
if minutes==seconds==0:
hours-=1
minutes=59
seconds=59
elif seconds==0:
minutes-=1
seconds=59
else:
seconds-=1
Try this. Can add padding to the numbers if required. Here is the POC.
>>1:0:4
>>1:0:3
>>1:0:2
>>1:0:1
>>1:0:0
>>0:59:59
>>0:59:58
>>0:59:57
>>0:59:56
I modified your code ,this will produce your required output.
Please let me know in terms of any query,
import time
count=int(input("Enter your start point"))
count2=int(input("Enter your start point"))
count3=int(input("Enter your start point"))
while count | count2 | count3 >0:
while(count>=0):
while(count2>=0):
while(count3>=0):
time.sleep(1)
print count,":",count2,":",+count3
count3 -= 1
count3 = 59
count2 -= 1
count2 = 59
count -= 1
In your old code AND operator is used , so it will terminate theexecution even if any variable is zero.
Output:
1 : 0 : 4
1 : 0 : 3
1 : 0 : 2
1 : 0 : 1
1 : 0 : 0
0 : 59 : 59
0 : 59 : 58
0 : 59 : 57
0 : 59 : 56
0 : 59 : 55
0 : 59 : 54
0 : 59 : 53
0 : 59 : 52