1

I want to make a program to sort lists with numbers in ascending order with values between 1 and 100. This is what I did and if you run the code the list prints multiple times and doesn't point out the errors correctly:

# This is the user input
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
c = int(input("Enter the third number: "))
d = int(input("Enter the fourth number: "))
e = int(input("Enter the fifth number: "))
f = int(input("Enter the sixth number: "))
# This the list to sort
array = [a, b, c, d, e, f]
#This will count how many time each value is repeated
aa = int(array.count(a))
bb = int(array.count(b))
cc = int(array.count(c))
dd = int(array.count(d))
ee = int(array.count(e))
ff = int(array.count(f))
# This will loop through the list
for i in array:
    # This is checking if any values are repeated or not between 1 and 100
    if i > 100 or i < 1 or aa and bb and cc and dd and ee and ff > 1:
        print("Number should be smaller than 100 and larger than 1 and numbers can't be repeated")
    # This is to print the sorted list if you get everything right
    else:
        array.sort()
        print(array)
deceze
  • 510,633
  • 85
  • 743
  • 889
hmood
  • 603
  • 7
  • 25
  • See https://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true… – deceze Feb 28 '20 at 14:05
  • Does this answer your question? [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) And also: You are printing in a loop, thats why you print multiple times. You have to rethink your logic there – Ocaso Protal Feb 28 '20 at 14:07
  • FWIW: `l = [int(input()) for _ in range(6)]`, `if len(set(l)) != len(l) ...`, `l.sort()`… – deceze Feb 28 '20 at 14:09

2 Answers2

2

I think you want to replace your and statements by or statements in your if...aa and bb and cc and dd and ee and ff > 1:. Otherwise, the condition will be True only if all numbers are repeated.

if i > 100 or i < 1 or aa or bb or cc or dd or ee or ff > 1:

NOTE : this will print Number should be smaller than 100 and larger than 1 and numbers can't be repeated 6 times, is that what you really want ?

Otherwise, there is a shorter way to do what you (probably) want to do:

  • check if max(array) > 100
  • check if min(array) < 1
  • transform array to a set (which only keeps unique values) and compare their length

Try this :

# This is the user input
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
c = int(input("Enter the third number: "))
d = int(input("Enter the fourth number: "))
e = int(input("Enter the fifth number: "))
f = int(input("Enter the sixth number: "))

# This the list to sort
array = [a, b, c, d, e, f]

if max(array)>100 or min(array)<1 or len(set(array)) != len(array):
    print("Number should be smaller than 100 and larger than 1 and numbers can't be repeated")
else:
    array.sort()
    print(array)
Phoenixo
  • 2,071
  • 1
  • 6
  • 13
1

Alternative approach: Use a set to collect the numbers you are accepting, and use it to reject numbers that are already there

I know it is not the intention of StackOverflow to write the code for others, but of many things I would like to recommend you, I can't but express in code. Here it is, something that I think you can analyze to learn somethings.

(It needs Python 3.8 or greater):

accepted_numbers = set() # Begin with an empty bag


## Main Job ##
def main():
  for i in range(1, 6+1):
      while not satisfy_conditions(number := get_ith_number(i)):
          notify_error()
      accepted_numbers.add(number)

  report()
## END Main Job ##


## Support ##

def satisfy_conditions(number):
    # Returns True if the number is not in the bag and is between 1 and 100
    return number not in accepted_numbers and (1 <= number <= 100)

def get_ith_number(i):
    word_number = {1:'first', 2:'second', 3:'third', 4:'forth', 5:'five', 6:'sixth'}
    return int(input(f'Enter the {word_number[i]} number: '))

def notify_error():
    print("Number should be smaller than 100 and larger than 1 and numbers can't be repeated")

def report():
    print(sorted(accepted_numbers))

## END Support ##

if __name__ == '__main__':
    main()
jgomo3
  • 1,153
  • 1
  • 13
  • 26