Home > Mobile >  Creating a list with random numbers. Asking the user and check if the number is in the desired range
Creating a list with random numbers. Asking the user and check if the number is in the desired range

Time:10-19

list = []
import random
for i in range(20):
    list.append(random.randrange(1,9))
print("List = ", list)

number = int(input("Tell me a number between 1 and 9: "))

if number > 0 and number < 10 and number in list:
    print("The number",number,"is in the desired range", list.count(number), "times")
    for number in list:
        print("Indexes of", number, "=", list.index(number))


    ##########################

    # I can´t solve this last part correctly

CodePudding user response:

You can use enumerate to get the index and value of items in a list and check if the value is the number and then print the index

from random import randrange

numbers = [randrange(1, 9) for _ in range(20)]
print("numbers = ", numbers)

number = int(input("Tell me a number between 1 and 9: "))

if 0 < number < 10 and number in numbers:
    print("The number", number, "is in the desired range", numbers.count(number), "times")
    for index, val in enumerate(numbers):
        if val == number:
            print("Indexes of", number, "=", index)

OUTPUT

numbers =  [3, 6, 3, 3, 5, 8, 3, 2, 4, 4, 3, 5, 7, 6, 2, 1, 2, 4, 6, 8]
Tell me a number between 1 and 9: 4
The number 4 is in the desired range 3 times
Indexes of 4 = 8
Indexes of 4 = 9
Indexes of 4 = 17

CodePudding user response:

It seems you're looking for the indices. Additionally, use f-strings instead and name your variables correctly. That said, you could use

import random

# initialisation
lst = [random.randrange(1, 9) for _ in range(20)]
print("List = ", lst)

# input
number = int(input("Tell me a number between 1 and 9: "))

if number in lst and (0 < number < 10):
    print(f"The number {number} is in the desired range {lst.count(number)} times")

    indices = [index for index, value in enumerate(lst) if value == number]

    print(f"The indices are {indices}")

CodePudding user response:

Better don't use built-in function like list as variable and index return first index always, you need enumerate like below: (I try don't improve OP's code)

lst = []
import random
for i in range(20):
    lst.append(random.randrange(1,9))
print("List = ", lst)

number = int(input("Tell me a number between 1 and 9: "))

if number > 0 and number < 10 and number in lst:
    print("The number",number,"is in the desired range", lst.count(number), "times")
    idxs = [idx  for idx , num in enumerate(lst) if num==number]
    print("Indexes of", number, "=", idxs)

Output:

List =  [4, 6, 5, 6, 6, 3, 5, 7, 5, 4, 6, 3, 4, 7, 1, 5, 2, 1, 2, 6]
Tell me a number between 1 and 9: 3
The number 3 is in the desired range 2 times
Indexes of 3 = [5, 11]

you can try this approach:

(Beacuse all random number in range (1,10) if input number in this range number exist in lst and you don't need number in lst)

import random
lst = [random.choice(range(1,10)) for _ in range(20)]
print("List = ", lst)

number = int(input("Tell me a number between 1 and 9: "))
if number in range(1,10):
    print("The number",number,"is in the desired range", lst.count(number), "times")
    idxs = [idx  for idx , num in enumerate(lst) if num==number]
    print("Indexes of", number, "=", idxs)
  • Related