adplus-dvertising

Python Scripting not sure whats wrong in my script and why it is not running

Asked 3 months ago
Viewed 10 times

The code is to generate 6 unique random numbers from 10 to 99. Once the function is completed it will print the six random numbers on one line with a – between each number. The code

import random

def random6unique():
# list to store random numbers
random_nums = []

i = 0
# looping till 6 unique random numbers are generated
while i < 6:
    # generating a random number
    num = random.randint(10,99)
    # checking if its not present in list
    # then adding it to list and incrementing i
    if num not in random_nums:
        random_nums.append(num)
        i += 1

# printing the numbers in the given format
print(str(random_nums[0]),end="")
for j in range(1,6):
    print("-" + str(random_nums[j]),end="")

print()

asked 3 months ago

Correct Answer

Pay close attention to indentation and the function must be called to work:

import random

def random6unique():
# list to store random numbers
    random_nums = []

    i = 0
    # looping till 6 unique random numbers are generated
    while i < 6:
        # generating a random number
        num = random.randint(10,99)
        # checking if its not present in list
        # then adding it to list and incrementing i
        if num not in random_nums:
            random_nums.append(num)
            i += 1

    # printing the numbers in the given format
    print(str(random_nums[0]),end="")
    for j in range(1,6):
        print("-" + str(random_nums[j]),end="")

random6unique()
answered 3 months ago