-2

I'm new to python and i'm trying to run my first module but i can't do that through gedit embedded terminal the code i wrote is

def num(b):
    print("Give A Number")
    b = b +5
    print(b)

I managed to run it easily in IDLE but couldn't in gedit terminal i tried giving it chmod +xand used ./num.py to run it also tried python num.py but nothing happens the terminal just moves to a new line

12
  • are you in the right folder? can you try python num.py? is the name of your file num.py or something else? PS: welcome to python and to ask ubuntu!
    – don.joey
    Aug 27, 2014 at 15:29
  • Yeah i'm sure i'd that right Aug 27, 2014 at 15:31
  • I asked three questions, you gave one answer.
    – don.joey
    Aug 27, 2014 at 15:32
  • 2
    Your code (a) doesn't define n and (b) doesn't actually call the num function you just defined. Aug 27, 2014 at 15:36
  • 1
    Add #!/usr/bin/env python3 at the beginning of your num.py file. save your file in home folder, open terminal and run it by using python3 num.py Aug 27, 2014 at 15:45

1 Answer 1

7

This answer is partially a summary of the comments, but here it is:

There is no way the code works in IDLE like it is pasted into your question: you do not call the function, and (thus) it has no argument as well.

To make it run from the terminal, your code (file) should at least include:

def num(b):
    print("Give a number ")
    b = b + 5
    print(b)

num(4)

Save it including language extension (.py) and run it by:

python3 /path/to/script

Alternatively, you can make it executable, and run it without python3 in front, but then you need to add the shebang to your script:

#!/usr/bin/env python3

def num(b):
    print("Give a number ")
    b = b + 5
    print(b)

num(4)

Then (if you made it executable), you can run it by:

/path/to/script

In that case, the language extension is not necessary.

If you are new to coding, I would say just use IDLE for writing, first testing and error correction, then use the terminal (like gnome-terminal) to check if all works as you planned.

As a side note: I am not sure what the script should do :)

I have the feeling you want something like:

#!/usr/bin/env python3

def num(b):
    number = input("Give a number ")
    output = b + int(number)
    print("If I add "+str(b)+ ", it makes "+str(output))

num(4)

Then you would get a result like:

Give a number

Then when you give a number:

7

After you press return, you get:

If I add 4, it makes 11

5
  • It printed something but not what i wanted it should let the user give b a value then call num(b) and it prints b+5 it's a silly program i know but it was a tutorial for beginners that i'm following.. And thanks for the help :) Aug 27, 2014 at 17:42
  • @AhmedGnedy added some information :) Aug 27, 2014 at 18:03
  • +1 but I've noticed many (if not all) of your answers have #!/usr/bin/env python3 shebang but I found (as a beginner) that #!/usr/bin/env python works and I wonder if that isn't the best future-proofing method as well as for backwards compatibility? Nov 28, 2019 at 1:43
  • @WinEunuuchs2Unix #!/usr/bin/env python3 has been calling python 3 for ages on Ubuntu, while python calls python2, also since ages. Note that if you call a script by python3 <script>, the shebang plays no role, the interpreter is called directly from the command. Nov 28, 2019 at 7:09
  • If the python2 shebang also works, it is because, coincidentally, the code works on both versions, which is not the case often. All my scripts are written and tested for python3. Nov 28, 2019 at 7:12

Not the answer you're looking for? Browse other questions tagged .