-2

Edit(07/08/2023)

I'm new to coding and was using the terminal/code wrong and did not understand what I was doing wrong. Resolved now and learnt the lesson.

If you find yourself in a similar position, call the code with: Python filename.py 1 4 5 67 (enter the numbers/arguments in the first line as well as the call command), then it will work.

Thanks to all.

Original post -

I'm having an issue interacting with scripts via the terminal.

My code is as follows:

import sys

total = 1
del(sys.argv[0])
for argument in sys.argv:
    try:
        number = float(argument)
        total *= number
    except Exception as e:
        print(e)
        print("Only numbers can be provided")
        sys.exit(1)

print(total)

I should be able to type numbers into the terminal, then those numbers are used to multiply the total, and then print the multiplied value. However, when I type a single integer into the terminal, it just prints the integer, when I put in multiple numbers, it prints those numbers and does no multiplication.

It does not work in command prompt either.

I cannot get sys related elements to work in other code, I have googled error after error and I cannot find a resolution. Any ideas?

I expected the number input in the terminal to multiply the number in the total variable, to then be printed in the terminal, and then further numbers entered into the terminal to multiply the updated variable total.

I should be able to type multiple numbers without commas but it just does not work.

  • You blanked out the parts of the image where we could see how you execute the code. – mkrieger1 Aug 04 '23 at 21:43
  • 3
    You're not passing any CLI arguments. You're running your script once with zero arguments (`> python3 __.py`), and then just typing numbers into the prompt (`> 1 7 6 8`). Try `python __.py 1 7 8 9`. – Brian61354270 Aug 04 '23 at 21:43
  • 2
    Sitenote: why name your script `__.py`? – Brian61354270 Aug 04 '23 at 21:44
  • 1
    Does this answer your question? [How can I read and process (parse) command line arguments?](https://stackoverflow.com/questions/1009860/how-can-i-read-and-process-parse-command-line-arguments). The answers there show how to pass CLI arguments as well. – Brian61354270 Aug 04 '23 at 21:46
  • 2
    Instead of `del(sys.argv[0])`, just use a slice: `for argument in sys.argv[1:]:`. – Code-Apprentice Aug 04 '23 at 21:47
  • Your code works for me: https://replit.com/@codeguru/WavyFinancialPrintablecharacter. You will have to show how you are running it in order for us to help. I clicked on the Shell tab on the right and typed `python main.py 1 2 3 4 5` and get `120.0` as output. – Code-Apprentice Aug 04 '23 at 21:49
  • 1
    Please replace those links to images with _the actual text_, as per the [posting guidelines](/help/how-to-ask). Posting images of text is so disallowed that we had to bold and all-caps it in the rules, and yet people still do. Be better. – Mike 'Pomax' Kamermans Aug 04 '23 at 21:49
  • 2
    @Code-Apprentice that's because Replit runs a Python shell after you execute the program. OP is literally typing numbers into Powershell terminal and getting Powershell errors. – DallogFheir Aug 04 '23 at 21:51
  • @DallogFheir I used the linux shell, not the python console to test it. But yah, I think the OP isn't using command line arguments correctly. – Code-Apprentice Aug 04 '23 at 21:52
  • @Brian61354270 - Thank you for your comment, it is named that way because it was far easier to trouble shoot executing the script with a v simple name :) – wannabeunstuck Aug 06 '23 at 21:51
  • @DallogFheir Thanks for the comment, you are right I was not using it right, thanks for showing me the correct way. Just for context, I've been coding less than 30 days and this is part of a course I'm using to teach myself, but again thank you for helping. – wannabeunstuck Aug 06 '23 at 21:53
  • 1
    @Code-Apprentice Hi, I was using the terminal wrong which is why none of my trouble shooting was working, thank you for pointing out I needed to run the script and the numbers - Just for context, I've been coding less than 30 days and this is part of a course I'm using to teach myself, but again thank you for helping. – wannabeunstuck Aug 06 '23 at 21:54
  • @Mike'Pomax'Kamermans Noted for next time, apologies and thanks for pointing out. – wannabeunstuck Aug 06 '23 at 21:55
  • @wannabeunstuck no: noted for _now_, the [edit] link works perfectly fine, **fix your post**. And if it got closed in the mean time, **still fix your post** and then ask to get it reopened. – Mike 'Pomax' Kamermans Aug 06 '23 at 23:09
  • @wannabeunstuck I second Mike's suggestion, although, not the tone. You should fix this post now so that it will help other users in the future that have a similar problem. Please take the time to pay it forward. We volunteer to answer questions here and all we ask in return is that you clean up your question so it will help other people, too. – Code-Apprentice Aug 07 '23 at 03:57
  • @Mike'Pomax'Kamermans, I've updated the question and left for closing to pass forward the learning. – wannabeunstuck Aug 07 '23 at 08:48
  • 1
    @Code-Apprentice, I've updated the question and left for closing to pass forward the learning. – wannabeunstuck Aug 07 '23 at 08:48
  • @wannabeunstuck You should still include the example text from the console, but copy/paste it as text, not a screenshot. (Pro tip: text is much easier to redact than a screenshot.) – Code-Apprentice Aug 07 '23 at 18:21

1 Answers1

0

It looks like you are typing the numbers as a separate command in Powershell. This means that Powershell interprets them directly and doesn't run your python code. The error message when you type 1 7 6 8 confirms this. This is a Powershell error, not a python error.

Instead, you need to put them on the same line as the program you are running in order to send them to your python script:

C:\> python __.py 1 2 3 4 5

I recommend that you read more about how command line arguments work since you seem to be using them incorrectly.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268