Python Forum
Autonomous Python Script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Autonomous Python Script
#1
good evening,

May I ask for your help again?
Quote:Create a plural.py script, capable of taking several words and displaying their plural.

Example:

$several.py dog, cat, bird
dogs
cats
birds

The script must be used in the terminal.

so far I wrote:
def several(a,b,c):
    plural_a=a+'s'
    plural_b=b+'s'
    plural_c=c+'s'

    print( plural_a+"\n",plural_b+"\n",plural_c+"\n")
several('cat','dog','camel')
And it works.
but somehow I can't launch it from my terminal.
I'm on Ubuntu.
the script is in the main directory
I used chmod +x several.py to make the script executable from the terminal
yet nothin happens -> is my script wrong?
Reply
#2
Insert at top of several.py

#!/usr/bin/env python3
or run in terminal

python3 several.py
Reply
#3
How do you try to launch it?
Reply
#4
(Mar-31-2022, 08:23 PM)deanhystad Wrote: How do you try to launch it?

Hello,
In the terminal I've tried :

python3 several.py
as suggested by Axel_Erfurt

also:
sudo python3 several.py
but nothing.
Reply
#5
(Mar-31-2022, 08:03 PM)Leyo Wrote: Example:

$several.py dog, cat, bird
As the example shows and as Axel_Erfurt emphasised, you have to start the script on the command line with arguments. So you have to do something to get those arguments. So make a script "test_arguments.py" like this:
#!/usr/bin/python3
import sys
print(sys.argv)
when you start this script with:
test_arguments.py  dog  cat  bird
Output:
['test_arguments.py', 'dog', 'cat', 'bird']
Here you see sys.argv is a list where sys.argv[0] is the name of the script and the following items are the arguments.
So when you change the script to:
import sys

for arg in sys.argv[1:]:
    print(arg)
... you get:
Output:
dog cat bird
Now you should know enough to finish your script.



(Apr-01-2022, 07:27 AM)Leyo Wrote: but nothing.
I think an error message appeared. You should always include the complete error message.
Reply
#6
(Apr-01-2022, 07:27 AM)Leyo Wrote: In the terminal I've tried :

python3 several.py

You should do this in the script folder , adjust /path/to/script

cd /path/to/script
python3 several.py
Reply
#7
f-string makes it easier

#!/usr/bin/env python3

def several(a,b,c):
    plural_a=f'{a}s'
    plural_b=f'{b}s'
    plural_c=f'{c}s'
 
    print(f'{plural_a}\n{plural_b}\n{plural_c}\n')
several('cat','dog','camel')
saved in /tmp as several.py

then in terminal

Output:
axel@Esprimo-P400:~$ cd /tmp axel@Esprimo-P400:/tmp$ python3 several.py cats dogs camels
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Re-write BASH script to Python script popi75 5 2,446 Apr-30-2021, 03:52 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020