简体   繁体   中英

Function only returning first input of a list and not continuing in the input is outside a range

I'm really new to coding (as in about 3 weeks in), and I'm writing a code where the user is asked to input a number between 0 and 100 and then this number is added to a list. Once the list reaches a length of 10 numbers, it should print the list. I'm using a function to this.

I can get it so it prints a list, but it's only printing the first number that is input. For example if I enter 5, it'll still ask for the other numbers, but it prints the list as [5, 5, 5, 5, 5, 5, 5, 5, 5, 5]. Also if I enter a number outside the range of 0-100 it does ask you to input a different number, but then it stops and prints the list as 'None'.

Below is my code:

def getUser(n):
    mylist = []
    while 0 < n < 100:
        mylist.append(n)
        int(input("Please enter a number between 0 and 100: "))
        if len(mylist) == 10:
            return(mylist)
    else:
        int(input("This number is not in the range of 0-100, please input a different number: "))
    
    
n = int(input("Please enter a number between 0 and 100: "))
print("The numbers you have entered are: ", getUser(n))

I'm guessing its to do with the while/else loop but as I said I'm new to this and it all feels so overwhelming, and trying to google this just seems to bring about super complicated things that I don't understand!! Thanks

First : Why you get [5, 5, 5, 5, 5, 5, 5, 5, 5, 5] :

int(input("Please enter a number between 0 and 100: "))

You should give this to n first :

n=int(input("Please enter a number between 0 and 100: "))

Then, it gets out of the loops because you go in the else , and then you don't go back in the while . Then your function ended, without return , so none

To be honest, I don't understand why your code gets out of the while loop if you give a number out of the range, as the value is not given to n. If someone could explain in comment, that would be great !

I would do it like that :

def getUser():
    mylist = []
    while len(mylist) < 10:
        n = int(input("Please enter a number between 0 and 100: "))
        
        if (0 < n < 100):
            mylist.append(n)
        else:
            print('This is not between 0 and 100 !')

    return mylist
    
    
print("The numbers you have entered are: ", getUser())

Then you will be asked number from 0 to 100 (excluded) until you get a size of 10.

So here's what you were doing wrong

def getUser(n):
    mylist = []
    while 0 < n < 100:
        mylist.append(n)
        n = int(input("Please enter a number between 0 and 100: "))
        if len(mylist) == 10:
            return(mylist)
    else:
        int(input("This number is not in the range of 0-100, please input a different number: "))
    
    
n = int(input("Please enter a number between 0 and 100: "))
print("The numbers you have entered are: ", getUser(n))

You notice any difference in the edited code?

You were never re-assigning the value of the variable n So you would just add whatever value you passed to getUser every time (and it would be the same value every time)

Don't be afraid to ask for help if you're stuck!

PS You can also move a line somewhere to somewhere else to make it behave a bit better if you're up to the challenge ;)

def getUser(n):
mylist = []
t = True
while t:
    n =int(input("Please enter a number between 0 and 100: "))
    if n >0 and n<=100:
        mylist.append(n)
    if len(mylist) == 10:
        return(mylist)
        t = False
    elif n < 0  or n >100:
        print('This number is not in the range of 0-100, please input a different number:')
    
        

Ok, you are trying to use while instead of if . Here is what I would do - I will explain it soon:

def get_user():
    mylist = []
    for i in range(5):
        n = int(input("Please enter a number between 0 and 100: "))
        if n <= 100 and 0 <= n:
            mylist.append(n)
            
        else:
            print("This is not between 0 and 100 !")
            i = i - 1

    return mylist

Going through the code

for i in range(5)

Here we use the for to iterate through a range of numbers (0 - 5). for will assign i to the smallest number in the range and constantly add 1 to i every iteration until i is out of the range . See this for more info on range

if n <= 100 and 0 <= n:

Here and checks if 2 expressions are true. In this case, if n is less than or equal to hundred and more than or equal to 0. You could use and any number of times you want like this:

if 1 < 100 and 100 > 200 and 20 < 40:
   print("foo!")

Now look at the else :

else:
    i = i - 1

Here, we subtract 1 from i and thus, decreasing our progress by one. Also note that my function is called get_user() and not getUser() . In python, we use snake case and not camel case.

I hope this answer helps. I have given small hints in my answer. You should google them! I also am not directly giving you the logic. Try to figure it out! That way you would have a good understanding of what I did.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM