Why this the result of this summation code wrong? [closed] - python

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!

Related

Python GCD program [closed]

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)

I am Wrote this python code but every time it's give me wrong answer [closed]

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)

Temperature conversion Table [closed]

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])

Asigning value to an object in python [closed]

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 am trying to assign 1 and 0 to a column in a data-frame which is actually a string. In the below dateset 'abc' is to be 1 and rest should be 0
Below is the sample datasest:
type={'type':['abc','abc','kk','arr','yg','np']}
df=pd.DataFrame(type,columns=['type'])
My code:
df['new']=np.where(df['type']=='abd',1,0)
df['new'].value_counts()
Output:
0 6
Name: new, dtype: int64
try this one instead np.where:
df['new'] = df['type'] == 'abc'
btw:you wrote 'abd' instead 'abc' so you have 6 - 0 :)

Why do I get None as a result [closed]

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.

Categories