Vahid999
(Vahid)
February 6, 2025, 10:43pm
1
In the following code,
basic_text = """
SystemName water
SystemLabel water
\n"""
def basic(basic_text:str):
siesta = basic_text
siesta += "#\n# Definition of Atomic Species\n#\n"
siesta += "#\n# Atoms\n#\n"
siesta += "LatticeConstant \n"
print(siesta)
I am hoping that the following gets printed
SystemName water
SystemLabel water
Definition of Atomic Species
Atoms
LatticeConstant
Instead, I get blank. Any hints would be appreciated.
Thanks,
Vahid
MRAB
(Matthew Barnett)
February 6, 2025, 11:22pm
2
You pass a value to a function when you call it.
That code defines function, but doesn’t call it.
To call it and pass basic_text
, you would need to add basic(basic_text)
.
Vahid999
(Vahid)
February 6, 2025, 11:49pm
3
Thank you Matthew for your help. I modified the code so it reads
basic_text = """
SystemName water
SystemLabel water
\n"""
def basic(basic_text:str):
siesta = basic_text
siesta += "#\n# Definition of Atomic Species\n#\n"
siesta += "#\n# Atoms\n#\n"
siesta += "LatticeConstant 1.00 Ang\n"
basic(basic_text)
print(basic_text)
It prints the string basic_text. However, when I try to print(siesta), which is basic_text+three more lines, I get “NameError: name ‘siesta’ is not defined”. Is there a way to print all 5 lines, i.e., siesta?
Thanks
MRAB
(Matthew Barnett)
February 7, 2025, 12:50am
4
siesta
is a local variable inside basic
. You can’t get it from outside that function.
Maybe what you want is to return the value of siesta
.
If that’s the case, then put:
return siesta
at the end of basic
to return the value and then assign the result that was returned to a variable called siesta
outside the function with:
siesta = basic(basic_text)
Vahid999
(Vahid)
February 7, 2025, 1:07am
5
That is exactly what I needed. Thank you for your help.
Vahid