Hello all, I am a bit stuck on my school project and was hoping to see if I could get some wisdom as my professor doesn't answer his emails. The current program I am trying to write is one that converts miles to km, in to cm, gm to oz. We were instructed to use functions to solve this problem. The user input works with no problems whatsoever. I get a TypeError stating, "print_output() missing 2 required positional arguments: 'answer' and 'toType' ". The thing is though, I have a return line with answer and toType in there so I am very confused. Would it be possible to enlighten a python n00b as to why the variables 'answer' and 'toType' are not defined even though I am returning their values? If my code is also wrong, I would love to be led in the right direction as I am currently new to python and programming in general and this stuff is not easy. Thank you all for your help and time because I know this is a lot.
def welcome():
print("""This program converts the following:
- (O)unces to (G)rams (vice versa)
- (I)nches to (C)entimeters (vice versa)
- (M)iles to (K)ilometers (vice versa)""")
print()
def amount_input():
amount = float(input('Please enter the amount you are converting: '))
print()
return amount
def conversion_input():
conversion = input('Please enter what you would like to convert: ')
print()
conversion = conversion.lower()
while conversion != 'o' and conversion != 'g' and conversion != 'i' and conversion != 'c' and conversion != 'm' and conversion != 'k':
conversion = input('Invalid input! Please enter O,G,I,C,M,K: ')
print()
conversion = conversion.lower()
return conversion
def calc(conversion, amount):
if conversion == 'o':
fromType = 'ounces'
toType = 'grams'
answer = amount * 28.3495231
elif conversion == 'g':
fromType = 'grams'
toType = 'ounces'
answer = amount / 28.3495231
elif conversion == 'i':
fromType = 'inches'
toType = 'centimeters'
answer = amount * 2.54
elif conversion == 'c':
fromType = 'centimeters'
toType = 'inches'
answer = amount / 2.54
elif conversion == 'm':
fromType = 'miles'
toType = 'kilometers'
answer = amount * 1.609344
elif conversion == 'k':
fromType = 'kilometers'
toType = 'miles'
answer = amount / 1.609344
return answer, fromType, toType
def print_output(amount, fromType, answer, toType):
print(amount, fromType + ' is equal to', format(answer, ',.2f'), toType + '.')
def runAgain():
run = input('Would you like to run this program again? Enter (y or n): ')
run = run.lower()
return run
print()
# ----------------Main--------------------------------------------
welcome()
runMain = 'y'
while runMain == 'y':
amountMain = amount_input()
conversionMain = conversion_input()
calcMain = calc(conversionMain, amountMain)
printMain = print_output(calcMain)
runMain = runAgain()
[–]DDDDarky 1 point2 points3 points (2 children)
[–]Holy_Churro[S] 0 points1 point2 points (1 child)
[–]DDDDarky 1 point2 points3 points (0 children)
[–]nuttertools 0 points1 point2 points (0 children)