Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
def gcd(m,n):
fm = []
for i in range(1, m+1):
if (m%i) == 0 :
fm.append(i)
fn = []
for j in range(1, n+1):
if (n%j) == 0 :
fm.append(j)
cf = []
for f in fm:
if f in fn:
cf.append(f)
return(cf[-1])
print(gcd(56,23))
Although, there are other ways to find gcd using while loop and math.gcd commands, but my mentor wants this solution working as its mentioned in his programming book. How can I solve IndexError: list index out of range in the above program.
You don't append anything to cf.
In your 2nd for loop, change fm.append(j) to fn.append(j)
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have wrote this Code for calculates the product of the first natural numbers, but it showing answer 1 every time. I don't where i did mistake?? Can you please help me find out my mistake in this code..
num = 10
i = 0
prod = 1
while i<=num:
i = i+1
prod*prod*i
print(prod)
The problem seems to be on the line prod*prod*i. The product needs to be accumulated and for this it should be exchanged for prod*=i.
The new snippet is:
num = 10
i = 0
prod = 1
while i<=num:
print(i)
i = i+1
prod*=i
print(prod)
Instead of prod*prod*i write prod=prod*i
Here we first take the input of the number of terms.Then we iterate the for loop and multiply the value of x with the initial value(=1).Then we assign the new value to p.
n=int(input('Terms: ')) #specifing the limit
p=1
for x in range(1,n+1):
p=p*x
print(p)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I need to write a program that displays Celsius to Fahrenheit temperature conversion in a table.
Question - What is the correct way to print a specific index within a list. My attempt comes from the answer to a similar exercise outlined here. Thanks in advance.
This is my code
temp = range(0 , 101, 10)
tbl = []
tbl2 = []
for i in temp:
cel = i
tbl.append(cel)
fah = (i * 9/5) + 32
tbl2.append(fah)
print(cel, fah)
print('Celsius\t\tFahrenheit')
print('-------------------')
print(tbl(0) + ':\t\t', tbl2(0))
print('-------------------')
This is my output
Functions are called with parenthesis(). Indexes are accessed with with brackets[0]:
print(tbl[0] + ':\t\t', tbl2[0])
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I was following a tutorial online and I did the same as the video but it gives me a different result?
total1 = 0
for b in range(1,5):
total1 += b
print(b)
The total should be 10, but instead I get 4. What did I do wrong?
You should be printing total1, instead of b.
This would be your code:
total1 = 0
for b in range(1, 5):
total1 += b
print(total1)
Hope this helps!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have been trying to horizontally reverse the matrix in Python.
input 1 0
1 0
output 0 1
0 1
Here is the code...
class Reversedmat(object):
def __list_oflist__(self,mat)
rows = len(mat)
columns = len(mat[0])
for i in range(0, len(mat)):
reversed_mat = ""
for j in range(0, len(mat[i])):
reversed_mat += str(mat[i][j]) + "\t" ## this is where im
print(reverse(reversed_mat))
if __name__ == "__main__":
mat =[[1,0],[1,0]]
print(Reversedmat().__list_oflist__(mat))
I have been trying to do this in Python, but I was not able to achieve. Any help is much appreciated! Thanks
A simple solution:
def reversemat(mat):
return [row[::-1] for row in mat]
print(reversemat([[1,0],[1,0]]))
# [[0, 1], [0, 1]]
besides, you have returned nothing from your function, so you will get None from your second print.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have the following script:
import math
scores = [3.0,1.0,0.1]
sum = 0
i=0
j=0
for s in scores:
sum = sum + math.exp(scores[i])
i=i+1
def myFunction(x):
math.exp(x)/sum
for s2 in scores:
print(myFunction(scores[j]))
j=j+1
But, the output I get is:
None
None
None
Why is that? How can I retrieve the correct values?
Thanks.
You forgot to return.
def myFunction(x):
return math.exp(x)/sum
print(myFunction(scores[j]))
Here you try to print something. But myFunction doesn't return anything to print.
You can use,
def myFunction(x):
return math.exp(x)/sum
This will solve the problem.