0

I need help to create a function that generates 8 digit random numbers with 2 favorite number sequencing anywhere.

def favourite_number(first, second):
    phone = random.randint(600000,699999)
    generate = first,second,phone
    return generate
print(favourite_number(8,6))

This is the result I am getting but I want it to be randomly placed anywhere not compulsorily in the beginning and that too without comma.

"C:\Users\WIN Ultimate\PycharmProjects\new\venv\Scripts\python.exe" "C:/Users/WIN Ultimate/PycharmProjects/new/Numbergenerator.py"
(8, 6, 699936)
1
  • The commas are just part of the way that lists and tuples are printed. Commented Sep 28, 2019 at 14:52

2 Answers 2

1

I think that this is what you're looking for (:

import random
def favourite_number(first, second):
    phone = random.randint(600000,699999)
    generate = first,second,phone
    generate = [str(x) for x in generate]
    return ''.join(generate)
print(favourite_number(8,6))

There are many more ways to do it!!

Sign up to request clarification or add additional context in comments.

3 Comments

Every time I run code the 2 digits are at beginning I want them to be anywhere indexing randomly not at a specific index 0 and 1. Example:- 25154862, 25154286, 55861355 at every different run code I want different value.
I've just edited my answer so it'll match your requirements (:
If you've found my answer helpful I'd love it if you could accept it for my reputation please (:
1

Use a list instead of tuple, so you can insert in a random index.

def favourite_number(first, second):
    phone = random.randint(600000,699999)
    generate = [first, second]
    generate.insert(random.randint(0, 2), phone)
    return generate

If you want to print it without commas, use:

print(" ".join(map(str, favourite_number(8, 6))))

2 Comments

This is the output I am getting: Traceback (most recent call last): File "C:/Users/WIN Ultimate/PycharmProjects/new/Numbergenerator.py", line 20, in <module> print(" ".join(map(str, favourite_number(8, 6)))) NameError: name 'favourite_number' is not defined
I had a typo in the function name, it's fixed now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.