terms = int(input("Enter the terms: "))
x, y = 1, 2
count = 0
sum = 0.0
if terms <= 0:
print("Please enter a positive integer")
elif terms == 1:
print("Fibonacci sequence upto",terms,":")
print(x)
else:
print("Fibonacci sequence:")
while count < terms:
sum = z + x + y
print(x,end=" ")
z = x + y
x = y
y = z
count += 1
print("The sum of Fibonacci sequence:",sum)
'The error is the program cant defined the z and cant display the sum. Is that i put the sum code in wrong place?'
When you call
sum = z + x + y
you have not defined z.
Try this
x, y, z = 1, 2, 0
you are simply using z variable before even initializing it. The program doesn't know z exists and you are trying to use it to calculate sum.
first you need to initialize z and then use it to calculate sum.
You should replace "sum" with another name, try "summation"
Since you didn't defined z in first 14 lines, you can't use it in line 15. You should define z before using it.
Your sum calculation is not correct at all.
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')
https://www.urionlinejudge.com.br/judge/en/problems/view/1145
Write an program that reads two numbers X and Y (X < Y). After this, show a sequence of 1 to y, passing to the next line to each X numbers.
Input
The input contains two integer numbers X (1 < X < 20) and Y (X < Y < 100000).
Output
Each sequence must be printed in one line, with a blank space between each number.
My code is here:
x,y = raw_input().split(" ")
x = int(x)
xr = x
y = int(y)
lis = []
for i in range(1, y+1):
lis.append(i)
j = 0
for i in range(1, y+1):
while j <= x:
try:
if j < x:
print str(lis[j]),
j=j+1
else:
if x == y:
break
else:
print ""
x = xr + x
except IndexError:
break
Code's output is accurate. But the website is not accepting my code for some reason. Please help me find the bug?
Maybe the problem in on the line print str(lis[j]),, which add a space after the third digit and the site don't consider this valid, the problem say "with a blank space between each number." and maybe this is considered invalid.
Another possible solution is to split the list with the X numbers in lists of Y elements and print them with something like print " ".join(lis[Xs:Xf])
If you need just print you probably don't need list.
I don't know how to format code from phone.
sx,sy = raw_input().split(" ")
x = int(sx)
y = int(sy)
lines = int(y/x)+1
for line in range(lines):
offset = x*line
for i in range(x):
print i+offset,
if i+offset>=y:
print
break
print
x=0
y=0
while 1==1:
while y!=5:
y=y+1
print(str(x) + str(y))
else:
x=x+1
#NOW GO TO WHILE 1==1 AND DO THAT AGAIN
This code should print 01; 02; 03; 04; 05 and then it should print 11; 12; 13; 14; 15. But in reality it does only the first five prints because I don't know how to get to the start again after else:.
EDIT: I am so sorry, i tried to make the code easier to understand and i made few mistakes instead, that werent really a problem.
Here's a working code with a similar structure than yours :
x = 0
y = 0
while x != 2:
while y != 5:
y = y + 1
print(str(x) + str(y))
else:
y = 0
x = x + 1
But please don't do that. Instead :
for x in range(2):
for y in range(5):
print '%d%d' % (x,y+1)
I would say a better approach is to do a nested for loop.
from itertools import count
for x in count():
[print('{}{}'.format(x, y)) for y in range(1, 6)]
and it's Pythonic (hope that wasn't your homework).
Just remove else: and use formatted print to avoid printing the sum.
A better version of your code is:
x = 0
while 1 == 1:
y = 1
while y <= 5:
print '%d%d' % (x,y)
y = y+1
x = x+1
First of all your code outputs:
1
2
3
4
5
and then stops. What you are asking for is this:
01
02
03
04
05
11
12
13
[...]
To get this output you need an infinite loop which continuously increments x to do this start off with this piece of code:
x = -1
while True:
x += 1
You then need a loop which will increment y from 1 to 5 and print the string concatenation of x and y:
for y in range(5):
print(str(x) + str(y+1))
Nest the for loop in the while loop and voila!
x = -1
while True:
x += 1
for y in range(5):
print(str(x) + str(y+1))
Background: I was trying to create a code that would make Python generate a pattern from a 3 x 3 grid of dots, such that once a dot is chosen, the next dot can only be adjacent to it, and that no dot can be used twice. The following code is my input into Python for generating a 5-dot pattern for a 3 x 3 grid.
import random
x_arr=[]
y_arr=[]
def addTolist(m,n):
x_arr.append(m)
y_arr.append(n)
grid = [[1,2,3],[4,5,6],[7,8,9]] #rows of dots from top to bottom
dot = random.choice(range(1,10)) #random dot from the grid is chosen
for i in range(3):
for j in range(3):
if dot == grid[i][j]:
x = i
y = j
break
num = [dot]
for i in range(5): # loop 5 times
print('dot:',dot)
# every time empty the list
neighbours = []
x_arr=[]
y_arr=[]
#choosing only the adjacent numbers (neighbours) to the current dot
if y-1 >= 0:
neighbours.append(grid[x][y-1])
addTolist(x,y-1)
if x+1 < 3:
neighbours.append(grid[x+1][y-1])
addTolist(x+1,y-1)
if x-1 >= 0:
neighbours.append(grid[x-1][y-1])
addTolist(x-1,y-1)
if x-1 >= 0:
neighbours.append(grid[x-1][y])
addTolist(x-1,y)
if y+1 < 3:
neighbours.append(grid[x-1][y+1])
addTolist(x-1,y+1)
if y+1 < 3:
neighbours.append(grid[x][y+1])
addTolist(x,y+1)
if x+1 < 3:
neighbours.append(grid[x+1][y+1])
addTolist(x+1,y+1)
if x+1 < 3:
neighbours.append(grid[x+1][y])
addTolist(x+1,y)
dot = random.choice(neighbours)
while dot in num:
dot = random.choice(neighbours)
num.append(dot)
position = neighbours.index(dot)
x = x_arr[position]
y = y_arr[position]
Output:
('dot:', 6)
('dot:', 2)
('dot:', 3)
('dot:', 5)
('dot:', 7)
Is there any way that I can keep generating these patterns (without repetition) until it equals to the pattern [1,2,6,9,8] and find how many trials it took to get to this pattern [1,2,6,9,8]? I assume that I would have to get the output into a single list first, but I have been struggling to do so. Any help is greatly appreciated.
Try this, I think it would work:
import random
x_arr=[]
y_arr=[]
index = 0
pattern = [1,2,6,9,8]
def addTolist(m,n):
x_arr.append(m)
y_arr.append(n)
grid = [[1,2,3],[4,5,6],[7,8,9]] #rows of dots from top to bottom
dot = random.choice(range(1,10)) #random dot from the grid is chosen
guess = 1
while dot!= pattern[index]:
print("Wrong guess",dot)
guess += 1
dot = random.choice(range(1,10))
for i in range(3):
for j in range(3):
if dot == grid[i][j]:
x = i
y = j
break
num = [dot]
print('dot:',dot)
for i in range(4):
index +=1
# every time empty the list
neighbours = []
x_arr=[]
y_arr=[]
#choosing only the adjacent numbers (neighbours) to the current dot
if y-1 >= 0:
neighbours.append(grid[x][y-1])
addTolist(x,y-1)
if x+1 < 3:
neighbours.append(grid[x+1][y-1])
addTolist(x+1,y-1)
if x-1 >= 0:
neighbours.append(grid[x-1][y-1])
addTolist(x-1,y-1)
if x-1 >= 0:
neighbours.append(grid[x-1][y])
addTolist(x-1,y)
if y+1 < 3:
neighbours.append(grid[x-1][y+1])
addTolist(x-1,y+1)
if y+1 < 3:
neighbours.append(grid[x][y+1])
addTolist(x,y+1)
if x+1 < 3:
neighbours.append(grid[x+1][y+1])
addTolist(x+1,y+1)
if x+1 < 3:
neighbours.append(grid[x+1][y])
addTolist(x+1,y)
if pattern[index] not in neighbours: # if wrong Pattern is entered
print("Wrong Pattern can not generate",pattern[index] , "After",pattern[index-1])
break
dot = random.choice(neighbours)
guess += 1
while dot != pattern[index]: # for checking pattern
print("Wrong guess",dot)
dot = random.choice(neighbours)
guess += 1
print('dot:',dot)
num.append(dot)
position = neighbours.index(dot)
x = x_arr[position]
y = y_arr[position]
print("Total guess :",guess)
Output:
Wrong guess 3
Wrong guess 7
Wrong guess 5
Wrong guess 8
Wrong guess 5
Wrong guess 4
dot: 1
dot: 2
Wrong guess 4
Wrong guess 1
Wrong guess 5
Wrong guess 1
Wrong guess 1
dot: 6
Wrong guess 8
Wrong guess 8
dot: 9
dot: 8
Total guess : 18
Modifications:
Instead of checking unique variable , I am checking for pattern, So I
take pattern list.
I am checking for range(4) instead of 5 because 1st number is
already generated and so why waste computing 6th random number when
we are not even printing it.
I take guess variable to keep track of number of guesses.
Note : I am checking for if pattern is wrong then it will print wrong pattern .
For example: if pattern =[1,2,3,9,8] it will give output as:
Wrong guess 7
Wrong guess 4
dot: 1
Wrong guess 5
Wrong guess 4
Wrong guess 5
dot: 2
Wrong guess 4
dot: 3
Wrong Pattern can not generate 9 After 3
Total guess : 9
Updated : If you want to count whole sequence as the one guess then:
import random
x_arr=[]
y_arr=[]
index = 0
pattern = [1,2,6,9,8]
def addTolist(m,n):
x_arr.append(m)
y_arr.append(n)
guess = 0
while True :
grid = [[1,2,3],[4,5,6],[7,8,9]] #rows of dots from top to bottom
dot = random.choice(range(1,10)) #random dot from the grid is chosen
for i in range(3):
for j in range(3):
if dot == grid[i][j]:
x = i
y = j
break
num = [dot]
for i in range(4):
# every time empty the list
neighbours = []
x_arr=[]
y_arr=[]
#choosing only the adjacent numbers (neighbours) to the current dot
if y-1 >= 0:
neighbours.append(grid[x][y-1])
addTolist(x,y-1)
if x+1 < 3:
neighbours.append(grid[x+1][y-1])
addTolist(x+1,y-1)
if x-1 >= 0:
neighbours.append(grid[x-1][y-1])
addTolist(x-1,y-1)
if x-1 >= 0:
neighbours.append(grid[x-1][y])
addTolist(x-1,y)
if y+1 < 3:
neighbours.append(grid[x-1][y+1])
addTolist(x-1,y+1)
if y+1 < 3:
neighbours.append(grid[x][y+1])
addTolist(x,y+1)
if x+1 < 3:
neighbours.append(grid[x+1][y+1])
addTolist(x+1,y+1)
if x+1 < 3:
neighbours.append(grid[x+1][y])
addTolist(x+1,y)
dot = random.choice(neighbours)
while dot in num :
dot = random.choice(neighbours)
num.append(dot)
position = neighbours.index(dot)
x = x_arr[position]
y = y_arr[position]
guess += 1
if num == pattern :
break
print("Total guess :",guess)
Output :
Total guess : 12
Note : I don't recommended to use this method as sometimes it gives total guess is 1 or most of the times it gives you timeout
because it is random it may happen that the required sequence may
never gets generated or running time exceeded. SO you can try put limit on the guesses like
instead of while True use for c in range(50): or what over range
you have to give.(And if timeout is coming run the code more number of
times till it gives you answer I tried 8-10 times before I got this
output)
Hope this will help you.