d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Python Modules W/ Loop > Can Anyone Tell Me Why The Loop Wont End
Add Reply New Topic New Poll
Member
Posts: 16,455
Joined: May 2 2007
Gold: 20.17
Jul 10 2015 11:22am
It is currently an infinite loop, i need it to end when the user enters "E", any help?



Code
choice = ""
def welcome():
print ("Welcome to the Metric Conversion Program\nThis program will take a unit of measurement and convert\nit to degrees Celcius or Meters.\n**********************************************************")

def choices(choice):
choice = ""
choice = str(input("Here are your choices:\n\tC for Celcius\n\tM for Meters\n\tE for Exit"))
Fahrenheit = 0.0
Celcius = 0.0
Feet = 0.0
Meters = 0.0
if choice == "C":
Celcius = fahrenheit_to_celcius(Fahrenheit,Celcius)
print (Celcius)
elif choice == "M":
Meters = feet_to_meters(Feet,Meters)
print (Meters)
elif choice == "E":
print("Thank you for using our program")
else:
print("Sorry you did not enter a C, M or E\nPlease try again: ")
choices(choice)
return choice

def fahrenheit_to_celcius(Fahrenheit,Celcius):
Fahrenheit = 0.0
Celcius = 0.0
Fahrenheit = float(input("Enter Degrees Fahrenheit: "))
Celcius = (5/9)*(Fahrenheit-32)
return Celcius

def feet_to_meters(Feet,Meters):
Feet = 0.0
Meters = 0.0
Feet = float(input("Enter Feet: "))
Meters = (0.305*Feet)
return Meters

def main():
choice = ""
Feet = 0.0
Meters = 0.0
Fahrenheit = 0.0
Celcius = 0.0
choice = choices(choice)
welcome()
while choice != "E":
main()


This post was edited by Drakwen on Jul 10 2015 11:39am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jul 10 2015 06:07pm
http://forums.d2jsp.org/topic.php?t=72930181&f=27

i'm making a note not to waste time helping you. once you resolve your problem, you need to come back to your topics and say it's done.
Member
Posts: 16,455
Joined: May 2 2007
Gold: 20.17
Jul 10 2015 07:51pm
Quote (carteblanche @ Jul 10 2015 07:07pm)
http://forums.d2jsp.org/topic.php?t=72930181&f=27

i'm making a note not to waste time helping you. once you resolve your problem, you need to come back to your topics and say it's done.


sorry i didnt post back in here, you don't have to help if you dont want to anyhow... :( didnt mean to waste your time.

idk there may even be a better solution than the one i found on my own, i wouldn't mind knowing if that was the case.
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Jul 10 2015 09:25pm
Code
def welcome():
print ("Welcome to the Metric Conversion Program\nThis program will take a unit of measurement and convert\nit to degrees Celcius or Meters.\n**********************************************************")

def choices():
choice = str(input("Here are your choices:\n\tC for Celcius\n\tM for Meters\n\tE for Exit\n\n")).upper()
Fahrenheit = 0.0
Celcius = 0.0
Feet = 0.0
Meters = 0.0
if choice == "C":
Celcius = fahrenheit_to_celcius(Fahrenheit,Celcius)
print (Celcius)
elif choice == "M":
Meters = feet_to_meters(Feet,Meters)
print (Meters)
elif choice == "E":
print("Thank you for using our program")
else:
print("Sorry you did not enter a C, M or E\nPlease try again: ")
choices()
return choice

def fahrenheit_to_celcius(Fahrenheit,Celcius):
Fahrenheit = 0.0
Celcius = 0.0
Fahrenheit = float(input("Enter Degrees Fahrenheit: "))
Celcius = (5/9)*(Fahrenheit-32)
return Celcius

def feet_to_meters(Feet,Meters):
Feet = 0.0
Meters = 0.0
Feet = float(input("Enter Feet: "))
Meters = (0.305*Feet)
return Meters

def main():
choice = ""
Feet = 0.0
Meters = 0.0
Fahrenheit = 0.0
Celcius = 0.0
choice = choices()


if __name__ == "__main__":
welcome()
choices()


You will probably not want to confuse your scopes again by having an unnecessary choice parameter being passed that didn't actually do anything, then choices = """ in the choices() scope and globally. :wallbash:

I'm not actually sure where you want the loop or even if you just want it to run forever

Code

import string

def choices():
Fahrenheit = 0.0
Celcius = 0.0
Feet = 0.0
Meters = 0.0

choice = str(input("\nHere are your choices:\n\tC for Celcius\n\tM for Meters\n\tE for Exit\n\n")).upper()

while choice in list(string.ascii_uppercase):

if choice == "C":
Celcius = fahrenheit_to_celcius(Fahrenheit,Celcius)
print (Celcius)
choices()
elif choice == "M":
Meters = feet_to_meters(Feet,Meters)
print (Meters)
choices()
elif choice == "E":
print("Thank you for using our program")
break
else:
print("Sorry you did not enter a C, M or E\nPlease try again: ")
choices()
return choice


This post was edited by j0ltk0la on Jul 10 2015 09:51pm
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Jul 11 2015 12:24am
Code
import sys

def welcome():
print ("Welcome to the Metric Conversion Program\nThis program will take a unit of measurement and convert\nit to degrees Celcius or Meters.\n**********************************************************")



class Conversion:

def __init__(self):
self.choice = ""
self.options = {}

def ask(self):
self.choice = str(input('(C)elsius, (M)eters, or (E)xit? ')).upper()

def action(self):
self.options = {"C": self.f2c,
"M": self.f2m,
"E": self.quit,}
return self.options[self.choice]()

def results(self):
print('\nResults: %.2f \n' % self.res)

def f2c(self):
""" Fahrenheit to Celsius """
self.fahrenheit = float(input("Enter Degree Fahrenheit: "))
self.res = (5/9)*(self.fahrenheit-32)

def f2m(self):
""" Feet to Meters """
self.feet = float(input("Enter Feet: "))
self.res =(0.305*self.feet)

def quit(self):
return sys.exit(0)


if __name__ == "__main__":
OP = 'gay'
while OP == 'gay':
welcome()
conv = Conversion()
conv.ask()
conv.action()
conv.results()


Playing around with your code

I thought about making a request to Google and being able to convert anything, lol.

This post was edited by j0ltk0la on Jul 11 2015 12:42am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll