Home > Enterprise >  i am working to create a function but this code appears TypeError: 'int' object does not s
i am working to create a function but this code appears TypeError: 'int' object does not s

Time:12-23

def num():
    while True:
        n= int(input("donnez le num"))
        if n > 0:
            break

    T=([int]*n)
    for i in range (0,n):
        n[i]=i
num()
print(T)

The code is mainly about creating a table after asking the user a number in this table i want to fill it with number and descending example: the user puts 10 desired result:

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

this is the code

this is the outpout

CodePudding user response:

couple of issues in your code

  1. your code is not returning any value , so T is not defined outside of function
  2. n[i] meant to be T[i]
  3. you need to change the range to n 1
  4. you can shorten/optimize your code as follows

so:

def num():
    while True:
        n= int(input("donnez le num"))
        if n > 0:
            break
    return [i for i in range(0,n 1)]

print(num())

et voila, output:

donnez le num 10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  • Related