Reputation: 13
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()
Upvotes: 0
Views: 662
Reputation: 19264
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'
Upvotes: 1
Reputation: 476547
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.
Upvotes: 1