'Creating an error message for an invalid input in python

I have been trying to create a program that creates a list from user input. The program is meant to give an error message to the user input when the length of the list is more or less than nine (9)

For example: "Please enter nine (9) numbers from 0 - 100: "

input 4 5 6 7 8 9 output Sorry! Numbers should be up to nine.

or input 5 67 8 2 90 65 3 45 2 7 1 0 "Sorry! Numbers should not be more than nine."

my code:

number = []
while True:
    num =  input("Please type numbers: ")
    num = list(map(int, num.split()))
    number.append(num)
    if len(number) > 9:
        print ("Numbers should not be more than 9!")
    
    else:
        break

print (number)

I don't know what I am doing wrong.

number = []
num = input("Please type numbers: ")
num = list(map(int, num.split()))
number.append(num)
while True:

    if (len(number)) == 7:
        print ("List is complete")
    elif (len(number)) < 7:
        print ("List is not complete")
    else:
        print ("List is more than 7")
    break

this code works fine when I'm not asking for user input. However it brings back the output "List is not complete" whenever I run it without regard for the length of the string.



Solution 1:[1]

Your code creates a list within a list, like this

[[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]]

to solve this problem, you just have to replace number.append(num) by number = num or simply use directly the variable num like this

num = input("Please type numbers: ")
num = list(map(int, num.split()))


while True:

    if (len(num)) == 7:
        print ("List is complete")
    elif (len(num)) < 7:
        print ("List is not complete")
    else:
        print ("List is more than 7")
    break

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 crazycat256