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 5 years ago.
Improve this question
I have an indetntation issue with my code:
#!/usr/bin/python
import numpy as np
#Hit and mess method
A=0 #Integral left limit
B=1 #Integral right limit
C=1.0 #Maximum function value in interval [a,b]
N=100000 #Number of experiments
L=[10**x for x in range(7)] #List of powers of ten (1,10,100,...)
def F(X): #Function to integrate
return np.sqrt(1-(X**2))
for M in L:
NA=0 #Number count of hits
for J in range(M):
U=np.random.rand()
V=np.random.rand()
X=A+(B-A)*U
if F(X) > C*V:
NA=NA+1
P=NA/float(M) #Probability
I=C*(B-A)*P #Integral value
STD=np.sqrt(P*(1-P)/M)*C*(B-A)
print M,NA,P,I,STD
The error is:
File "HIT.py", line 19
U=np.random.rand()
^
IndentationError: expected an indented block
Thanks!
You are mixing tabs and spaces. Your code uses tabs in the 2 lines above U=np.random.rand(), however, indentation is provided by spaces for U=np.random.rand(). And there are many other examples of mixed spaces and tabs in your code.
For a few of the lines, the code looks like this (\t represents a tab):
for M in L:
\tNA=0 #Number count of hits
\tfor J in range(M):
U=np.random.rand()
\tV=np.random.rand()
\tX=A+(B-A)*U
You should only use spaces for indentation, if possible (it may not be possible when maintaining older code). You can read about this and other style issues in PEP 8.
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 4 months ago.
Improve this question
I'm trying to write a program that has a function that takes in a string and prints the number of capital letters in the first line, then prints the sum of their indices in the second line.
Here is what I came up with but I am getting errors. Anybody knows what I can fix on this code to make it run?
import sys
def string(s):
a={"UPPER":0}
for b in s:
if b.isupper():
a["UPPER"]+=1
print ("No. of Upper case characters : ", a["UPPER"])
ind = [idx for idx in range(len(val)) if val[idx].isupper()]
Sum=sum(ind)
print(Sum)
val = input("")
string(sys.argv[1])
The issue with your code is val is declared outside the scope of function.
You can rewrite your function like this.
def string(s):
ind = [idx for idx, i in enumerate(s) if i.isupper()]
print(f"Sum of index of upper case character : {sum(ind)}")
print(f"No. of Upper case characters : {len(ind)}")
Execution:
In [1]: string('Hello World')
Sum of index of upper case character : 6
No. of Upper case characters : 2
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 am running a big loop and I want to see the progress. I want the code to print the index of the iteration when the remainder of that number with 1e4 is zero. What I ve tried so far looks like this:
print(i if (i%1e4==0))
But it doesn seem to work. I cant use any functions such as qdtm because I am also using numba and it does not accept it. Any help?
The conditional operator requires an else value. If you don't want to print anything when the condition is false, you need to use an ordinary if statement, not a conditional expression.
if i % 1e4 == 0:
print(i)
If it really has to be a single statement, you could make use the the end argument to print() to print nothing, and then include the newline in the output you print.
print(str(i) + '\n' if i % 1e4 == 0 else '', end='')
for i in range(int(1e5)):
if (i%1e4==0):
print(i)
Outputs:
0
10000
20000
30000
40000
50000
60000
70000
80000
90000
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
Check out the screenshot and help this newb with why i'm getting this syntax error with the for loop even though im following the right syntax.
The code :
elif choice == 'AVERAGE':
import statistics
lst = []
n = int(input('Enter number of values to calculate mean of: ')
for i in range(0,n):
ele=int(input())
lst.append(ele)
The Error : Invalid Syntax for the ':' after 'range(0,n)'
You are spacing the items inside the for loop with double Tab, the indentation should be either 4 spaces or a single tab.
And you are missing a parenthesis closing in the n input line
See the modified code below.
elif choice == 'AVERAGE':
import statistics
lst = []
n = int(input('Enter number of values to calculate mean of: '))
for i in range(0,n):
ele=int(input())
lst.append(ele)
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 5 years ago.
Improve this question
Helli I'm a beginner programmer and I'm getting a print syntax error and I don't know why....
def Input_Q_bounds (lower,upper):
delta_x = .1
#since there are 100 iterations
J=np.zeros(101)
for i in range(101) :
Q=(i*delta_x)+(delta_x/2)
if lower <=(Q_i)<= upper :
Q_i =1
else :
Q_i=0
#now fill the matrix
J[i]=(Q+(9.5*(J[i-1])))/10.5
while (i==1):
J_analytical = Q*(np.exp(upper-10)+(np.exp(lower-10))
print(J_analytical)
break
Here's the error:
File "<ipython-input-135-25106d5ec500>", line 19
print(J_analytical)
^
SyntaxError: invalid syntax
Your parentheses in the line above are not balanced - you have four open parens and only three closing parens.
In the line before
print(J_analytical)
the brackets do not match!
J_analytical = Q*(np.exp(upper-10)+(np.exp(lower-10))
^
# change it to:
J_analytical = Q*(np.exp(upper-10)+(np.exp(lower-10)))
^