Python while循环未结束

2024-10-03 17:21:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些while循环问题。我的目标是,如果用户输入负值3次,那么while循环结束。问题是当我看到第三条信息时,它并没有像它应该的那样以“milesLoop”结尾 (我试过使用一个循环,但我想用多个循环进行测试) 这个问题可能很容易解决,但我可能很容易解决。在

这是python代码:

__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')

Tags: toyouinputifnotfloatthismany
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))
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)

相关问题 更多 >