Sequence of numbers – python

Question:

i create a program that reads a sequence of numbers, determines how many different numbers there are (we count the repetitions once), and writes the result to the standard output.
my first code:

f=int(input("String of numbers: "))
l=[]
for x in range(f):
    string_numbers = int(input(f'Enter {x+1} string of numbers: '))
    l.append(string_numbers)

mylist = list(dict.fromkeys(l))
print(len(mylist))

I wanted to take into account if the user entered a string too short or too long than declared. I wanted the user to type everything on one line. When I enter an incorrect string number, I get duplicated "incorrect string lengthincorrect string length"

f=int(input("String of numbers: "))
my_list = input('Enter numbers in the string, separated by spaces: ').split()
list_of_integers=[]
l=len(str(list_of_integers))
for i in my_list:
    list_of_integers.append((i))
mylist = list(dict.fromkeys(list_of_integers))
for i in range(f):
    if i < l:
        print("incorrect string length", end='')
    elif i > l:
        print("incorrect string length", end='')
    else:
Asked By: Negatyp

||

Answers:

It seems like you’re mixing up your different variables — f is what you want the length to be, l is just the number 2, and the way you’re comparing those two has nothing to do with the actual input entered by the user, which is my_list.

Using variable names that indicate their meaning might make it easier to keep it all straight:

num_count = int(input("Length of string of numbers: "))
num_list = input('Enter numbers in the string, separated by spaces: ').split()
if len(num_list) == num_count:
    print(f"there are {len(set(num_list))} different numbers")
else:
    print("incorrect string length")

In the above code, num_count is the count of how many (non-unique) numbers you expect them to input, and num_list is the actual list. To figure out if the list is the expected length, compare num_count to len(num_list).

Note that since all you’re doing is looking for unique values, converting the strings in num_list to int is not necessary (whether or not you use a set as I’ve done here).

Answered By: Samwise

You will most likely be better off using another function that ultimately has a while loop. This will make sure that when the user is giving the input that if anything is malformed you can then parse it checking and finally making sure to prompt the user again.

For example:

f=int(input("String of numbers: "))
my_list = input('Enter numbers in the string, separated by spaces: ').split()
list_of_integers=[]
l=len(str(list_of_integers))
for i in my_list:
    list_of_integers.append((i))
mylist = list(dict.fromkeys(list_of_integers))
for i in range(f):
    # XXX Here call your "input-function"
    get_user_input(i, l)


def get_user_input(user_len, len):
    while True user_len != len:
        print('Incorrect Input')
        user_len = int(input("String of numbers: "))
    return

This is not exactly a working example but with what you have you get the idea that you want to do a while loop until your inputs match.

Answered By: tijko
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.