2
amount = input ("enter amount: ") hundredDollar = amount / 100 amount = amount % 100 fiftyDollar = amount / 50 amount = amount % 50 twentyDollar = amount / 20 amount = amount % 20 tenDollar = amount / 10 amount = amount % 10 fiveDollar = amount / 5 amount = amount % 5 oneDollar = amount / 1 amount = amount % 1 quarter = amount / .25 amount = amount % .25 dime = amount / .10 amount = amount % .10 nickel = amount / .05 amount = amount % .05 penny = amount / .01 amount = amount % .01 print(int(hundredDollar) + " hundred dollar bills") print(int(fiftyDollar) + " fifty dollar bills") print(int(twentyDollar) + " twenty dollar bills") print(int(tenDollar) + " ten dollar bills") print(int(fiveDollar) + " five dollar bils") print(int(oneDollar) + " one dollar bills") print(int(quarter) + " quarters") print(int(dime) + " dimes ") print(int(nickel) + " nickels") print(int(penny) + " pennies") 

So the objective of this program is to output maximum number of dollar bills that fit in the amount, then the maximum number of hundred, fifty, dollar bills, then 20, then 10, 5 and 1. After that, display the maximum number of quarters, number of dimes, nickels, and pennies.

For example $100 could be displayed as 10000 pennies, or 2 fifty dollar bills or 5 twenty dollar bills. But the correct answer is the maximum number of 100 dollar bills first: 1 one hundred dollar bill. Display only the amount of a denomination if it is not zero.

This issue I'm having is my input keeps reading as a string instead of an int how can I solve this issue

1
  • The issue is at the inputting line; The other details are superfluous. Commented Jun 8, 2018 at 7:28

3 Answers 3

4

You can use the built-in functions int() or float() to return the string as an int or float respectively and where appropriate.

For example:

amount = float(input("Enter amount:")) 

Will set amount to a float constructed from the user input.

Other Improvements

Looking at the code you have provided, the other improvements you can make are as follows:

Use // to divide and floor a number.

For example:

hundredDollar = amount // 100 

Will set hundredDollar to a whole number indicating the maximum number of times 100 goes into amount. So, if the amount is 150, hundredDollar will be set to 1 as the amount is composed of one whole hundred dollar bill.

Use str() when concatenating a number with a string

When you concatenate (combine) a number with a string and the number comes first, you will need to first cast the number as a string. For example:

str(hundredDollar) + " hundred dollar bills." 

When a float is used and you want the output to display as an int i.e. 2 instead of 2.0 then you can either use the int() function or format the output. For example:

print( int(hundredDollar), "hundred dollar bills." ) 

Add validation for user input

When receiving input from the user it is advisable to add some validation to check if the data entered by the user is as expected - in this case, is a valid amount. This can be done using a try and except block for the data type and if statements to check if the data is within a valid range or meets additional requirements.

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

5 Comments

In the case of the question int is the appropriate. float won't work with // and the operator / will result in a float value instead of the desired int.
print(hundredDollar, "hundred dollar bills.") is simpler
@ArpadHorvath I would suggest that float is more appropriate to work with currency here since in the question it indicates that an amount can have pennies, nickels, etc. Float will work with // and would return the desired whole number. In terms of output, the float can be formatted as int.
@ArpadHorvath Your suggestion for the print statement is suitable when printing. The approach I have suggested would be required in all cases when concatenating a number with a string. I have updated the answer to more clearly reflect this.
Actually, neither float nor int are appropriate for use with currency. The correct type is fixed point, which in Python is accessible as decimal.
1

The reason why your input keeps reading as a string instead of an int is because input() returns a string object (this has been so ever since they removed the raw_input() function from Python 2 and made the input() function take its place).

Use the int() function to change the string to an integer, like so:

amount = int(input("Enter amount: ")) 

(This will also work with the float() function.)

However, if the user enters a string, this will produce an error. To avoid this, wrap the conversion to an integer in a try...except block:

try: amount = int(input("Enter amount: ")) except ValueError: #Perhaps prompt the user to try again here 

(Once again, this will work with the float() function)

Comments

0

use this

amount = eval(input ("enter amount: ")) 

It converts string from input to int

if you want float

amount = float(input ("enter amount: ")) 

1 Comment

Eval doesn't "convert string to int". It executes the string entered as a python expression. Now, if the user types a number, then you'll get back a number - but they can do all sorts of other things too. Never call eval until you have thoroughly sanitized the arguments.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.