i have assignment and we need to convert celsius to fahrenheit and vice versa. i have to loop the program but i dont know how.
my code is working but its still wrong.
def main():
print("a. Celsius to Fahrenheit")
print("b. Fahrenheit to Celsius")
choice = str(input("Enter Choice:"))
if choice == "a":
def c_to_f(C):
return((9/5)*C) + 32
temp =float(input("Enter Temp: "))
print(temp,"Celsius is",c_to_f(temp),"Fahrenheit")
if choice =="b":
def f_to_c(F):
return (5/9)*(F-32)
temp =float(input("Enter Temp: "))
print(temp,"Fahrenheit is",f_to_c(temp),"Celsius")
repeat = input("Do you want to convert again? (Yes/No): ")
for _ in range(10):
if repeat == "yes":
main()
else:
exit()
main()
Hi that's because variable has scope in which you can read their modifications, repeat modification in main() are not visible outside of their scope: here the main function.
Variables thatcan be "inspect" everywhere are called globals, and you should declare them at the beggining of a file outside of a function.
You have many solution to your problem, if you want to stay close from your actual code, here is one:
from sys import exit
def main():
print("a. Celsius to Fahrenheit")
print("b. Fahrenheit to Celsius")
choice = str(input("Enter Choice:"))
if choice == "a":
def c_to_f(C):
return((9/5)*C) + 32
temp =float(input("Enter Temp: "))
print(temp,"Celsius is",c_to_f(temp),"Fahrenheit")
if choice =="b":
def f_to_c(F):
return (5/9)*(F-32)
temp =float(input("Enter Temp: "))
print(temp,"Fahrenheit is",f_to_c(temp),"Celsius")
return input("Do you want to convert again? (Yes/No): ")
for _ in range(10):
repeat = main()
if repeat.lower() == 'no':
exit()
The problem with your code is that you ran main() after finishing in your last line of code
This will work great for you:
def main():
for _ in range(10):
print("a. Celsius to Fahrenheit")
print("b. Fahrenheit to Celsius")
choice = str(input("Enter Choice:"))
if choice == "a":
def c_to_f(C):
return ((9 / 5) * C) + 32
temp = float(input("Enter Temp: "))
print(temp, "Celsius is", c_to_f(temp), "Fahrenheit")
if choice == "b":
def f_to_c(F):
return (5 / 9) * (F - 32)
temp = float(input("Enter Temp: "))
print(temp, "Fahrenheit is", f_to_c(temp), "Celsius")
repeat = input("Do you want to convert again? (Yes/No): ")
if repeat == "yes":
main()
else:
exit()
if __name__ == '__main__':
main()
The problem with your code is your last user input.
This will work great for you:
def main():
print("a. Celsius to Fahrenheit")
print("b. Fahrenheit to Celsius")
choice = str(input("Enter Choice:"))
if choice == "a":
def c_to_f(C):
return((9/5)*C) + 32
temp =float(input("Enter Temp: "))
print(temp,"Celsius is",c_to_f(temp),"Fahrenheit")
if choice =="b":
def f_to_c(F):
return (5/9)*(F-32)
temp =float(input("Enter Temp: "))
print(temp,"Fahrenheit is",f_to_c(temp),"Celsius")
repeat = str(input("Do you want to convert again? (Yes/No): "))
for _ in range(10):
if repeat == "yes" or repeat == "Yes":
main()
else:
exit()
if __name__ == '__main__':
main()
Have Fun with CODE :)
This code should work
choice = 'y';
while choice != 'e':
choice = input('If you want to convert c to f press a\nIf you want to convert f to c press b\nIf you want to exit press e');
if choice == 'a':
c = int(input('Enter temp in C'));
print("F =", c*1.8+32);
elif choice == 'b':
f = int(input('Enter temp in F'));
print("C =", (F-32)/1.8);
Related
I want this code to repeat from the top. Im trying to build a calculator app and when I say "Do you want to continue (y) or go back to home (n): " I want it so that if the user says "n" then it will start the code from the beggining. Here is the code I want to do this in.
# Importing stuff
import math
import random
import time
import numpy as np
def startup():
global ask
print("Welcome to this math python program")
ask = input(
"Do you wnat to solve 1, add, 2, subtract, 3, multiply, divide, or 5 other complex problems? (answer with 1, 2, 3, 4, or 5: "
)
startup()
# Some stuff to make it look better
print(" ")
print(" ")
def add():
print("Addition")
num1 = int(input("First number: "))
num2 = int(input("Second Number: "))
num = num1 + num2
print("The answer is " + str(num))
# Subtract
def subtract():
print("Subtraction")
num3 = int(input("First number: "))
num4 = int(input("Second Number: "))
num5 = num3 - num4
print("The answer is " + str(num5))
print(" ")
def multiply():
print("Multiplication")
num6 = int(input("First number: "))
num7 = int(input("Second Number: "))
num8 = num6 * num7
print("The answer is " + str(num8))
print(" ")
def divide():
print("Division")
num9 = int(input("First number: "))
num10 = int(input("Second Number: "))
num11 = num9 / num10
print("The answer is " + str(num11))
print(" ")
def squareRoot():
print("Square Root")
asksqrt = int(
input("What number would you like to find the square root of: "))
answer1 = math.sqrt(asksqrt)
print("The square root of " + str(asksqrt) + " is: " + str(answer1))
print(" ")
def cubeRoot():
print("Cube root")
num12 = int(input("What number do you want to find the cube root of: "))
cube_root = np.cbrt(num12)
print("Cube root of ", str(num12), " is ", str(cube_root))
print(" ")
def exponent():
print("Exponents")
num13 = int(input("First you are going to tell me the number: "))
num14 = int(input("Next you are going to tell me the exponent: "))
num15 = num13**num14
print(str(num13) + " to the power of " + str(num14) + " is " + str(num15))
print(" ")
# While loops
while ask == "1":
print(" ")
add()
ask4 = input("Do you want to continue (y) or go back to home (n): ")
while ask4 == "y":
add()
while ask == "2":
print(" ")
subtract()
ask5 = input("Do you want to continue (y) or go back to home (n): ")
while ask5 == "y":
add()
while ask == "3":
print(" ")
multiply()
ask6 = input("Do you want to continue? (y/n) ")
while ask6 == "y":
add()
while ask == "4":
print(" ")
divide()
ask7 = input("Do you want to continue (y) or go back to home (n): ")
while ask7 == "y":
add()
while ask == "5":
ask2 = input(
"Do you want to do a 1, square root equation, 2, cube root equation, or 3 an exponent equation? (1, 2, 3): "
)
print(" ")
while ask2 == "1":
print(" ")
squareRoot()
ask8 = input("Do you want to continue (y) or go back to home (n): ")
while ask8 == "y":
add()
while ask2 == "2":
print(" ")
cubeRoot()
ask9 = input("Do you want to continue (y) or go back to home (n): ")
while ask9 == "y":
add()
while ask2 == "3":
print(" ")
exponent()
ask10 = input("Do you want to continue (y) or go back to home (n): ")
while ask10 == "y":
add()
I'm a begginer so I dont know a lot. If you could tell me what I can do better that would be much appreciated.
In this case, it's good to take a step back and think of what your main entrypoint, desired exit scenario and loop should look like. In pseudo-code, you can think of it like this:
1. Introduce program
2. Ask what type of calculation should be conducted
3. Do calculation
4. Ask if anything else should be done
5. Exit
At points 2 and 4, your choices are either exit program or do a thing. If you want to 'do a thing' over and over, you need a way of going from 3 back into 2, or 4 back into 3. However, steps 2 and 4 are basically the same, aren't they. So let's write a new main loop:
EDIT: Decided to remove match/case example as it's an anti-pattern
if __name__ == "__main__": # Defines the entrypoint for your code
current_task = 'enter'
while current_task != 'exit':
current_task = ask_user_for_task() # The user will input a string, equivalent to your startup()
if current_task == 'add': # If its 'add', run add()
add()
elif current_task == 'subtract':
subtract()
else:
current_task = 'exit' # If its 'exit' or anything else not captured in a case, set current_task to 'exit'. This breaks the while loop
ask_user_for_task() will be an input() call where the user gives the name of the function they want to execute:
def ask_user_for_task():
return input("Type in a calculation: 'add', 'subtract', ...")
This answer is not perfect - what if you don't want to exit if the user accidentally types in 'integrate'. What happens if the user types in "Add", not 'add'? But hopefully this gives you a starting point.
What you want to do can be greatly simplified. The number you choose correlates to the index of the operation in funcs. Other than that, the program is so simple that each part is commented.
Using this method eliminates the need for excessive conditions. The only real condition is if it is a 2 argument operation or a 1 argument operation. This is also very easily extended with more operations.
import operator, math, numpy as np
from os import system, name
#SETUP
#function to clear the console
clear = lambda: system(('cls','clear')[name=='posix'])
"""
this is the index in `funcs` where we switch from 2 arg operations to 1 arg operations
more operations can be added to `funcs`
simply changing this number accordingly will fix all conditions
"""
I = 6
"""
database of math operations - there is no "0" choice so we just put None in the 0 index.
this tuple should always be ordered as:
2 argument operations, followed by
1 argument operations, followed by
exit
"""
funcs = (None, operator.add, operator.sub, operator.mul, operator.truediv, operator.pow, math.sqrt, np.cbrt, exit)
#operation ranges
twoarg = range(1,I)
onearg = range(I,len(funcs))
#choice comparison
choices = [f'{i}' for i in range (1,len(funcs)+1)]
#this is the only other thing that needs to be adjusted if more operations are added
menu = """
choose an operation:
1:add
2:subtract
3:multiply
4:divide
5:exponent
6:square root
7:cube root
8:quit
>> """
#IMPLEMENT
while 1:
clear()
print("Supa' Maffs")
#keep asking the same question until a valid choice is picked
while not (f := input(menu)) in choices: pass
f = int(f)
#vertical space
print("")
if f in twoarg: x = (int(input("1st number: ")), int(input("2nd number: ")))
elif f in onearg: x = (int(input("number: ")), )
else : funcs[f]() #exit
#unpack args into operation and print results
print(f'answer: {funcs[f](*x)}\n')
input('Press enter to continue')
if you want to, you can see the code below:
while(input("want to continue (y/n)?") == 'y'):
operation = input("operation: ")
num_1 = float(input("first num: "))
num_2 = float(input("second num: "))
match operation:
case "+":
print(num_1 + num_2)
case "-":
print(num_1 - num_2)
case "*":
print(num_1 * num_2)
case "/":
print(num_1 / num_2)
case other:
pass
make sure you have the version 3.10 or later of python
When I want to quit my calculator it works, but when I want to keep using it there comes a problem:
File "main.py", line 54, in <module>
main()
NameError: name 'main' is not defined
I want to keep looping it from the start if I type y or Y so that I could keep using it.
And if I type 9 after I use it, I can't come to the closing screen. So I want it to show the close screen and stop looping.
here's my code:
print("calculator")
print("1.Pluss")
print("2.Minus")
print("3.Ganging")
print("4.Deling")
print("9.Avslutt")
chr=int(input("1-4 eller Avslutt: "))
while True:
if chr==1:
a=int(input("Number 1:"))
b=int(input("Number 2:"))
c=b+a
print("sum = ", c)
elif chr==2:
a=int(input("Number 1:"))
b=int(input("Number 2:"))
c=a-b
print("sum = ", c)
elif chr==3:
a=int(input("Number 1:"))
b=int(input("Number 2:"))
c=a*b
print("sum = ", c)
elif chr==4:
a=int(input("Number 1:"))
b=int(input("Number 2:"))
c=a/b
print("sum = ", c)
elif chr==5:
print("1-4 idiot")
elif chr==6:
print("1-4 idiot")
elif chr==7:
print("1-4 idiot")
elif chr==8:
print("1-4 idiot")
else:
print("Thank you for using my calculator!")
again = input("Do you want to keep using my calculator? y/n")
if again == "y" or again == "Y":
main()
else:
print("Thank you!")
break
It works if I want to quit, but it can't keep looping from the start after it comes to.
print("Thank you for using my calculator!")
again = input("Do you want to keep using my calculator? y/n")
if again == "y" or again == "Y":
main()
else:
print("Thank you!")
break
If you are offering an option to terminate (9. Avslutt) you don't need to ask user for confirmation of iteration. Just repeat the input prompts.
There's no need to convert the user input to int as the values are not used in any arithmetic context - i.e., they're just strings.
You may find this pattern useful:
from operator import add, sub, mul, truediv
OMAP = {'1': add, '2': sub, '3': mul, '4': truediv}
while True:
print("calculator")
print("1. Pluss")
print("2. Minus")
print("3. Ganging")
print("4. Deling")
print("9. Avslutt")
if (option := input('1-4 eller avslutt: ')) == '9':
break
if (op := OMAP.get(option)) is None:
print('Ugyldig alternativ')
else:
x = float(input('Number 1: '))
y = float(input('Number 2: '))
print(f'Resultat = {op(x, y):.2f}')
When I attempt to call the menu function in each choice after the conversion is run, it will take me back to the menu but will only convert in the original selection. If I choose option 1 first after the convertCelsius function is ran and it takes me back to the menu function, any choice will still only convert to celsius. How do I get call the menu function so I can run each choice again.
def menu():
print("1. Celsius to Fahrenheit")
print('2. Fahrenheit to Celsius')
print('3. Exit')
pick = int(input('Enter a choice: '))
return pick
def convertCelsius():
temperature = float(input('Write temp to convert from Celsius to Fahrenheit: '))
fahrenheitTemperature = ((temperature * 1.8) + 32)
print('Fahrenheit temperature is: ',fahrenheitTemperature)
def convertFahrenheit():
temperature = float(input('Write tempt to convert from Fahrenheit to Celsius: '))
celsiusTemperature = ((temperature - 32)* 5/9)
print("Celsius temperature is: ", celsiusTemperature)
def main():
choice = menu()
while choice != 3:
if choice == 1:
convertCelsius()
menu()
elif choice == 2:
convertFahrenheit()
menu()
elif choice==3:
choice = menu()
main()
I think you meant to do choice = menu() every time:
def main():
choice = menu()
while choice != 3:
if choice == 1:
convertCelsius()
elif choice == 2:
convertFahrenheit()
choice = menu()
I'm writing a Python math game in which the program asks an addition question and the user has to get the right answer to continue. My question is, how can I make the program generate a new math problem when the user gets the last one correct?
import random
firstNumber = random.randint(1, 50)
secondNumber = random.randint(1, 50)
result = firstNumber + secondNumber
result = int(result)
print("Hello ! What\'s your name ? ")
name = input()
print("Hello !"+" "+ name)
print("Ok !"+" "+ name +" "+ "let\'s start !")
print("What is"+ " " + str(firstNumber) +"+"+ str(secondNumber))
userAnswer = int(input("Your answer : "))
while (True) :
if (userAnswer == result):
print("Correct")
print("Good Job!")
break
else:
print("Wrong\n")
userAnswer = int(input("Your answer : "))
input("\n\n Press to exit")
Implement the game with a pair of nested loops. In the outer loop, generate a new arithmetic problem. In the inner loop, keep asking the user for guesses until he either gives the right answer or decides to quit by entering a blank line.
import random
playing = True
while playing:
# Generate a new arithmetic problem.
a = random.randint(1, 50)
b = random.randint(1, 50)
solution = a + b
print('\nWhat is %d + %d? (to quit, enter nothing)' % (a, b))
# Keep reading input until the reply is empty (to quit) or the right answer.
while True:
reply = input()
if reply.strip() == '':
playing = False
break
if int(reply) == solution:
print('Correct. Good job!')
break
else:
print('Wrong. Try again.')
print('Thank you for playing. Goodbye!')
This should get you started. The getnumbers() returns two random numbers, just like in your script. Now just add in you game code. Let me know if you have questions!
import random
def getnumbers():
a = random.randint(1, 50)
b = random.randint(1, 50)
return a, b
print("Math Game!")
while True:
a, b = getnumbers()
# game code goes here
print("%d %d" % (a, b))
input()
This might do what you want:
import random
def make_game():
firstNumber = random.randint(1, 50)
secondNumber = random.randint(1, 50)
result = firstNumber + secondNumber
result = int(result)
print("What is"+ " " + str(firstNumber) +"+"+ str(secondNumber))
userAnswer = int(input("Your answer : "))
while (True) :
if (userAnswer == result):
print("Correct")
print("Good Job!")
break
else:
print("Wrong\n")
userAnswer = int(input("Your answer : "))
print("Hello ! What\'s your name ? ")
name = input()
print("Hello !"+" "+ name)
print("Ok !"+" "+ name +" "+ "let\'s start !")
while True:
make_game()
end = input('\n\n Press to "end" to exit or "enter" to continue: ')
if end.strip() == 'end':
break
I'm sorry for asking, but I can't find why I'm getting this errors, especially after the program is running.
The erros to be exact are:
>>>
Welcome! This program will convert measures for you.
Select operation.
1.Miles to Kilometers
2.Fahrenheit to Celsius
3.Gallons to liters
4.Pounds to kilograms
5.Inches to centimeters
Enter your choice by number: 1
Traceback (most recent call last):
File "C:\Users\Levhitor\Downloads\Mario_Gomez_Lab2.py", line 112, in <module>
intro()
File "C:\Users\Levhitor\Downloads\Mario_Gomez_Lab2.py", line 12, in intro
main()
File "C:\Users\Levhitor\Downloads\Mario_Gomez_Lab2.py", line 25, in main
convertMK()
File "C:\Users\Levhitor\Downloads\Mario_Gomez_Lab2.py", line 44, in convertMK
input_M = float(raw_input(("Miles: ")))
TypeError: 'int' object is not callable
I don't understand what's happening here. Can anyone help me?
raw_input = 0
M = 1.6
# Miles to Kilometers
# Celsius Celsius = (var1 - 32) * 5/9
# Gallons to liters Gallons = 3.6
# Pounds to kilograms Pounds = 0.45
# Inches to centimete Inches = 2.54
def intro():
print("Welcome! This program will convert measures for you.")
main()
def main():
print("Select operation.")
print("1.Miles to Kilometers")
print("2.Fahrenheit to Celsius")
print("3.Gallons to liters")
print("4.Pounds to kilograms")
print("5.Inches to centimeters")
choice = input("Enter your choice by number: ")
if choice == '1':
convertMK()
elif choice == '2':
converCF()
elif choice == '3':
convertGL()
elif choice == '4':
convertPK()
elif choice == '5':
convertPK()
else:
print("Error")
def convertMK():
input_M = float(raw_input(("Miles: ")))
M_conv = (M) * input_M
print("Kilometers: %f\n" % M_conv)
restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
if restart == 'y':
main()
elif restart == 'n':
end()
else:
print("I didn't quite understand that answer. Terminating.")
main()
def converCF():
input_F = float(raw_input(("Fahrenheit: ")))
F_conv = (input_F - 32) * 5/9
print("Celcius: %f\n") % F_conv
restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
if restart == 'y':
main()
elif restart == 'n':
end()
else:
print("I didn't quite understand that answer. Terminating.")
main()
def convertGL():
input_G = float(raw_input(("Gallons: ")))
G_conv = input_G * 3.6
print("Centimeters: %f\n" % G_conv)
restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
if restart == 'y':
main()
elif restart == 'n':
end()
else:
print ("I didn't quite understand that answer. Terminating.")
main()
def convertPK():
input_P = float(raw_input(("Pounds: ")))
P_conv = input_P * 0.45
print("Centimeters: %f\n" % P_conv)
restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
if restart == 'y':
main()
elif restart == 'n':
end()
else:
print ("I didn't quite understand that answer. Terminating.")
main()
def convertIC():
input_cm = float(raw_input(("Inches: ")))
inches_conv = input_cm * 2.54
print("Centimeters: %f\n" % inches_conv)
restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
if restart == 'y':
main()
elif restart == 'n':
end()
else:
print ("I didn't quite understand that answer. Terminating.")
main()
def end():
print("This program will close.")
exit()
intro()
I do not know what is wrong... can anyone help me?
Thanks you!
At the beginning of your file you set raw_input to 0. Do not do this, at it modifies the built-in raw_input() function. Therefore, whenever you call raw_input(), it is essentially calling 0(), which raises the error. To remove the error, remove the first line of your code:
M = 1.6
# Miles to Kilometers
# Celsius Celsius = (var1 - 32) * 5/9
# Gallons to liters Gallons = 3.6
# Pounds to kilograms Pounds = 0.45
# Inches to centimete Inches = 2.54
def intro():
print("Welcome! This program will convert measures for you.")
main()
def main():
print("Select operation.")
print("1.Miles to Kilometers")
print("2.Fahrenheit to Celsius")
print("3.Gallons to liters")
print("4.Pounds to kilograms")
print("5.Inches to centimeters")
choice = input("Enter your choice by number: ")
if choice == '1':
convertMK()
elif choice == '2':
converCF()
elif choice == '3':
convertGL()
elif choice == '4':
convertPK()
elif choice == '5':
convertPK()
else:
print("Error")
def convertMK():
input_M = float(raw_input(("Miles: ")))
M_conv = (M) * input_M
print("Kilometers: %f\n" % M_conv)
restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
if restart == 'y':
main()
elif restart == 'n':
end()
else:
print("I didn't quite understand that answer. Terminating.")
main()
def converCF():
input_F = float(raw_input(("Fahrenheit: ")))
F_conv = (input_F - 32) * 5/9
print("Celcius: %f\n") % F_conv
restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
if restart == 'y':
main()
elif restart == 'n':
end()
else:
print("I didn't quite understand that answer. Terminating.")
main()
def convertGL():
input_G = float(raw_input(("Gallons: ")))
G_conv = input_G * 3.6
print("Centimeters: %f\n" % G_conv)
restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
if restart == 'y':
main()
elif restart == 'n':
end()
else:
print ("I didn't quite understand that answer. Terminating.")
main()
def convertPK():
input_P = float(raw_input(("Pounds: ")))
P_conv = input_P * 0.45
print("Centimeters: %f\n" % P_conv)
restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
if restart == 'y':
main()
elif restart == 'n':
end()
else:
print ("I didn't quite understand that answer. Terminating.")
main()
def convertIC():
input_cm = float(raw_input(("Inches: ")))
inches_conv = input_cm * 2.54
print("Centimeters: %f\n" % inches_conv)
restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
if restart == 'y':
main()
elif restart == 'n':
end()
else:
print ("I didn't quite understand that answer. Terminating.")
main()
def end():
print("This program will close.")
exit()
intro()
I don't know which version of Python you are using but I tried this in Python 3 and made a few changes and it looks like it works. The raw_input function seems to be the issue here. I changed all the raw_input functions to "input()" and I also made minor changes to the printing to be compatible with Python 3. AJ Uppal is correct when he says that you shouldn't name a variable and a function with the same name. See here for reference:
TypeError: 'int' object is not callable
My code for Python 3 is as follows:
# https://stackoverflow.com/questions/27097039/why-am-i-getting-a-traceback-most-recent-call-last-error
raw_input = 0
M = 1.6
# Miles to Kilometers
# Celsius Celsius = (var1 - 32) * 5/9
# Gallons to liters Gallons = 3.6
# Pounds to kilograms Pounds = 0.45
# Inches to centimete Inches = 2.54
def intro():
print("Welcome! This program will convert measures for you.")
main()
def main():
print("Select operation.")
print("1.Miles to Kilometers")
print("2.Fahrenheit to Celsius")
print("3.Gallons to liters")
print("4.Pounds to kilograms")
print("5.Inches to centimeters")
choice = input("Enter your choice by number: ")
if choice == '1':
convertMK()
elif choice == '2':
converCF()
elif choice == '3':
convertGL()
elif choice == '4':
convertPK()
elif choice == '5':
convertPK()
else:
print("Error")
def convertMK():
input_M = float(input(("Miles: ")))
M_conv = (M) * input_M
print("Kilometers: {M_conv}\n")
restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
if restart == 'y':
main()
elif restart == 'n':
end()
else:
print("I didn't quite understand that answer. Terminating.")
main()
def converCF():
input_F = float(input(("Fahrenheit: ")))
F_conv = (input_F - 32) * 5/9
print("Celcius: {F_conv}\n")
restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
if restart == 'y':
main()
elif restart == 'n':
end()
else:
print("I didn't quite understand that answer. Terminating.")
main()
def convertGL():
input_G = float(input(("Gallons: ")))
G_conv = input_G * 3.6
print("Centimeters: {G_conv}\n")
restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
if restart == 'y':
main()
elif restart == 'n':
end()
else:
print ("I didn't quite understand that answer. Terminating.")
main()
def convertPK():
input_P = float(input(("Pounds: ")))
P_conv = input_P * 0.45
print("Centimeters: {P_conv}\n")
restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
if restart == 'y':
main()
elif restart == 'n':
end()
else:
print ("I didn't quite understand that answer. Terminating.")
main()
def convertIC():
input_cm = float(input(("Inches: ")))
inches_conv = input_cm * 2.54
print("Centimeters: {inches_conv}\n")
restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
if restart == 'y':
main()
elif restart == 'n':
end()
else:
print ("I didn't quite understand that answer. Terminating.")
main()
def end():
print("This program will close.")
exit()
intro()
I noticed a small bug in your code as well. This function should ideally convert pounds to kilograms but it looks like when it prints, it is printing "Centimeters" instead of kilograms.
def convertPK():
input_P = float(input(("Pounds: ")))
P_conv = input_P * 0.45
# Printing error in the line below
print("Centimeters: {P_conv}\n")
restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))
if restart == 'y':
main()
elif restart == 'n':
end()
else:
print ("I didn't quite understand that answer. Terminating.")
main()
I hope this helps.