List cant store the input append attribute error - python-3.x

I don't know why I'm getting this error. The list has a function called append which adds the data to the list. But my code gives an error: int object has no attribute append. My codes input was
1
5
.....
.*...
.....
.....
I cant able to store the input in nested lists.Its shows an runtime error.Help me solve this.
t=int(input())
l=[]
for i in range(t):
n=int(input())
for x in range(n):
l.appen(list(input()))
I expected that the input will be stored as a nested list,but it throws an error int object has no attribute append.

If you are writing in Python's IDLE, this is a common mistake that happens with me. '1' looks almost same as 'l', so make sure.
t=int(input())
l=[]
for i in range(t):
n=int(input())
for x in range(n):
l.append(list(input())) # 'appen' is not a list attribute, 'append' is.
You can comment on this answer for more queries on this topic. I'll be eager to answer them
I hope it helped.

Related

python function only returns: <function x at 0x1215fd3a0> - how do I fix it?

Hello I have just started learning python, I keep getting stuck here: this is a data frame of titanic passengers, I want to sum all the ages of the survivors, however I need to exclude all 'nan' values from my calculations. This code does not return an error but <function age at 0x1215fd8b0>. I have also tried print age() but that gives me a syntax error. Any ideas? Thanks
survivors = titanic[titanic['Survived']==1]
def age():
summ=0
for age in survivors.Age:
if surivors.Age is float:
summ+=age
else:
summ=summ
return(summ)
print(age)
You need to actually call the function. And maybe learn about the sum function
survivors = titanic[titanic['Survived']==1]
def age(survivors):
return survivors['Age'].dropna().sum()
print(age(survivors))

I'm not getting expected result , I want to know what's wrong with the code

I want to know whether I can use Input function under for loops and Lists?
I'm using the latest version of python 3.7.4.
List=['apple','Pomegranate','orange']
K=print(input('Enter the Value:'))
if (K in List):
print("yes it's in the list")
else:
print("It's not in the list")
If I entered apple I'm getting the result as it's not on the list. I want to know whether we can use Input function under for loops and lists with if-else conditions.
Your issue is with the line
K=print(input('Enter the Value:'))
You do not need print here. Print is a function that takes a value, prints it to your screen and returns None. You passed the input to print, but you want to store the value in K, not print it to the screen (the user is entering the value so they probably do not need to see it again). So change this to:
K=input('Enter the Value:')
Here you can check your error with print function.
List=['apple','Pomegranate','orange']
K=print(input('Enter the Value:'))
print(K)
.....
K is None in this case.

how to convert an int array to string in python

I have a problem in converting an int array into string. Here is my part of code
response_list=[]
for key in json_response['response_code']:
if json_response['response_code'][key] ['0'] is True:
print('No such exist')
response_list.append('Check')
sys.exit()
What happens is this 'response_code' that is part of the output result of my entire code consist of either 0 and 1. So what I want to do is if 'response_code' is 0 in the output result print the needy and exit the whole operation.
I used for but it says 'int is not iterable with for loop. I tried using the dictionary:
response_list=[]
keydict=str(json_response['response_code'])
for key in keydict:
if keydict == ['0'] is True:
print('No such exist')
response_list.append('Check')
sys.exit()
I still get the int is not iterable
TypeError: 'int' object is not iterable
Can someone please explain how to solve the issue
p.s response_list stores the values so I can use later in my code.
What happens in my full code:
I have a list of urls where I want to get it scanned from VirusTotal API. So the API scans the list of urls one by one and if response code = 1 that means API outputs results. If the response code for another url becomes 0 it means API does not show result
You can only use a list for the iteration in For loop as you are using a dict and string doesn't work in you case
Trying to Iterate JSON
for key in json_response['response_code']
Trying to Iterate String
for key in str(json_response['response_code'])
Make sure you are using a valid list for the iteration.
Post your json_response['response_code'] structure if you need any help with the iteration of a specific value in the JSON.
if keydict == ['0'] is True:
This will not work,
If you want to check a variable
if keydict == ['0']: If you want to check for a specific value
if keydict: if you want to check if the keydict has a value

Indexes and ranges in python

I have this code:
def main():
if (len(sys.argv) > 2) :
P=list()
f= open('Trace.txt' , 'w+')
Seed = int(sys.argv[1])
for i in range(2, len(sys.argv)):
P[i-2] = int(sys.argv[i])
for j in range(0, len(sys.argv)-1) :
Probability=P[j]
for Iteration in (K*j, K*(j+1)):
Instruction= generateInstruction(Seed, Probability)
f.write(Instruction)
f.close()
else:
print('Params Error')
if __name__ == "__main__":
main()
The idea is that I am passing some parameters through the command line. the first is seed and the rest I want to have them in a list that I am parsing later and doing treatments according to that parameter.
I keep receiving this error:
P[i-2] = int(sys.argv[i])
IndexError: list assignment index out of range
what am I doing wrong
PS: K, generateSegment() are defined in a previous part of the code.
The error you see is related to a list being indexed with an invalid index.
Specifically, the problem is that P is an empty list at the time is being called in that line so P[0] is indeed not accessible. Perhaps what you want is to actually add the element to the list, this can be achieved, for example, by replacing:
P[i-2] = int(sys.argv[i])
with:
P.append(int(sys.argv[i]))
Note also that argument parsing is typically achieved way more efficiently in Python by using the standard module argparse, rather than parsing sys.argv manually.
It looks like you might be referencing a list item that does not exist.
I haven't used Python in quite a while but I'm pretty sure that if you want to add a value to the end of a list you can use someList.append(foo)
The problem is that you are assigning a value to an index which does not yet exist.
You need to replace
P[i-2] = int(sys.argv[I])
with
P.append(int(sys.argv[i]))
Furthermore, len(sys.argv) will return the number of items in sys.argv however indexing starts at 0 so you need to change:
for i in range(2, len(sys.argv)):
with
for i in range(2, len(sys.argv)-1):
As you will run into a list index out of range error otherwise

Can I read multiline input() during list comprehension?

This is just for my own curiosity about the language.
I have this working code:
for i in range(n):
name, grade = input(), int(input())
students += [[name, grade]]
Usually with a for loop that's constructing a list, I'm able to write a list comprehension, so I'm curious whether I can in this case.
I've tried a couple of experiments already, both were unsuccessful.
students = [[[name, grade]] for name in input() for grade in input() for i in range(n)]
but I get EOFerror. So maybe it is possible and there's some other error in my code, or maybe it's not at that error is caused by whatever strangeness occurs when I try this.
I also tried:
students = [[[name, grade]] for name, grade in zip(input(), int(input())) for i in range(n)]
Which raises an error informing me the second argument of zip must be iterable.
Yes, you can,
students=[[input(), input()] for i in range(3)]

Resources