Home > Software design >  How do I stop Python from splitting a double digit integer into two single integers in my code
How do I stop Python from splitting a double digit integer into two single integers in my code

Time:01-21

This is the task

Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number

largest = None
smallest = None
while True: 
    num = input("Enter a number: ")
    if num == "done":
        break
    try:
        intnum = int(num)
    except ValueError:
        print('Invalid')
        continue
    for size in (num):
        if largest is None: 
            largest = size
        if smallest is None: 
            smallest = size
        if largest < size:
            largest = size
        if smallest > size:
            smallest = (size)
   

print("Maximum is", largest)
print('Minimum is', smallest)type here

The code works when I use one digit numbers but if i use a double digit number like 10 then then it prints 0 as the smallest number

for example if I entered

1,3,10,2,6, done

even though the smallest number is 1 it would print:

maximum is 6
minimum is 0

I'm struggling to figure it out so any help would be appreciated, thanks!

CodePudding user response:

You were almost right, you can remove the for loop and just compare the maximum and minimum values with the intnum variable. The code would look like this.

largest = None
smallest = None

while True:
    num = input("Enter a number: ")
    if num == "done":
        break
    try:
        intnum = int(num)
    except ValueError:
        print('Invalid')
        continue
    if largest is None:
        largest = intnum
    if smallest is None:
        smallest = intnum
    if largest < intnum:
        largest = intnum
    if smallest > intnum:
        smallest = (intnum)

print("Maximum is", largest)
print('Minimum is', smallest)

This works fine.

CodePudding user response:

You can reduce if statement by apply reverse cycology.

From starting initializing largest with the smallest value i.e(float('-inf')) and smallest with the largest value i.e.(float('inf'))

and checking the user input if the number>largest modify largest. number<smallest modify smallest

One edgecase let's say user not input any number directly rights done than it will print inf -inf which is not correct. so just converted them into no minimum no maximum

Code:

largest = float('-inf')
smallest = float('inf')

while True:
    num = input("Enter a number: ")
    if num.lower() == "done":  #Works for Done also..
        break
    try:
        intnum = int(num)
    except ValueError:
        print('Invalid')
        continue
    if intnum>largest:
        largest=intnum
    if intnum<smallest:
        smallest = intnum

print("Maximum is", largest if largest!=float('-inf') else 'No maximum')
print('Minimum is', smallest if smallest!=float('inf') else 'No minimum')

Also you can take both if inside the try statement also..!

Output:

When user not input any number

Enter a number: done
Maximum is No maximum
Minimum is No minimum

When user input only one number

Enter a number: 4
Enter a number: done
Maximum is 4
Minimum is 4

Normal case when user input more than 1 number

Enter a number: 2
Enter a number: 7
Enter a number: 1
Enter a number: 3
Enter a number: 8
Enter a number: 9
Enter a number: done
Maximum is 9
Minimum is 1
  • Related