I need to code a get_input function that has loop validation so the number cannot be less than 0
This is the program. ive made this a global function but my instructor told me it was wrong. Making get_input a global function seems to work but i need to use the means of
def get_input():
get_input()
Ive been setting up the global function as get_input = input just because i have no clue how to do what i posted above without geting the error "global name is not defined".
Any help would be greatly appreciated
get_input = input
def main():
pennies = get_input("Enter pennies : ")
nickels = get_input("Enter nickels : ")
dimes = get_input("Enter dimes : ")
quarters = get_input("Enter quarters : ")
print("You entered : ")
print("\tPennies : " , pennies)
print("\tNickels : " , nickels)
print("\tDimes : " , dimes)
print("\tQuarters : " , quarters)
total_value = get_total(pennies, nickels, dimes, quarters)
dollars = get_dollars(pennies, nickels, dimes, quarters)
left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)
print("Total = $", total_value, sep="")
print("You have", dollars, "dollars and", left_over_cents, "cent(s)")
main()
Looks like you need to just put all your raw_input statements inside your get_input function.
def get_input(currency):
currency = -1.0
while currency < 0:
try:
currency = float(raw_input("Enter %s: ", % (currency)))
except ValueError:
print "Invalid input!"
currency = -1.0
continue
if currency < 0:
print "Can't have negative money!"
else:
return currency
def main():
pennies = get_input("pennies")
nickles= get_input("nickles")
dimes= get_input("dimes")
quarters= get_input("quarters")
Then so on with your program
Related
I am writing a script that asks the user for the numbers of coins of different denominations that they have, then displays the total sum in dollars and cents.
I am stuck at this point and am confused on what exactly is wrong. This is part of my intro class, so detail to what needs to be changed is greatly appreciated. The output I need is to tell the user how many dollars and left over cents there are. I am having a problem now with the output. The function tells me the right answer but when wording the answer the cent(s) are off.
For example when pennies are 5; nickels 3; dimes 3; quarters 3, the total is 1.25 but the function says "You have 1 dollars and 1 cent(s)"
def main():
pennies = get_input("Enter pennies : ")
nickels = get_input("Enter nickels : ")
dimes = get_input("Enter dimes : ")
quarters = get_input("Enter quarters : ")
print("You entered : ")
print("\tPennies : " , pennies)
print("\tNickels : " , nickels)
print("\tDimes : " , dimes)
print("\tQuarters : " , quarters)
total_value = get_total(pennies, nickels, dimes, quarters)
dollars = get_dollars(pennies, nickels, dimes, quarters)
left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)
print("Total = $",format(total_value, ".2f"), sep="")
print("You have", dollars, "dollars and", left_over_cents, "cent(s)")
def get_input(message):
get_input = int(input(message))
while get_input <= 0:
print("Error")
get_input = int(input(message))
return get_input
def get_total(pennies, nickels, dimes, quarters):
pennies = (.01 * pennies);
nickels = .05 * nickels;
dimes = .10 * dimes;
quarters = .25 * quarters;
total_value = pennies + nickels + dimes + quarters
return total_value
def get_dollars(pennies, nickels, dimes, quarters):
total_value = get_total(pennies, nickels, dimes, quarters)
dollars = format(total_value // 1, ".0f")
return dollars
def get_left_over_cents(pennies, nickels, dimes, quarters):
total_value = get_dollars(pennies, nickels, dimes, quarters)
left_over_cents = total_value
return left_over_cents
main()
The error actually lives in your get_input method.
def get_input(message):
get_input = int(input(message))
while get_input <= 0:
print("Error")
get_input = int(input(message))
return get_input
which returns None if the original input is > 0. You might want to unindent the return clause once.
def main():
pennies = get_input("Enter pennies : ")
nickels = get_input("Enter nickels: ")
dimes = get_input("Enter dimes: ")
quarters = get_input("Enter quarters: ")
print("You Entered : ")
print("\tPennies : " , pennies)
print("\tNickels :" , nickels)
print("\tDimes :" , dimes)
print("\tQuarters:" , quarters)
total_value = get_total(pennies, nickels, dimes, quarters)
dollars = get_dollars(pennies, nickels, dimes, quarters)
left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)
print("Total = $",total_value, sep="")
print("You Have", dollars, "dollars and", left_over_cents, "cent(s)")
I keep getting an error that states TypeError: unsupported operand type(s) for *: 'float' and 'NoneType. I've browsed answers that relate to this error on this website but found nothing that was able to help me out. After the user inputs four numbers, the program give me the error after trying to execute the next part of the code.
def get_input(message):
get_input = int(input(message))
while get_input <= 0:
print("Error")
return get_input
In this portion. If the user inputs a number 0 or lower, the while loop is supposed to return to the function but it just writes down 0 instead. I am sorry these are probably really easy solutions but I am in my intro class and this stuff is rather difficult for me.
def get_total(pennies, nickels, dimes, quarters):
pennies = (.01 * pennies);
nickels = .05 * nickels;
dimes = .10 * dimes;
quarters = .25 * quarters;
total_value = pennies + nickels + dimes + quarters
return total_value
def get_dollars(pennies, nickels, dimes, quarters):
total_value = get_total(pennies, nickels, dimes, quarters)
dollars = format(total_value // 1, ".0f")
return dollars
def get_left_over_cents(pennies, nickels, dimes, quarters):
total_value = get_dollars(pennies, nickels, dimes, quarters)
left_over_cents = total_value
return left_over_cents
main()
Your problem is in your get_input function:
def get_input(message):
get_input = int(input(message))
while get_input <= 0:
print("Error")
return get_input
You are only returning a value if you input something less than or equal to 0, apparently not desired input. Instead, you should move the return outside of the while statement, or consider improving it so that it only returns if the input is greater than 0:
def get_input(message):
get_input = -1
while get_input <= 0:
print("Enter a number greater than 0")
get_input = int(input(message))
return get_input
Such that:
>>> var = get_input('Enter: ')
Enter a number greater than 0
Enter: -4
Enter a number greater than 0
Enter: 0
Enter a number greater than 0
Enter: 5
>>> var
5
>>>
Earlier, you did not return anything so your value for pennies was stored as None. Python tried to multiply by pennies, None, hence your error:
>>> def get_input(message):
... get_input = int(input(message))
... while get_input <= 0:
... print("Error")
... return get_input
...
>>> pennies = get_input('Enter: ')
Enter: 9
>>> print(0.1*pennies)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'
First of all, I think your input function is wrong:
def get_input(message):
get_input = int(input(message))
while get_input <= 0 :
print("Error")
return get_input
Shouldn't this be:
def get_input(message):
get_input = int(input(message))
while get_input <= 0:
print("Error")
get_input = int(input(message))
return get_input
Most (strong) compilers won't even compile this, because they will convert your program into traces, and notice there is a possibility that your program returns without giving any value.
Since Python is weakly typed, this is not an issue: if no return statement is presented, it simply returns nothing, but ends the method.
Thus when you enter valid data, your method doesn't return anything, thus the result can't be processed.
This is one of the advantages of strongly typed languages over weakly typed ones like Python, although I admit there are other arguments that favor Python over say Java.
I cannot seem to figure out why when i run my program i receive the error ValueError: invalid literal for int() with base 10: 'Enter pennies : '.
The entire program was made by my instructor so we could add the functions to make it work. im currently trying to define get_input1 but im having no luck. any help would be great.
def main():
pennies = get_input1("Enter pennies : ")
nickels = get_input("Enter nickels : ")
dimes = get_input("Enter dimes : ")
quarters = get_input("Enter quarters : ")
print("You entered : ")
print("\tPennies : " , pennies)
print("\tNickels : " , nickels)
print("\tDimes : " , dimes)
print("\tQuarters : " , quarters)
total_value = get_total(pennies, nickels, dimes, quarters)
dollars = get_dollars(pennies, nickels, dimes, quarters)
left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)
print("Total = $", total_value, sep="")
print("You have", dollars, "dollars and", left_over_cents, "cent(s)")
def get_input1(pennies):
int(input("Enter Pennies: "))
if int(pennies) < 0:
print('Error: money cannot be negative')
pennies = int(input('Enter correct amount of pennies: '))
main()
change this:
int(input("Enter Pennies: "))
to this:
pennies = input("Enter Pennies: ")
Edited
I believe this is just a typo, you should be assigning pennies to the input result.
def main():
pennies = get_input1("Enter pennies : ")
nickels = get_input2("Enter nickels : ")
dimes = get_input3("Enter dimes : ")
quarters = get_input4("Enter quarters : ")
print("You entered : ")
print("\tPennies : " , pennies)
print("\tNickels : " , nickels)
print("\tDimes : " , dimes)
print("\tQuarters : " , quarters)
total_value = get_total(pennies, nickels, dimes, quarters)
dollars = get_dollars(pennies, nickels, dimes, quarters)
left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)
print("Total = $", total_value, sep="")
print("You have", dollars, "dollars and", left_over_cents, "cent(s)")
def get_input1(pennies):
pennies = int(input("Enter Pennies: "))
if int(pennies) < 0:
print('Error: money cannot be negative')
pennies = int(input('Enter correct amount of pennies: '))
return(pennies)
def get_input2(nickels):
nickels = int(input("Enter nickels: "))
if int(nickels) < 0:
print('Error: money cannot be negative')
pennies = int(input('Enter correct amount of nickels: '))
return(nickels)
def get_input3(dimes):
dimes = int(input("Enter dimes: "))
if int(dimes) < 0:
print('Error: money cannot be negative')
pennies = int(input('Enter correct amount of dimes: '))
return(dimes)
def get_input4(quarters):
quarters = int(input("Enter quarters: "))
if int(quarters) < 0:
print('Error: money cannot be negative')
pennies = int(input('Enter correct amount of quarters: '))
return(quarters)
main()
This will get you through defining your pennies,nickels,dimes and quarters. Now you will have to define get_total, get_dollar, and get_left_over_cents.
You needed to have pennies in front of the int value is why you were receiving the error. Then you can return that value of pennies to the main.
Hope this helps!
Your data flow is backwards. In main(), you're calling get_input1 to retrieve values, much as with input(), but if we look at that get_input1 does:
# Names its argument, which was a question, "pennies"
def get_input1(pennies):
# Asks a question, converts to int, and discards it
int(input("Enter Pennies: "))
if int(pennies) < 0: # Converts pennies to int; but it was a question!
print('Error: money cannot be negative')
# This is the only place to alter pennies, but it doesn't return it
pennies = int(input('Enter correct amount of pennies: '))
It looks like this used to be code that was refactored to handle the various coins with a function call each, but it doesn't have the two modifications it needs: a way to alter what it asks for, and a way to return what the answer was. Note that function arguments, like pennies in get_input1, are local variables; main never sees it changing.
I need to create a function that returns a total of dollar amount. The problem is that I have no clue about how to exactly do this. I've tried to put in some code, but I don't believe it will return what I am looking for:
def main():
pennies = get_input("Enter pennies : ")
nickels = get_input("Enter nickels : ")
dimes = get_input("Enter dimes : ")
quarters = get_input("Enter quarters : ")
print("You entered : ")
print("\tPennies : " , pennies)
print("\tNickels : " , nickels)
print("\tDimes : " , dimes)
print("\tQuarters : " , quarters)
total_value = get_total(pennies, nickels, dimes, quarters)
dollars = get_dollars(pennies, nickels, dimes, quarters)
left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)
print("Total = $", total_value, sep="")
print("You have", dollars, "dollars and", left_over_cents, "cent(s)")
def get_input(currency):
currency = 0
if currency < 0:
print('Cannot have negative money')
else:
return currency
def get_total(pennies, nickels, dimes, quarters):
(pennies * .01) + (nickels * .05) + (dimes * .10) + (quarters * .25)
def get_dollars(pennies, nickels, dimes, quarters):
main()
Your prints are incorrect, and instead of get_input you should use raw_input
def main():
pennies = raw_input("Enter pennies : ")
nickels = raw_input("Enter nickels : ")
dimes = raw_input("Enter dimes : ")
quarters = raw_input("Enter quarters : ")
print("You entered : ")
print("\tPennies : %s" % pennies)
print("\tNickels : %s" % nickels)
print("\tDimes : %s" % dimes)
print("\tQuarters : %s" % quarters)
total_value = get_total(pennies, nickels, dimes, quarters)
dollars = get_dollars(pennies, nickels, dimes, quarters)
left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)
print("Total = %s$" % total_value)
print("You have"+ str(dollars) + "dollars and"+ str(left_over_cents)+ "cent(s)")
def get_input(currency):
currency = 0
if currency < 0:
print('Cannot have negative money')
else:
return currency
def get_total(pennies, nickels, dimes, quarters):
return (float(pennies) * .01) + (float(nickels) * .05) + (float(dimes) * .10) + (float(quarters) * .25)
def get_dollars(pennies, nickels, dimes, quarters):
return 1
main()
EDIT:
"I need to create a function that returns a total of dollar amount."
return was missing in your function get_total
The above code should be working
I made this program that takes change and figures out how many full dollars, and leftover change. The way it is setup, it takes the amount of change, 495 for example, and then converts it to dollars, 4.95. Now I want to cut off the .95 and leave the 4, how do I do this without it rounding up to 5? Thanks!
def main():
pennies = int(input("Enter pennies : "))
nickels = int(input("Enter nickels : "))
dimes = int(input("Enter dimes : "))
quarters = int(input("Enter quarters : "))
computeValue(pennies, nickels, dimes, quarters)
def computeValue(p,n,d,q):
print("You entered : ")
print("\tPennies : " , p)
print("\tNickels : " , n)
print("\tDimes : " , d)
print("\tQuarters : " , q)
totalCents = p + n*5 + d*10 + q*25
totalDollars = totalCents / 100
totalDollarsTrunc = int(format(totalDollars, '.0f'))
totalPennies = totalCents - (totalDollarsTrunc * 100)
print("Amount of Change = ", totalDollarsTrunc, "dollars and ", totalPennies ,"cents.")
if totalCents < 100:
print("Amount not = to $1")
elif totalCents == 100:
print("You have exactly $1.")
elif totalCents >100:
print("Amount not = to $1")
else:
print("Error")
In Python, int() truncates when converting from float:
>>> int(4.95)
4
That said, you can rewrite
totalDollars = totalCents / 100
totalDollarsTrunc = int(format(totalDollars, '.0f'))
totalPennies = totalCents - (totalDollarsTrunc * 100)
using the divmod function:
totalDollars, totalPennies = divmod(totalCents, 100)
You probably want to use math.ceil or math.floor to give you the rounding in the direction that you want.
Function int() will do just that