import math
operation = (" ")
result = 0.0
number1 = 0.0
number2 = 0.0
def choice():
global operation, number1, number2
choices = input('Enter in desired option then numbers:')
operation, number1, number2 = choices.split(" ")
return (operation, number1, number2)
def add():
global result
result = float(number1) + float(number2)
return result
def sub():
global result
result = float(number1) - float(number2)
return result
def mul():
global result
result = float(number1) * float(number2)
return result
def div():
global result
result = float(number1) / float(number2)
return result
def absolute():
global result
result = abs(float(number1))
return result
def report():
print(result)
def main():
print ('Welcome to homeBrewCalculator')
print ('operations available are below:')
print ('(add)ition')
print ('(sub)traction')
print ('(mul)tiplication')
print ('(div)ivsion')
print ('(absolute) value')
print ('quit')
done = False
while not done:
choice()
if operation == 'add':
add()
report()
elif operation == 'sub':
sub()
report()
elif operation == 'mul':
mul()
report()
elif operation == 'div':
div()
report()
elif operation == 'abs':
absolute()
report()
elif operation == 'quit':
break
main()
I have to build a simple calculator that needs to read the command as a single line then it splits it up.
but i also have to have a quit function but when I try and quit I get this error below.
builtins.ValueError: not enough values to unpack (expected 3, got 1)
also when the program quits it is supposed to print function usage statistics and end the program of course. But I am stuck on how to integrate how many times a function has been used before the user quits.
it should look something like this when it quits
Function usage count
add function: 1
sub function: 0
mul function: 2
div function: 0
abs function: 3
depending how many times the user used each function before quitting.
counter = {'add': 0, 'sub': 0, 'mul': 0, 'div': 0, 'abs': 0 }
counter[operation] += 1
def report():
for func, times in counter.iteritems():
print "%s function: %d" % (func, times)
where would I insert the counter[operation] += 1
since I cannot seem to get it to come up when it should print
import math
operation = (" ")
result = 0.0
number1 = 0.0
number2 = 0.0
def choice():
global operation, number1, number2
userinput = input('Enter in desired option then numbers:')
choices = userinput.split(" ")
operation = choices[0]
if operation == 'add':
number1 = float(choices[1])
number2 = float(choices[2])
return (operation, number1, number2)
add()
report()
elif operation == 'sub':
number1 = float(choices[1])
number2 = float(choices[2])
return (operation, number1, number2)
sub()
report()
elif operation == 'mul':
number1 = float(choices[1])
number2 = float(choices[2])
return (operation, number1, number2)
mul()
report()
elif operation == 'div':
number1 = float(choices[1])
number2 = float(choices[2])
return (operation, number1, number2)
div()
report()
elif operation == 'abso':
number1 = float(choices[1])
return (operation, number1)
abso()
report()
elif operation == 'quit':
return (operation)
printstats()
def add():
global result
result = (number1) + (number2)
return result, add.counter
def sub():
global result
result = (number1) - (number2)
return result
def mul():
global result
result = (number1) * (number2)
return result
def div():
global result
result = (number1) / (number2)
return result
def abso():
global result
result = abs(number1)
return result
def report():
if operation == 'quit':
main()
else:
print(result)
def main():
print ('Welcome to homeBrewCalculator')
print ('operations available are below:')
print ('(add)ition')
print ('(sub)traction')
print ('(mul)tiplication')
print ('(div)ivsion')
print ('(absolute) value')
print ('quit')
done = False
while not done:
choice()
if operation == 'add':
add()
report()
elif operation == 'sub':
sub()
report()
elif operation == 'mul':
mul()
report()
elif operation == 'div':
div()
report()
elif operation == 'abso':
abso()
report()
elif operation == 'quit':
done = True
main()
Okay, so I fixed the issue with quitting and absolute value. New code above. I am very new to programming so I am not familiar with all possibilities I could use.
I still cannot figure out how to integrate a global variable to keep track of the number of times each function were called(executed)and then have the quit call a function that prints out the stats that the program tallies.
For example:
Function usage count
add function: 1
sub function: 0
mul function: 2
div function: 0
abs function: 3
This is your program with stats:
import math
operation = (" ")
result = 0.0
number1 = 0.0
number2 = 0.0
counter = {'add': 0, 'sub': 0, 'mul': 0, 'div': 0, 'abs': 0, 'quit': 0 }
def printstats():
del counter['quit']
for func, times in counter.iteritems():
print "%s function: %d" % (func, times)
def choice():
global operation, number1, number2
userinput = input('Enter in desired option then numbers:')
choices = userinput.split(" ")
operation = choices[0]
counter[operation] += 1
if operation == 'add':
number1 = float(choices[1])
number2 = float(choices[2])
return (operation, number1, number2)
add()
report()
elif operation == 'sub':
number1 = float(choices[1])
number2 = float(choices[2])
return (operation, number1, number2)
sub()
report()
elif operation == 'mul':
number1 = float(choices[1])
number2 = float(choices[2])
return (operation, number1, number2)
mul()
report()
elif operation == 'div':
number1 = float(choices[1])
number2 = float(choices[2])
return (operation, number1, number2)
div()
report()
elif operation == 'abso':
number1 = float(choices[1])
return (operation, number1)
abso()
report()
elif operation == 'quit':
printstats()
return (operation)
def add():
global result
result = (number1) + (number2)
return result, add.counter
def sub():
global result
result = (number1) - (number2)
return result
def mul():
global result
result = (number1) * (number2)
return result
def div():
global result
result = (number1) / (number2)
return result
def abso():
global result
result = abs(number1)
return result
def report():
if operation == 'quit':
main()
else:
print(result)
def main():
print ('Welcome to homeBrewCalculator')
print ('operations available are below:')
print ('(add)ition')
print ('(sub)traction')
print ('(mul)tiplication')
print ('(div)ivsion')
print ('(absolute) value')
print ('quit')
done = False
while not done:
choice()
if operation == 'add':
add()
report()
elif operation == 'sub':
sub()
report()
elif operation == 'mul':
mul()
report()
elif operation == 'div':
div()
report()
elif operation == 'abso':
abso()
report()
elif operation == 'quit':
done = True
main()
This is an online diff for you to see changes a bit easier:
https://www.diffchecker.com/KBJirh7b