please tell me how can I add line break after every 5 numbers
i = 1
while i < 30:
print(i, end = ' ')
i += 1
Use:
i = 1
while i < 30:
print(i, end = '\n' if i % 5 == 0 else " ")
i += 1
Should post an example for clarity, this should do it
for i in range(1, 30):
print(i, end=' ')
if not i % 5:
print('\n')
Just use the modulo operator % with an if-block to check if i is a multiple of 5:
i = 1
while i < 30:
print(i, end=' ')
if i % 5 == 0:
print()
i += 1
Output:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29
Try using the Modulo funktion, which is done with % in python.
i=1
while i<30:
print(i, end=' ')
if i%5 == 0:
print()
i += 1
the Modulo funktion returns the remainder of the division i/5, which is 0 for 0,5,10 and so on, so every 5 steps.
print('\n') prints a newline
Related
[1 3 6 5 7 10 9 11 14 13 15 18 17 19 22 21 23 26]
I have to display these numbers with only one print command and a while loop.
I got it with several print commands but when I was challenged to use one, I just couldn't figure it out.(beginner btw)
Your list follows a pattern, it is +2 +3 -1. So, you can just write something like the following code
index = 0
number = 1
while(index < 18):
print(number)
index += 1
if(index%3 == 1):
number += 2
elif(index%3 == 2):
number += 3
elif(index%3 == 0):
number -= 1
I had knowledge about java so I tried writing an if block within the for block saying,
for i in range(25):
if i == 9:
i = 18
print(i)
This code logic works in java but not in python. What should I do?
Use two ranges and the power of itertools.
import itertools
for i in itertools.chain(range(1, 9), range(18,25)):
print(i)
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
Using a while loop instead worked
i = 1
while i < 25:
if i == 9:
i += 9
print(i)
i += 1
Output
1
2
3
4
5
6
7
8
18
19
20
21
22
23
24
Use two loops
for i in range(1, 9):
print(i)
for i in range(18,25):
print(i)
I'm just asking a quick question on how to format how numbers are presented in a text file with however many columns I want.
ie.)
1 3 4
2 6 10
3 18 6
As of right now my text file is just writing a new line for ever iteration and I'm having trouble figuring out how to get it to right a new line after 3 iterations of the calculation.
for Number in range(1, 10001):
count = 0
for i in range(2, (Number // 2 + 1)):
if Number % i == 0:
count = count + 1
break
if count == 0 and Number != 1:
file.write(str(Number) + "\n")
You could track the count of numbers written to file, and each multiple of three, write a newline.
output_count = 0
for Number in range(1, 10001):
count = 0
for i in range(2, (Number // 2 + 1)):
if Number % i == 0:
count = count + 1
break
if count == 0 and Number != 1:
file.write(f'{Number} ')
output_count += 1
if output_count % 3 == 0:
file.write("\n")
Code Refactor
It is recommended to refactor the code into functions that tackle pieces of the problem, such as calculating primes versus input/output. For example, in Python 3, you could write the following:
from typing import List
def generate_primes(maximum: int) -> List[int]:
primes = []
for n in range(1, maximum):
count = 0
for i in range(2, (n // 2 + 1)):
if n % i == 0:
count = count + 1
break
if count == 0 and n != 1:
primes.append(n)
return primes
def write_to_file(filename: str, data: List[int], columns: int) -> None:
with open(filename, 'w') as file:
output_count = 0
for n in data:
output_count += 1
if output_count % columns == 0:
file.write(f"{n}\n")
else:
file.write(f"{n} ")
if __name__ == "__main__":
data = generate_primes(10001)
write_to_file("primes.txt", data, columns=3)
Output
2 3 5
7 11 13
17 19 23
29 31 37
41 43 47
53 59 61
67 71 73
79 83 89
...
What's the error here?. I cant print the CozaLoza, CozaWoza and LozaWoza. And it only prints from 1-109. I also wants to print 11 output per lines. How can I do that?
for num in range (1, 22):
if (num % 3 == 0):
print("Coza" )
elif (num % 5 == 0):
print("Loza")
elif (num % 7 == 0):
print("Woza")
elif (num % 3 and num % 5 == 0):
print(" CozaLoza")
elif (num % 3 and num % 7 == 0):
print("CozaWoza")
elif (num % 5 and num % 7 == 0):
print("LozaWoza")
else :
print (num)
I updated the code as following:
num = int ( input ( "Enter a number: " ) )
if num < 1 or num > 110:
print("From 1-110 number is allowed")
else :
for n in range ( 1, num ) :
name = str(n)
if n % 3 == 0:
name = "Coza"
elif n % 5 == 0:
name = "Loza"
elif n % 7 == 0:
name = "Woza"
print(name, end=" ")
if (n + 1) % 10 == 0:
print("") # print new line each 10 number prints
The answer for your question is the use of if (n+1) % 10 == 0 and under.
It prints newline for each 10 numbers because of every mupltiple of 10 is True on (n+1) % 10 == 0.
BUT it has some wrongs on other lines.
NOT the for num in range(1, num), it is for n in range(1, num). the num is same with a name of variable in loops and it is ambiguous.
you could omit bracket ( and ) at if, elif statement in Python. if (A) equals if A in python :)
anyway, this is the output when input is 55:
Enter a number: 55
1 2 Coza 4 Loza Coza Woza 8 Coza
Loza 11 Coza 13 Woza Coza 16 17 Coza 19
Loza Coza 22 23 Coza Loza 26 Coza Woza 29
Coza 31 32 Coza 34 Loza Coza 37 38 Coza
Loza 41 Coza 43 44 Coza 46 47 Coza Woza
Loza Coza 52 53 Coza
EDITED: (by updated question)
when num is 10, the code can be reached to elif num % 3 and num % 5 == 0? I don't think so. because it was already True on elif num % 5 == 0 above.
you need to fix your if conditions as like:
if num % 3 == 0:
...
elif num % 5 == 0:
if num % 3:
# CozaLoza
else:
# Loza
I am new to Python and Stackoverflow in general, so sorry if my formatting sucks and i'm not good at enlish.But i have a problem with this code.
print('Displays prime numbers from 1 to N.')
n = int(input('Please enter a value of n: '))
for n in range(1, n + 1):
if n >= 1:
for i in range(2, n):
if (n % i) == 0:
break
else:
print('They are',n,end=' ')
The result of the code when ran comes out looking like this:
Displays prime numbers from 1 to N.
Please enter a value of n:40
They are 1 They are 2 They are 3 They are 5 They are 7 They are 11 They are 13 They are 17 They are 19 They are 23 They are 29 They are 31 They are 37
but i want it like this:
Displays prime numbers from 1 to N.
Please enter a value of n:40
They are 1 2 3 5 7 11 13 17 19 23 29 31 37
If you're completely determined not to use the print function more than once inside the loop, you could set a flag to determine whether to print the first two words. Like so:
print('Displays prime numbers from 1 to N.')
n = int(input('Please enter a value of n: '))
first = 'They are '
for n in range(1, n + 1):
if n >= 1:
for i in range(2, n):
if (n % i) == 0:
break
else:
print(first + str(n), end=' ')
if len(first) > 0:
first = ''
The following solution may help you
print('Displays prime numbers from 1 to N.')
n = int(input('Please enter a value of n: '))
num = [] # Create empty list
for n in range(1, n + 1):
if n >= 1:
for i in range(2, n):
if (n % i) == 0:
break
else:
num.append(n)
# Write the print statement outside of the loop and use .join() function and for loop
#to print each element of the list look like the output you have posted
#
print('They are'," ".join(str(x) for x in num))
Output:
Displays prime numbers from 1 to N.
Please enter a value of n: 40
They are 1 2 3 5 7 11 13 17 19 23 29 31 37