Print list in terminal on multiple lines [duplicate] - python

This question already has answers here:
How to print X items in a list per line
(3 answers)
Closed 2 years ago.
i am really beginner and just starting programming with python since 2 weeks.
here is my question i cant find anywhere or i cant figure out the solution.
i want to see my result in terminal with multiple line , for example i want to calculate long for loop and the result show up this loop on terminal with very long a single line.
is there any solution for this?
and sorry for my bad english isnt my native language.
Code:
list = list(range(1,100))
for x in list:
if x % 3 == 0 or x % 5 == 0:
print(x, end=' ')
Output:
3 5 6 9 10 12 15 18 20 21 24 25 etc...
But i want this one:
3 5 6 9 10
12 15 18 20 21

You could add an if statement before you print, to determine whether you want to have end be a blank or a newline character.

x_list = range(1,100)
n = 0
for x in x_list:
if x % 3 == 0 or x % 5 == 0:
if n == 4:
print(x, end='\n')
n = 0
else:
print(x, end=' ')
n = n + 1

You can change your print statement to:
print(x)
If you would print some results in the same line, you can do a logic to print some characters inline and after some iteration print in next line.
list1 = list(range(1,100))
for num, x in enumerate(list1):
if x % 3 == 0 or x % 5 == 0:
print(x, end=' ')
if num % 10 == 0:
print()

Related

Count to 10 and Next line [duplicate]

This question already has answers here:
How to print every nth index of a python list on a new line?
(4 answers)
Closed 1 year ago.
I'm having trouble with my code, and I can't figure it out. My code is supposed to print 0 to input number subtracted by 1. Here's my code:
num = int(input(''))
if num > 50:
print('Number must be below 50')
else:
for num in range(0, num):
if num >= 0:
print(num, end=" ")
The output must next line every 10 numbers, but my output only displays one line.
Input:
15
Output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Output must be
Input:
15
Output:
0•1•2•3•4•5•6•7•8•9
10•11•12•13•14
num = int(input(''))
if num > 50:
print('Number must be below 50')
else:
for i in range(0, num):
print(i, end=" ")
if i % 10 == 9:
print()
For clarity, don't reuse the variable name num inside and outside the loop. You can easily introduce bugs that way.
You are not printing a new line after printing every 10 numbers. As you are starting from 0, you have to print a new line whenever you get a multiple of 9. The code should look like this:
num = int(input(''))
if num > 50:
print('Number must be below 50')
else:
for i in range(0, num):
if i >= 0:
print(i, end=" ")
if i % 9 == 0:
print("")

Regarding the def and return function in python

I was writing a program to write the multiplication table of number given by the user.
Here's my code:
def mul_table(n):
for i in range(1,11):
print(n,"x",i,"=",(n * i))
i = i + 1
return(n)
x = int(input("Enter the number: "))
x1 = mul_table(x)
print(x1)
But in the output it shows the number entered by the user also in the end like this (I know have written the value after "return" but if I leave it empty then it shows None, so how can I get rid of this?):
Enter the number: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
**5**
Can anyone tell me that how can the program return nothing (absolutely nothing, not even the "None" text or the number entered by the user) in PYTHON 3.9.1 ?
Your mul_table function handles the printing itself. It needs no return value, and you shouldn't print it even if it exists - you should just call the function:
def mul_table(n):
for i in range(1,11):
print(n,"x",i,"=",(n * i))
i = i + 1
# return statement removed here
x = int(input("Enter the number: "))
mul_table(x) # No need to capture the return value or print it
The problem is that mul_table method is returning n and you are printing it with print(x1). Remove this call to print if you don't need it.
There are several issues, here is a working version
def mul_table(n):
for i in range(1,11):
print(n,"x",i,"=",(n * i))
x = int(input("Enter the number: "))
mul_table(x)
You print inside the function, so you don't need to call mul_table in a print statement
You also don't need a return statement in the function (though it returns None if you omit the return statement or use a return statement without a return value)
i = i+1 inside the function is wrong, you are using a for statement where i gets values 1, 2, ..., 10 successively.

boolean string not counted in while/if statement in python[3.8]

I'm a beginner programmer and chose python[3.8] as my choice to learn. I don't even know what to ask or how to search this site. My program counts 30 to 40 and prints 'Go' for multiples of 3 and strings of 3 and remainders of 3. It counts Go's. Output should be 10 but it's 3. It's not counting the strings. There is no error msgs.
`enter code here`
s = '3'
x = 40
y = 30
num = 0
while y < x :
y=y+1
if y % 3 == 0 or y % 10 == 3 or s in 'y' :
print('Go',y)
num = num + 1
print(num, 'Go\'s')
I think the problem here is to understand how python works.
When you write s in 'y' it will always return false because y here is a character that composes the string which value is ythe character.
So what you'll need to do is use the function str(param) to convert your integer to a string with the same value.
This is a code that works the way you want to.
s = '3'
x = 40
y = 30
num = 0
while y < x :
if y % 3 == 0 or y % 10 == 3 or s in str(y) :
print('Go',y)
num = num + 1
y=y+1
print(num, 'Go\'s')

How to create python program, multiple of 3 with while loops

ok first sorry if my english is bad
i want to ask how to create python programs multiple of 3 with while loop, like this:
i=0
while i < 10:
i += 1
if i == 3:
continue
print(i)
output:
1
2
4
5
6
7
8
9
10
so i want to eliminate the number 3 6 9, can anyone help me ? i newbie :'v thanks.
It seems as if you want to print the numbers in reverse order (backwards/upside-down), so here is the snippet of code to achieve that task:
i=10
while i > 0:
if i%3 != 0:
print(i)
i-=1
The value of 'i' is the number which it will start counting down from.
i=0
while i < 10:
i += 1
if i % 3 == 0:
continue
print(i)

Find number based on modulo conditions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need to upgrade my code and I need to add function to find a number divided by 7, and getting modulo from 2,3,4,5,6 = 1.
I wrote that code:
a = 0
while a != 1:
x = 0
if(x % 2 == 1 and x % 3 == 1 and x % 4 == 1 and x % 5 == 1 and x % 6 == 1
and x % 7 == 0) == True:
a = 1
print(x)
else:
a = 1
x = x+1
I need to find first number divided by 7 and get all modulo from 2 to 6 ==1
Your loop is a while loop while a != 1.
However, regardless of what happens (your number x meets the criteria or not), a is immediately set to 1, meaning that after only 1 run, your loop will immediately end.
Furthermore, since x=0 at the start of any run of the loop, x will never meet your criteria.
Try re-designing your algorithm, because the way you're getting your number doesn't work at all.
Personally, I'd suggest using a while True, and you don't need a, only x.
Here's what I wrote
x = 0
while True:
if(x % 2 == 1 and x % 3 == 1 and x % 4 == 1 and x % 5 == 1 and x % 6 == 1 and x % 7 == 0):
print(x)
break
x += 1
The first number I got was 301.
You could loop through multiples of 7 and check the condition while doing so, you can use all() to check for those conditions as the following:
i = 7
while True:
if all(i%x==1 for x in range(2, 7)):
print(x)
break
i += 7

Categories