samedi 18 juin 2016

Python 3.5.1 - Give user 3 invalid attempts then terminate pgm (Simple FOR loop)

I need assistance (new student - 2 weeks). I'd like to get the most minimal changes possible to this code that allows a user 3 chances at typing in the incorrect values for each conversion in the program. After typing in an incorrect value 3 times, the program should terminate. The only requirement is that the code must contain a FOR loop. I don't know if it requires one FOR loop or 3 FOR loops (one for each conversion). I've tried so many scenarios and can't seem to get it right.
Thank you !!!!

miles = float(input('Type miles to be converted to km.n')) 
if miles >= 0:
    milesToKm = miles * 1.6 
    print (miles, 'miles is', format(milesToKm, ',.1f'), 'kilometers.n')
    inch = float(input('Give me inches to convert to cm.n')) 
    if inch >=0:
       inchesToCm = inch * 2.54
       print (inch, 'inches is', format(inchesToCm, '.2f'), 'centimeters.n')
       temp = float(input('Give me a Fahrenheit temp to convert to Celsius.n'))
       if temp <= 1000:
          celsius = (temp - 32) * (5/9) 
          print (temp, 'degrees Fahrenheit is', format (celsius, '.1f'), 'Celsius.n')
       else:
          print ('Wrong input, too high.n')               
    else:
        print ('Wrong input, no negatives.n')
else:
    print ('Wrong input, no negatives.n')

One scenario I've tried but don't know how to incorporate the next conversion or get it just right.

count = 0
max = 1

for count in range (0, max):
    miles = float (input('Type miles to convert to kilometers?n'))
    if miles >=0:
        max = 1
        milesToKm = miles * 1.6
        print (miles, 'miles is', format(milesToKm, ',.1f'), 'kilometers.n')
        inch = float(input('Give me inches to convert to cm.n'))
    else:
        if max < 3:
            max = max + 1
            print ('Please use a non-negative number, try again.')

Thank you ! I modified what you listed into the format that I would need based on what we've learned so far. (We haven't learned sys.exit or breaks yet.) I also had to insert a count = 3 in the inner most loop as the pgm still wanted to run 3 times even with valid input. I know this is using a While loop. But is there a way to still do this as a 'For' Loop? Or is that not possible? (Hopefully the alignment below is good as I modified it in notepad.)

count = 0
while count < 3:
    miles = float(input('Type miles to be converted to km.n')) 
    if miles >= 0:
        milesToKm = miles * 1.6 
        print (miles, 'miles is', format(milesToKm, ',.1f'), 'kilometers.n')

        count = 0:
        while count < 3:
            inch = float(input('Give me inches to convert to cm.n')) 
            if inch >=0:
                inchesToCm = inch * 2.54
                print (inch, 'inches is', format(inchesToCm, '.2f'), 'centimeters.n')

                count = 0:
                while count < 3:
                    temp = float(input('Give me a Fahrenheit temp to convert to Celsius.n'))
                    if temp <= 1000:
                        celsius = (temp - 32) * (5/9) 
                        print (temp, 'degrees Fahrenheit is', format (celsius, '.1f'), 'Celsius.n')
                        count = 3

                    else:
                        print ('Wrong input, too high.n')               
                    count+=1

            else:
                print ('Wrong input, no negatives.n')
            count +=1

    else:
        print ('Wrong input, no negatives.n')
    count +=1   

Aucun commentaire:

Enregistrer un commentaire