Why is my input function not supported? - python

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.

Related

Why am i getting this error "name is not defined" when it is?

Why am i getting this error? The picture has the details. I need to get the _spent values to print the proper amount of times. So, say it runs through the loop 3 times, I need it to print 3. I think that is where the 1s are coming from. I don't like it!
pennies = 10
nickels = 10
dimes = 10
quarters = 10
print("\nWelcome to change-making program.")
in_str = input("\nEnter the purchase price (xx.xx) or `q' to quit: ")
while in_str.lower() != 'q':
dollar_str, cents_str = in_str.split(".")
if in_str.lower() == 'q':
quit()
in_int = int(float(in_str) * 100)
if in_int < 0:
print("Error: purchase price must be non-negative.")
in_str = input("\nEnter the purchase price (xx.xx) or `q' to quit: ")
if in_int > 0:
payment = input("\nInput dollars paid: ")
payment_int = int(float(payment) * 100)
change = payment_int - in_int
#determines if there payment input
if payment_int < in_int:
print("Error: Insufficient payment.")
payment = input("\nInput dollars paid: ")
payment_int = int(float(payment) * 100)
if change == 0:
print("No change.")
#determines how many quarters, dimes, nickels, and pennies are left
while change >= 25 and quarters > 0:
change = change - 25
quarters_spent = 0
quarters_spent += 1
quarters = quarters - quarters_spent
print(quarters_spent)
while change >= 10 and dimes > 0:
change = change - 10
dimes_spent = 0
dimes_spent += 1
dimes = dimes - dimes_spent
print(dimes_spent)
while change >= 5 and nickels > 0:
change = change - 5
nickels_spent = 0
nickels_spent += 1
nickels = nickels - nickels_spent
print(nickels_spent)
while change >= 1 and pennies > 0:
change = change - 1
pennies_spent = 0
pennies_spent += 1
pennies = pennies - pennies_spent
if quarters == 0 and dimes == 0 and nickels == 0 and pennies == 0:
print("Error: ran out of coins.")
quit()
print("\nCollect Payment Below:")
print(10 - quarters, "Quarters")
print("\nStock: ", quarters, "Quarters, ", dimes, " Dimes, ", nickels, " Nickels, ", pennies, " Pennies ")
in_str = input("\nEnter the purchase price (xx.xx) or `q' to quit: ")
pennies = pennies
nickels = nickels
dimes = dimes
quarters = quarters
This error implies that you didn't define the value nickels_spent before trying to use it.
I guess the error is in this line: print (nickels_spent).
What is probably happening is that the while statement condition that is used to assign a value to that variable is not true when you try to run it, so it wasn't defined but you still try to use it.
Try debugging before that while loop to see what exactly happens there.
You only defined and initialized this variable nickels_spent inside the while loop
However, if the condition is not met, the program will skip the loop and jump to execute this print(nickels_spent) statement where this variable is not yet defined.
You can either
Move the print statement into the while loop
Or
Define and initialize this variable outside the while loop.
Depends on the purpose of your program

denomination of coins in def main() python

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.

ValueError: invalid literal for int() with base 10 cannot figure out why

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.

Need help defining a function in python

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

Truncating ints without rounding?

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

Categories