.append python with list of imiona - python

I am new in python. I trying do some list with name and I don't know where is in here problem
>imiona = ["zbychu", "rychu", "zdzisiu"]
>print (imiona)
>imiona[1] = "rychu2"
>print (imiona[1])
>print ((len(imiona))
>imiona.append("Renata")
>print (imiona)
and the error showing:
imiona.append("Renata")
^
SyntaxError: invalid syntax

your line:
print ((len(imiona))
has three opening parenthesis '(' and only two closing ')'

Try this, looks like you have missing/extra parenthesis:
def main():
imiona = ["zbychu", "rychu", "zdzisiu"]
print(imiona)
imiona[1] = "rychu2"
print(imiona[1])
print(len(imiona))
imiona.append("Renata")
print (imiona)
main()

Related

Appending charaters in a string

I am trying to append characters in a string but I am getting problem with the loop. I don't know the command to append characters in the string:
import string
import random
def main():
generateRandomNumbers()
def generateRandomNumbers():
nameLength = 10
for i in range(nameLength)
x = random.choice(string.ascii_lowercase )
uname.append(x)
print (x)
size = 0
nameLength=0
if __name__ == "__main__":
main()
I am getting following error message:
File "Fl.py", line 8
for i in range(nameLength)
^ SyntaxError: invalid syntax
You are getting the syntax error because you are missing a colon at the end of your for loop. It should look like this:
for i in range(nameLength):
As Kenan has already said, I would recommend using the + operator to append to a string. This article has some decent examples that should put you on the right path.

Trying to generate an array with an exec function in Python throws an error

I am trying to generate the next code in Python:
atribute_name = 'atr1'
exec("list_%s = []", atribute_name)
So the expected result should be:
list_atr1 = []
But when I execute it I get the next error message:
TypeError: exec: arg 2 must be a dictionary or None
Why is it happening?
Instead of comma, you should replace it with % for string concatenation.
exec("list_%s = []" % atribute_name)
You are passing second argument for exec function.
Instead you just need to format your string.
Below sample code can help you.
atribute_name = 'atr1'
str1 = "list_%s = []" % atribute_name
print str1
exec(str1)
print list_atr1

print "message", var = raw_input()

print "Question?",
answer = raw_input()
The error:
Brians-Air:PythonFiles Ghost$ python ex11.py
File "ex11.py", line 1
print "How old are you?
^
SyntaxError: EOL while scanning string literal
I removed the "," and the interpreter gave an error. My thought was that removing the "," would give a new-line and request input on this new-line.
My question is why is the "," after the print statement necessary? Is this just the syntax coded into Python?
here is what you need to write:
while True:
print 'Question?'
answer = raw_input(' >')
if answer == ('done'):
break
print answer

concatenating string in python

I am converting a command line to a python string. The command line is:
../src/clus -INFILE=../input/tua40.sq -OUTPUT=OUT
The python statement is:
c_dir = '~/prj/clus/'
c_bin = c_dir + 'src/clus'
c_data = c_dir + 'input/tua40.sq'
c = LiveProcess()
c.executable = c_bin
c.cwd = c_dir
c.cmd = [c.executable] + ['-INFILE=', 'c_data, '-OUTPUT=OUT']
Problem is the c.cmd at the end looks like
~/prj/clus/src/clus -INFILE= ~/prj/clus/input/tua40.sq ...
Not that there is a 'space' after '=' which causes the program to report an error.
How can I concatenate '=' to the path?
LiveProcess is expecting an argv-style list of arguments. Where you want to make one argument, you need to provide one string. So use concatenation to make the string:
c.cmd = [c.executable] + ['-INFILE='+c_data, '-OUTPUT=OUT']
Also, no need for the list addition:
c.cmd = [c.executable, '-INFILE='+c_data, '-OUTPUT=OUT']
Why don't you just concatenate string like this:
a = 'A'+'B'
then
a == 'AB'
that is in your example
['-INFILE=' + c_data, '-OUTPUT=OUT']
Given that it looks like you're concatenating paths, you should be using os.path.join, not regular string concat.
Try this:
c.cmd = [c.executable] + ['-INFILE='+c_data, '-OUTPUT=OUT']

Using if-condition to check filename

I'm doing a real silly mistake in Python but unable to find what it is
I'm doing something like this in python
filename="file1"
if name == 'file1'
print 1
I'm getting an invalid syntax error
You are missing a colon
filename="file1"
if name == 'file1':
print 1
You need to put a colon at the end of the if statment
filename="file1"
if name == 'file1':
print 1
what is name?? did you define it elsewhere?? I assume its "filename" instead, so
filename="file1"
if filename == 'file1':
print 1
if "name" is defined, then the problem is indeed the ":" at the end of "if" line.

Categories