Home > Back-end >  Python Input Without Spaces
Python Input Without Spaces

Time:09-23

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?

CodePudding user response:

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.

CodePudding user response:

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))

CodePudding user response:

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))
  • Related