Using a break statement to exit a loop [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 want to detect when the program sees the value 26, i have a function that looks like this
val = 26
for n in range (0,101):
if n in val:
print(n,"is the value i am looking for")
break
else:
print(n)
I keep getting this error:
TypeError: argument of type 'int' is not iterable
What is wrong with this function

n in val checks whether the value n is in the iterable val. The correct check here is n == val

Related

Keyword argument (kwargs) function error 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 3 months ago.
Improve this question
Following is the code:
def my_funct(**kwarg):
print(kwarg[fn]*kwarg[sn])
print('enter 2 numbers to get product of')
a=input()
print('enter second number')
b=input()
my_funct(fn=a,sn=b)
The output is error saying 'fn is not defined'. What is the solution?
Kwargs is a dictionary where the variable name is the key and has type str. You are trying to find the value to the key which is saved in the variable fn but this variable hasn't been defined. Instead what you want is the value corresponding to the key 'fn', so you do
print(kwarg['fn'] * kwarg['sn'])
Edit: Because your corresponding values come from the input() function, you should make sure they are actually numbers and not strings. So when you do a=input() you should change that to a=float(input()). Same for b.

Print statement gives not callable error [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 4 years ago.
Improve this question
print = ('Tell me about your pet. ')
about_pet = input()
if 'dog'.lower() in about_pet == True :
print('ah, a dog')
if 'cat'.lower() in about_pet == True :
print ('ooh, a kitty')
print ('Thanks for the story')
when I run this code I get an error:
str object is not callable
What causes this?
print = ('Tell me about your pet. ') overwrites the function print as a string. It's not longer a function after that, so any time you try to call it as a function later, you're going to get errors.
Get rid of the = so you aren't changing what print is.

How to resolve "return" outside function error [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 4 years ago.
Improve this question
phone_letters =[" ", "1", "ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ","*","0","#" ]
key = 0
string = input("Enter a Letter: ",)
while key < 10 :
if string in phone_letters[key]:
print(key)
return key
else:
key = key+1
return "not found"
I am getting error 'return' outside function; I checked indentation and still the error continues.
return may only occur syntactically nested in a function definition
Source: https://docs.python.org/3/reference/simple_stmts.html#the-return-statement
Your return is not nested inside a function. Therefore, the error.
You can use print("not found") if you want to display the text.

Python: How to store string inputs on a array [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 8 years ago.
Improve this question
Hi I would like to store strings on a array. This strings are produced in this loop:
while (count < ts ):
dt=tb
t1=count+180
t2=t1+360
dt1=dt+t1
dt2=dt+t2
slice=stream.slice(dt1, dt2)
B=str(dt1)
E=str(dt2)
slice.write(station+'_'+comp[i]+'_'+B+'_'+E, format="MSEED")
count = count + 360
bb=[]
name=station+B+'_'+E
a=[str(name)]
bb.append(a)
But it doesn't work. The variable name is from type:
name=2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z
And I would like to have an array like that:
bb=[2011-05-22T23:42:00.000000Z_2011-05-22T23:48:00.000000Z, 2011-05-22T23:48:00.000000Z_2011-05-22T23:54:00.000000Z, 2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z]
But what bb returns me is an array with the last element called:
bb=[2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z]
If I do it manually:
bb.append('2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z')
It works perfectly because I put the ''. But I need to it in a automatic way.
Any suggestion?
Thanks in advance!
Declare bb outside the loop and a will be a list. You will get a list of lists(not in the way you asked for)

Eulers method- velocity and position as a function of time for runner [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 5 years ago.
Improve this question
Acceleration in a given point is given by a=5-0.004364*v^2
I want to store all the values of v and x and a in lists so that I later can plot them as a function of time. Keep in mind I'm a beginner. This is my code so far:
x_val=0
x=[]
x.append(x_val)
v_val=0
v=[]
v.append(v_val)
a_val=5.0
a=[]
a.append(a_val)
h=0.1
while x_val <=100:
v_val += (a_val*h)
x_val += (v_val*h)
a_val=(5-0.004364*(v_val**2)
a.append(a_val)
v.append(v_val)
x.append(x_val)
I'm getting a syntax error on "a.append(a_val)": invalid syntax
What am I doing wrong here? Please help
There is closed parenthesis missing in the below line
`a_val=(5-0.004364*(v_val**2)`

Categories