0

I'm having some while Loop issues. My goal is to make it so the while loop end if the user enters a negative value 3 times. The issue is when I get to the third message it doesn't end with the "milesLoop" like it should (I tried using one loop but I want to test using multiple loops) This may be an easy issue to fix but I'm stuck.

This is the python code:

__author__ = 'MichaelCorbett'
import sys

print('Michael Corbett converter ')
print('\n')

milesLoop = 1
fhietLoop = 1
gallonLoop = 1
poundsLoop = 1
inchesLoop = 1

while milesLoop == 1:

    miles = float(input('What\'s up Will, how many miles do you wish to convert to Kilometers? '))
    if miles < 0:
        print('This converter does not accept negeative values. Try again!')

        miles = float(input('What\'s up Will, how many miles do you wish to convert to Kilometers? '))
        if miles < 0:
            print('This converter does not accept negeative values. Try again')

            miles = float(input('What\'s up Will, how many miles do you wish to convert to Kilometers? '))
            if miles < 0:
                print('This converter does not accept negeative values. Program is Terminated')
                milesLoop = 2


                while fhietLoop == 1:

                    Fheit = float(input('What temperature is it outside in Fahrenheit? '))
                    if Fheit < 0 and Fheit > 1000:
                        print('This converter does not accept negeative values.')

                        Fheit = float(input('What temperature is it outside in Fahrenheit? '))
                        if Fheit < 0 and Fheit > 1000:
                            print('This converter does not accept negeative values.')

                            Fheit = float(input('What temperature is it outside in Fahrenheit? '))
                            if Fheit < 0 and Fheit > 1000:
                                print('This converter does not accept negeative values. Program is Terminated')
                                fhietLoop = 2


                            while gallonLoop == 1:

                                gallon = float(input('How many gallons are you trying to convert? '))
                                if  gallon < 0:
                                    print('This converter does not accept negeative values.')

                                    gallon = float(input('How many gallons are you trying to convert? '))
                                    if  gallon < 0:
                                        print('This converter does not accept negeative values.')

                                        gallon = float(input('How many gallons are you trying to convert? '))
                                        if  gallon < 0:
                                            print('This converter does not accept negeative values. Program Terminated')
                                            gallonLoop = 2


                                        while poundsLoop == 1:

                                            pounds = float(input('How many pounds would you like to convert? '))
                                            if  pounds < 0:
                                                print('This converter does not accept negeative values.')

                                                pounds = float(input('How many pounds would you like to convert? '))
                                                if  pounds < 0:
                                                    print('This converter does not accept negeative values.')

                                                    pounds = float(input('How many pounds would you like to convert? '))
                                                    if  pounds < 0:
                                                        print('This converter does not accept negeative values. Program Terminated')
                                                        poundsLoop = 2


                                                    while inchesLoop == 1:

                                                        inches = float(input('How many inches would you like to convert? '))
                                                        if  inches < 0:
                                                            print('This converter does not accept negeative values.')

                                                            inches = float(input('How many inches would you like to convert? '))
                                                            if  inches < 0:
                                                                print('This converter does not accept negeative values.')

                                                                inches = float(input('How many inches would you like to convert? '))
                                                                if  inches < 0:
                                                                    print('This converter does not accept negeative values. Program Terminated')
                                                                    inchesLoop = 2



                                                                # Calculations

                                                                kilometers = miles * 1.6
                                                                celsius = int((Fheit - 32) * 5/9)
                                                                liters = gallon * 3.9
                                                                kilograms = pounds * .45
                                                                centimeters = inches * 2.54

                                                                # Output

                                                                print('\n')
                                                                print(miles,  ' miles is ',  kilometers,  ' Kilometers')
                                                                print('Its is ', celsius, 'Celsius outside.')
                                                                print(gallon,  ' gallons is ',  liters,  ' liters')
                                                                print(pounds,  ' pounds is ',  kilograms,  ' kilograms')
                                                                print(inches,  ' inches is ',  centimeters,  ' centimeters')
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
shurburt
  • 93
  • 2
  • 6
  • 4
    wow, you probably want to to restructure your code right now, why? because it's nested like crazy, that you cannot even debug your own code that consists of simple `if`s and `while`s – taesu Sep 14 '15 at 15:45
  • This is rather a lot of code with loads of copying (like using each `if` statement three times) please narrow it down to the minimal code reproducing this error. – Adriaan Sep 14 '15 at 15:46
  • 1
    what do you think your code does when the user enter a positive value upfront? – 301_Moved_Permanently Sep 14 '15 at 15:47
  • `import this` (line 5) – Alexander Sep 14 '15 at 15:58
  • 4
    This may be useful to you: [Asking the user for input until they give a valid response](http://stackoverflow.com/q/23294658/953482) – Kevin Sep 14 '15 at 16:00

2 Answers2

3
def get_float(prompt):
    while True:
       try:
          return float(input(prompt))
       except:
          print "Thats not a number!"


def get_positive_number(prompt,tries=3):
    for i in range(tries):
         result = get_float(prompt)
         if result >= 0: return result
         print "Sorry Negative not allowed %d/%d"%(i,tries)

while True:
     result = get_positive_number("How Many Gallons?")
     if result is None: 
        print "OK DONE"
        break
     print "Convert %0.2f Gallons"%(result)
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
-2
__author__ = 'MichaelCorbett'
import sys

def myLoop(question, tries = 0):
    while True: 
        if tries == 3:
            sys.exit()
        user_in = float(input(question))
        if user_in < 0:
            print('This converter does not accept negeative values. Try again!')
            tries += 1
        else:
            return user_in

print('Michael Corbett converter\n')

miles = myLoop('What\'s up Will, how many miles do you wish to convert to Kilometers?')
Fheit = myLoop('What temperature is it outside in Fahrenheit?')
gallon = myLoop('How many gallons are you trying to convert?')
pounds = myLoop('How many pounds would you like to convert?')
inches = myLoop('How many inches would you like to convert?')

# Calculations

kilometers = miles * 1.6
celsius = int((Fheit - 32) * 5/9)
liters = gallon * 3.9
kilograms = pounds * .45
centimeters = inches * 2.54

print('\n{0} miles is {1} Kilometers'.format(miles, kilometers))
print('Its is {0} Celsius outside.'.format(celsius))
print('{0} gallons is {1} liters'.format(gallon, liters))
print('{0} pounds is {1} kilograms'.format(pounds, kilograms))
print('{0} inches is {1} centimeters'.format(inches, centimeters))
Cody Bouche
  • 945
  • 5
  • 10
  • 1
    1) You have 5 functions that do almost the same thing, so they could be replaced by a single function that accepts an argument for the type of conversion desired. 2) Why use recursion when you can do the same thing with a simple loop? Recursion has extra overheads, and is best avoided in Python, unless it's natural for the problem domain, eg working with recursive data structures like trees. 3) There's only one "e" in "negative". – PM 2Ring Sep 14 '15 at 16:14
  • Yes this can be rewritten into a single function. I copy pasted the text so there may be typos in there. These recursions use tail-call optimization to avoid allocation a new stack frame for the recursive function. – Cody Bouche Sep 14 '15 at 16:22
  • 1
    Python doesn't have tail-call optimization. I realised that the spelling error was due to copy-pasting, but that doesn't prevent you from fixing it. :) OTOH, you're certainly not obliged to fix such errors. – PM 2Ring Sep 14 '15 at 16:27
  • Didn't realize python didn't have tail-call optimization. I guess it would be too complex to implement in a dynamic language. Learned something new ;) – Cody Bouche Sep 14 '15 at 16:31
  • Yes, it would be tricky to make TCO work in all cases. But mostly it's because TCO would interfere with stack traces. See [this answer](http://stackoverflow.com/a/13592002/4014959) and its links for more info. – PM 2Ring Sep 14 '15 at 16:42
  • Thank you code. Your idea was exactly what i needed. – shurburt Sep 15 '15 at 07:14