How do I print 4 numbers in a row? I am a complete beginner. This is Python code I did for Fibonacci sequence.
For example, if I put 6 as an input, I get
0 1 1 2 3 5
However, I want to get it like
0 1 1 2
3 5
Code:
i = 1
n1 = 0
n2 = 1
seq = n1 + n2
num = int(input("Enter a positive number: "))
while i <= num:
print(n1, end = " ")
n1 = n2
n2 = seq
seq = n1 + n2
i += 1
print()
print(n1, end = " " if i % 4 else "\n")
Related
I've to write a function that takes the limit as an argument of the Fibonacci series and prints till that limit. Here, if the limit is fibonacci(10) then the output should be 0 1 1 2 3 5 8
I tried:
def fibonacci(number):
a = 0
b = 1
print(a,end = ' ')
print(b, end = ' ')
for num in range( 2 , number):
c = a + b
a = b
b = c
print(c, end = " ")
fibonacci(10)
The output came:
0 1 1 2 3 5 8 13 21 34
What should I do?
You can simply just add a check to see if the number resulting from a+b is going to be larger than the limit.
for num in range( 2 , number):
if a+b > number: return
c = a + b
a = b
b = c
print(c, end = " ")
Output:
0 1 1 2 3 5 8
The break statement exits the for loop so that no numbers over the limit will be printed. You can also use return instead of break. However, if you end up adding logic after the for loop, it will not be reached because return will exit the entire function.
SUGGESTION (courtesy of #OneCricketeer)
You may also use a while loop here instead of a range. This achieves the same output with cleaner, easier to understand code.
while (a+b < number):
c = a + b
a = b
b = c
print(c, end = " ")
def fibonacci(limit):
a=0
b=1
print(a,end=" ")
print(b,end=" ")
for i in range(limit):
c=a+b
if c<=limit:
print(c,end=" ")
a=b
b=c
x=int(input())
fibonacci(x)
Input:
10
Output:
0 1 1 2 3 5 8
I am trying to print the following pattern in python but am not getting the desired output :
0
2 2
4 4 4
6 6 6 6
I have already used the following code but am not getting the desired output
n = int (input("Enter number : "))
for i in range (n):
print (i *2 )
Try this:
n = int (input("Enter number : "))
for i in range (n):
print (' '.join([str(i * 2) for _ in range(i + 1)]) )
You need to print the number i+1 times.
for i in range (n):
for _ in range(i+1):
print (i * 2," ", end='') # print without newlines
print() # end the line
Following simple code could help:
n = int (input("Enter number : "))
j = 1
k = 0
for i in range(n):
s = str(k)+' '
print(s*j)
j += 1
k += 2
if n = 4, then it would print the following :
0
2 2
4 4 4
6 6 6 6
n = int (input("Enter number : "))
count = 1
for i in range (n):
res = str(i*2) + " "
print (res * (i+1))
print n
I'm having some logic errors with my program. I've been trying to solve this for the last couple of hours. It's supposed to print the sum of all the numbers that are either multiple of three or five.
my output
1.)enter an integer number (0 to end): enter an integer number (0 to end):
2.)enter an integer number (0 to end): 3+ = 3
expected output
1.)enter an integer number (0 to end): 3 = 3
2.)enter an integer number (0 to end): 3+5 = 8
below is my code.
while True:
answer = ""
num = int(input("enter an integer number (0 to end): "))
end_answer = 0
if num == 0:
exit()
for i in range(1, num+1):
if i%3==0 or i%5==0 :
answer += str(i)
end_answer += i
if i != num and (i%3==0 or i%5==0):
answer += "+"
print(str(answer) + " = " + str(end_answer) )
I've seen similar answers for this just not in python specifically
The following (properly indented) code will give you what you need:
while True:
num = int(input('Enter an integer number (0 to end): '))
if num == 0: exit()
answer = ''
end_answer = 0
sep = ''
for i in range(1, num+1):
if i % 3 == 0 or i % 5 == 0 :
answer += sep + str(i)
sep = ' + '
end_answer += i
if end_answer > 0:
print(str(answer) + ' = ' + str(end_answer) )
Note that it uses a variable separator sep to more cleanly print the item you're working out. A sample run follows:
Enter an integer number (0 to end): 2
Enter an integer number (0 to end): 3
3 = 3
Enter an integer number (0 to end): 10
3 + 5 + 6 + 9 + 10 = 33
Enter an integer number (0 to end): 38
3 + 5 + 6 + 9 + 10 + 12 + 15 + 18 + 20 + 21 + 24 + 25 + 27 + 30 + 33 + 35 + 36 = 329
Enter an integer number (0 to end): 0
You can simplify your code a lot by using the sum builtin and f-strings for printed text formatting. This will likely be more efficient as well.
Code
from itertools import count
counter = count(1)
while True:
num = int(input(f'{next(counter)}). Enter an integer number (0 to end): '))
if num == 0:
break
nums = [x for x in range(1, num + 1) if x % 3 == 0 or x % 5 == 0]
print(f'{" + ".join(map(str, nums))} = {sum(nums)}')
Output
1). Enter an integer number (0 to end): 3
3 = 3
2). Enter an integer number (0 to end): 9
3 + 5 + 6 + 9 = 23
3). Enter an integer number (0 to end): 15
3 + 5 + 6 + 9 + 10 + 12 + 15 = 60
4). Enter an integer number (0 to end): 0
Why in python a times table program doesn't work like this?
n = int(input("Type a number: "))
count = 10
while count < 0:
print(f"{count} x {n} = {n * count}")
count = count - 1
But this works correctly:
n = int(input("Type a number: "))
for i in range(1, 11):
print(f"{i} x {n} = {n * i}")
The result is (or should be), for example:
1 x 5 = 5
2 x 5 = 10
3 x 5 = 15
4 x 5 = 20
5 x 5 = 25
6 x 5 = 30
7 x 5 = 35
8 x 5 = 40
9 x 5 = 45
10 x 5 = 50
As a beginner I need to understand... and is it possible to use while in that situation?
This is python 3.x
You want to start count from 1 so that your loop increments rather than decrements. Your while loop conditional check is also incorrect as count is never less than 0. Try this:
n = int(input("Type a number: "))
count = 1
while count <= 10:
print(f"{count} x {n} = {n * count}")
count = count + 1
You need to set the condition in the while to be true to go through the loop. Thus we will say do while count is greater than or equal to zero.
n = int(input("Type a number: "))
count = 10
while count >= 0:
print(f"{count} x {n} = {n * count}")
count = count - 1
N1 = int(input("What interger do you wish to turn to binary (from 0 - 7 only!)"))
while (N1 > 7) or (N1 <0) :
N1 = int(input("You input must be from 0 - 7 only!"))
while True:
if N1 == 0:
print ("0 0 0")
break
else:
a = (N1 - 4)
if a < 0:
a = 0
else:
a = 1
N1 = a
b = (N1 - 2)
if b < 0:
b = 0
else:
b = 1
N1 = b
c = (N1 - 1)
if c < 0:
c = 0
else:
c = 1
print(a,b,c)
break
this program turns int to binary but it's skipping line 8-13 why?
In this bit:
a = (N1 - 4)
if a < 0:
a = 0
else:
a = 1
N1 = a
After the else, you first set the value of a to 1, and then copy that value to N1. So, your program forgot what N1 was, and the outcome for b and c will always be the same. (The same problem happens again for b and c.)
By the way, there are more efficient ways to figure out the binary representation of a number. Hint: use the & operator to find out whether the last bit of a number is 1, and use the >> operator to shift all bits of the number to the right.
The naive way to fix it is to remove N1 at each case and subtract different values when running if statements since N1 doesn't update while executing the second while loop. You should always avoid this happening in your codes:
N1 = int(input("What interger do you wish to turn to binary (from 0 - 7 only!)"))
while (N1 > 7) or (N1 <0) :
N1 = int(input("You input must be from 0 - 7 only!"))
while True:
if N1 == 0:
print ("0 0 0")
break
else:
a = (N1 - 4)
if a < 0:
a = 0
else:
a = 1
b = (N1 - 6)
if b < 0:
b = 0
else:
b = 1
c = (N1 - 7)
if c < 0:
c = 0
else:
c = 1
print(a,b,c)
break
But you can do it in an iterative style:
N1 = int(input("What interger do you wish to turn to binary (from 0 - 7 only!)"))
if (N1 > 7) or (N1 <0) :
N1 = int(input("You input must be from 0 - 7 only!"))
else:
binary = []
if N1 == 0:
print ("0 0 0")
for i in range (0,3):
if (N1 - pow(2,(2 - i))) >= 0:
binary.append(1)
N1 = N1 - pow(2,(2 - i))
else:
binary.append(0)
print binary
... I've test it and it works! ( it works only when a < 0 so only with N1 < 4 because a = N1-4, 4 - 4 = 0, 0 = 0, so 0 not < 0, and the if statement doesn't work with 4, and naturally doesn't work with 5, 6, 7... too )