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.