저는 우분투 13.04에서 파이썬 2.7로 작성한 작은 max_min 프로그램이 있습니다. 이 코드는 두 조건에서 깨지는 무한 루프 내부에서 사용자 입력을받습니다. 나는 9보다 큰 숫자를 입력 할 때 프로그램이 잘못된 결과를 반환한다는 것을 알아 차렸다. 내가 원하는 것은 사용자가 숫자를 입력 할 때마다 그 숫자를 이전 숫자와 비교하고 사용자로부터 입력 된 최대 및 최소 숫자를 얻는 것입니다. 예를 들어raw_input의 반복 비교
: 난처한 상황에 빠진
count = 0
largest = None
smallest = None
while True:
inp = raw_input('Please enter a number: ')
# Kills the program
if inp == 'done' : break
if len(inp) < 1 : break
# Gets the work done
try:
num = float(inp)
except:
print 'Invalid input, please enter a number'
continue
# The numbers for count, largest and smallest
count = count + 1
# Gets largest number
for i in inp:
if largest is None or i > largest:
largest = i
print 'Largest',largest
# Gets smallest number
for i in inp:
if smallest is None or i < smallest:
smallest = i
print 'Smallest', smallest
print 'Count:', count, 'Largest:', largest, 'Smallest:', smallest
:
Please enter a number:
10
Max: 1, Min: 0, Count: 1
가 최대가 여기에 10되지 1.해야 내 코드입니다.