I am trying to create a for loop and I want the output to be this:
0 is even.
1 is odd.
2 is even.
3 is odd.
4 is even.
5 is odd.
6 is even.
7 is odd.
8 is even.
9 is odd.
And all I have gotten this far:
my_range=range(0,11)
for i in my_range:
print(i)
and the output is all the numbers:
0
1
2
3
4
5
6
7
8
9
10
You just need to do is, change your print function as -
print(i," is", 'even.' if (i % 2 == 0) else 'odd.')
What you need to use here is conditional statements along with 'String Interpolation' in python.
The Below code will give you the desired output:
my_range=range(0,11)
for i in my_range:
if i%2:
print("{} is odd".format(i))
else:
print("{} is even".format(i))
Related
I am trying some stuff on the 3x+1 problem and noticed when I check if this num is divisible by two comes always true even that the second iteration should be false it goes like this for 20 iterations and then start to act right. I guess it is a long number issue?
num = 3656565605161651626526625291991265161656
while num != 1:
if (num % 2 == 0):
num /= 2
print("2")
else:
num *= 3
num += 1
print("3")
num2=f'{num:.1f}'
print(num2)
Here is the start of the result:
2
1828282802580825918440824287108404346880
2
914141401290412959220412143554202173440
2
457070700645206479610206071777101086720
2
You need to use integer division, not float.
num //= 2
Here are the first 20 lines of the output, where you can see it is working:
2
1828282802580825813263312645995632580828
2
914141401290412906631656322997816290414
2
457070700645206453315828161498908145207
3
1371212101935619359947484484496724435622
2
685606050967809679973742242248362217811
3
2056818152903429039921226726745086653434
2
1028409076451714519960613363372543326717
3
3085227229355143559881840090117629980152
2
1542613614677571779940920045058814990076
2
771306807338785889970460022529407495038
I need to formulate an iteration sequence that follows the output pattern below:
0
1
2
3
4
5
6
6
7
8
However my attempts insofar are yielding an output that returns back to iteration, rather than returning to a logical sequencing:
for i in range(10):
if i==7:
i=i-1
print(i)
0
1
2
3
4
5
6
6
8
9
I feel as though I am overlooking something incredibly simple, or something that is an obvious syntactical error?!
**Edit -
Firstly, thanks for taking the time to assist in this issue.
I did try all the following, unfortunately the general principle is not compatible with the program at hand.
So, to elaborate:
from datetime import date, timedelta
sDate=date(2021,7,24)
eDate=date(2016,1, 1)
delta=sDate-eDate
leapCycle=4
for i in range(delta.days):
date=sDate-timedelta(days=i)
year=date.year
month=date.month
day=date.day
leap_year=(year%leapCycle)
if day==28 and month==2 and leap_year==0:
temp=i-1
print(temp,date)
print(i,date)
and this is outputting
...
510 2020-03-01
511 2020-02-29
511 2020-02-28
512 2020-02-28
513 2020-02-27
...
1971 2016-03-01
1972 2016-02-29
1972 2016-02-28
1973 2016-02-28
1974 2016-02-27
...
As you can see, i is 'held' as required however now the date is reiterated (making the 'holding' obsolete). Please also note, that i is being used to cycle through data sets that correspond to the associated iterate number so it is important that i,date,and data do what is needed.
Thanks again
This is simpler
for i in range(9):
if i==7:
temp=i-1
print(temp)
print(i)
Your code is is causing you to over write your i value.
Maybe you could try something like this:
def print_with_condition(limit, condition):
for i in range(limit):
if condition(i):
print(i)
print(i)
print_with_condition(9, lambda x: x == 6)
The above code would produce this result:
0
1
2
3
4
5
6
6
7
8
EDIT 1
If you want a simpler approach:
for i in range(10):
if i != 9:
if i == 6:
print(i)
print(i)
The last number is not taken into account. You need to write for i in range(9)
In your for loop , when i=7 we temporarily make i=6 and print it. But the for loop had run till the i=7 state , hence for the next iteration 'i' will be 8 due to for loop behavior, which explains your output. You can achieve your desired output , by printing i-1 for every iteration from when i=7.
Code:
for i in range(10):
if i>=7:
print(i-1)
else:
print(i)
Output:
0
1
2
3
4
5
6
6
7
8
Just few lines of change in your code (added print inside if block) gives the desired results:
for i in range(9):
if (i == 7):
print (i-1)
print (i)
OR if you still want to iterate until range(10), this is the code:
for i in range(10):
if (i >= 7):
print (i-1)
else:
print (i)
Output:
0
1
2
3
4
5
6
6
7
8
i can't understand this convention range in python, range(len(s) -1) for representing all elements including the last one. For me makes no sense like when I print all elements the last one is not included in the list. Someone could help me understand this logic?
this>
s = "abccdeffggh"
for i in range(len(s) -1):
print(i)
result this
0
1
2
3
4
5
6
7
8
9
you are trying to print range to compiler you saying like this print numbers greter than 1 and below than 10 to the output not include 1 and 10 compiler only print 2,3,4,5,6,7,8,9 in your case
s = "abccdeffggh"
this s string length must be 11 if you print like this
s = "abccdeffggh"
for i in range(len(s)):
print(i)
you get output as 1,2,3,4,5,6,7,8,9,10 that is the numbers between 0 and 11
but in your code you have subtract -1 from the length then your range is become -1 and 10 then compiler print all numbers between the -1 and 10 it not include -1 and 10
try this code
s = "abccdeffggh"
print(len(s))
print(len(s)-1)
for i in range(len(s)):
print(i)
Please close if this is a duplicate, but this answer does not answer my question as I would like to print a list, not elements from a list.
For example, the below does not work:
mylist = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
print(%3s % mylist)
Desired output:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
Basically, if all items in the list are n digits or less, equal spacing would give each item n+1 spots in the printout. Like setw in c++. Assume n is known.
If I have missed a similar SO question, feel free to vote to close.
You can exploit formatting as in the example below. If you really need the square braces then you will have to fiddle a bit
lst = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
frmt = "{:>3}"*len(lst)
print(frmt.format(*lst))
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
items=range(10)
''.join(f'{x:3}' for x in items)
' 0 1 2 3 4 5 6 7 8 9'
If none of the other answers work, try this code:
output = ''
space = ''
output += str(list[0])
for spacecount in range(spacing):
space += spacecharacter
for listnum in range(1, len(list)):
output += space
output += str(list[listnum])
print(output)
I think this is the best yet, as it allows you to manipulate list as you wish. even numerically.
mylist = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
print(*map(lambda x: str(x)+" ",a))
I'm trying to print a half pyramid that stars on the left side in python.
So far, this is my code
for i in range(1,12):
for j in range(12 - i):
print(" ", end = " ")
for j in range(1, i):
print(j, end = " " )
print("\n")
and my output is
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
However, my output is meant to be in the opposite order:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
How can I make this change?
Just reverse the second loop -- the one that prints that actual numbers:
for j in range(i-1, 0, -1):
The last parameter controls the "step", or how much the variable changes on each loop iteration. Output:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
...
Reverse the range by adding the third argument (-1). Also format your numbers to use 2 places, so 10 is not pushing the last line to the right. Finally, the last print should probably not have \n, since that is already the default ending character of print:
for i in range(1,12):
for j in range(12 - i):
print(" ", end = "")
for j in range(i-1, 0,-1):
print(str(j).rjust(2), end = "" )
print()
You could just reverse the range that you print out as numbers
for i in range(1,12):
for j in range(12 - i):
print(" ", end = " ")
for j in reversed(range(1, i)):
print(j, end = " " )
print("\n")
The problem is in your second for loop, as you are looping from 1 to i, meaning you start off with 1 being printed first, and every following number until (not including) i.
Fortunately, for loops are able to go in reverse. So, instead of:
for j in range(1, i)
You could write:
for j in range((i-1), 0, -1)
Where the first parameter is signifies where the loop starts, the second is where the loop finishes, and the third signifies how large our jumps are going to be, in this case negative. The reason we are starting at i-1 and finishing at 0 is because loops start at exactly the first given number, and loop until just before the second given number, so in your given code the loop stops just before i, and so this one starts just before i as well, although you could remove the -1 if you wish to include 12 in the pyramid.