Python 3.5.1 - Give user 3 invalid attempts then t

2019-09-17 11:17发布

问题:

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   

回答1:

The easiest for you is probably to make a loop for each prompt like in the following pseudo Python code:

# Beginning of prompt
i = 0
while i < 3:
    result = float(input(<question>))
    if <isok>:
        print(<result>)
        break
    else:
        print(<error>)
    i += 1
# Three failed inputs.
if i == 3:
    print('Bye, bye!')
    sys.exit(1)

at each <...> you will have to write something sensible for your program.

UPDATE

A variant with for loop:

# Beginning of prompt
ok = False
for i in range(3):
    result = float(input(<question>))
    if <isok>:
        print(<result>)
        ok = True
        break
    else:
        print(<error>)
# Three failed inputs.
if not ok:
    print('Bye, bye!')
    sys.exit(1)

Remember ok = False before each for loop.

UPDATE 2

This is the whole program as I see it, where you get 3 chances on each input. I have taken the liberty of adjusting your print statements.

import sys

# Beginning of prompt
ok = False
for i in range(3):
    miles = float(input('Type miles to be converted to km.\n'))
    if miles >= 0:
        milesToKm = miles * 1.6
        print('{} miles is {:.1f} kilometers.'.format(miles, milesToKm))
        ok = True
        break
    else:
        print('Wrong input, no negatives.')
# Three failed inputs.
if not ok:
    print('Bye, bye!')
    sys.exit(1)

# Beginning of prompt
ok = False
for i in range(3):
    inch = float(input('Give me inches to convert to cm.\n'))
    if inch >= 0:
        inchesToCm = inch * 2.54
        print('{} inches is {:.2f} centimeters.'.format(inch, inchesToCm))
        ok = True
        break
    else:
        print('Wrong input, no negatives.')
# Three failed inputs.
if not ok:
    print('Bye, bye!')
    sys.exit(1)

# Beginning of prompt
ok = False
for i in range(3):
    temp = float(input('Give me a Fahrenheit temp to convert to Celsius.\n'))
    if temp <= 1000:
        celsius = (temp - 32) * (5 / 9)
        print('{} degrees Fahrenheit is {:.1f} Celsius.'.format(temp, celsius))
        ok = True
        break
    else:
        print('Wrong input, too high.')
# Three failed inputs.
if not ok:
    print('Bye, bye!')
    sys.exit(1)

print('All OK')