0

I am currently learning python and I am trying to create a program that would allow a user to input their "change" and get a total. Personally I have been running it from the command line to test it as I go, but I'm having trouble getting it to respond the way I need it to.

Into the command line I put something like: filename.py 25 10 5 1

But the issue I'm having is that instead of accepting the numbers in the same line, I'm having to do something like:

filename.py
25
10
5
1
and then I'll get the total

What I want is:

filename.py 25 10 5 1
total

Here's the code I'm trying:

def coin_value(quarters, dimes, nickels, pennies):
     firsttotal = .25 * quarters + .10 * dimes + .05 * nickels + .01 * pennies
     total = round(firsttotal,2)
     currency_string = "${:,.2f}".format(firsttotal)
     print(f"The total value of your change is", currency_string)
coin_value(int(input()), int(input()), int(input()), int(input()))

Does anyone have any suggestions or know what I'm doing wrong?

1
  • 1
    Do you actually want to prompt them after the program is launched? Seems like you want to pass command line arguments, rather than reading anything after launch. sys.argv gives you basic access, but the argparse module (e.g. defining four positional arguments with type=int) is better for writing customizable, maintainable command line parsing (and it generates your usage messages for you). Commented Sep 23, 2021 at 0:49

3 Answers 3

1

Unlike others languages like C or C++ where you can specify what will you read from the terminal with scanf, the input function in python reads a whole line of text instead of just one element. So the first time that you execute int(input()) you would be reading "25 10 5 1" as a string, and then it would try to parse it as int, that would give you an error.

If you want to send the 4 values in one line I suggest the following:

quarters, dimes, nickels, pennies = map(int, input().split())

This would give you 4 variables with the information as an int in a single line.

Edit: I read the other comments and if you want to pass the values as command line arguments you want to use sys.argv:

import sys
quarters, dimes, nickels, pennies = map(int, sys.argv[1:])

Import sys first.

Sign up to request clarification or add additional context in comments.

Comments

0

you may be just need parse the parameter from command line; if you only want use input function, just modify a little like below:

def coin_value(quarters, dimes, nickels, pennies):
     firsttotal = .25 * quarters + .10 * dimes + .05 * nickels + .01 * pennies
     total = round(firsttotal,2)
     currency_string = "${:,.2f}".format(firsttotal)
     print(f"The total value of your change is", currency_string)

input_string = input()
quarters, dimes, nickels, pennies = input_string.split()
coin_value(int(quarters), int(dimes), int(nickels), int(pennies))

2 Comments

Unfortunately that's still making me do filename.py, new line, then the numbers. I am needing it to accept filename.py with the input numbers in the same line.
you just need sys.argv to parse the parameter ;input function will lauch a new line accept your input
0

input will be called after you launch your program and wait for your input. As per the requirement which you have, sys.argv is the way to go. Try out this:

import sys
def coin_value(quarters, dimes, nickels, pennies):
     firsttotal = .25 * quarters + .10 * dimes + .05 * nickels + .01 * pennies
     total = round(firsttotal,2)
     currency_string = "${:,.2f}".format(firsttotal)
     print(f"The total value of your change is", currency_string)

quarters, dimes, nickels, pennies = sys.argv[1:]
coin_value(int(quarters), int(dimes), int(nickels), int(pennies))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.