1

I'm pretty new to Python and working on a one and two-player guessing game. Right now I'm working on creating a random number that is the length of digits that the user specifies. I can't seem to figure out a way to make all of the numbers print to one string. They only print out in new lines and one at a time. I think I have a decent concept idea but can't figure out how to make it work. Any advice and help is appreciated. Thank you!

import random
import sys

def p(): #Prints new line quickly
    print()

def error(): #Restarts the main
    question = input("You have entered an invalid option. Would you like to try again? \n")
    if question in ("Yes","yes","y"):
        intro()
    else:
        sys.exit()

def oneplayer():
    print("Welcome to the one player mode of the guessing game.")
    p()
    print("How many digits would you like your number to be?")
    SecretNumber=0
    FinalNumber = 0
    digits=input()
    count = 0
    while int(digits) > count:
        ProduceNewNumber=0
        ProduceNewNumber=ProduceNewNumber+random.randrange(10)
        count=count+1
        SecretNumber=ProduceNewNumber
        print(str(SecretNumber))
        FinalNumber=(ProduceNewNumber)
def twoplayer():
    print("Welcome to the two player mode of the guessing game.")

def intro():
    print("Welcome to the guessing game! Would you like to play one player or play with two players?\n")
    players = input()
    if players in ("1","2"):
        if players in ("1"):
            oneplayer()
        if players in ("2"):
            twoplayer()
    else:
        error()


intro()

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Mason
  • 11
  • 1
  • 1
    Are you referring to what's going on in the `while` loop? Note how you're printing, and you can't really concatenate prints. It would be much simpler to store the numbers in a list, then call `"". join` on the list. – Carcigenicate Feb 21 '20 at 19:52
  • Welcome to Stack Overflow! Check out the [tour] and [ask]. You need to provide a [mre] including some example input, expected output, and actual output. You can [edit] the question. FWIW, `FinalNumber` is not used, and setting `ProduceNewNumber = 0` is pointless. – wjandrea Feb 21 '20 at 19:53
  • 1
    You could just pick a random n-digit number: `random.randrange(10**(digits-1), 10**digits)` – 001 Feb 21 '20 at 19:55
  • 1
    Use numpy to generate the numbers as ` ' '.join(np.random.randint(low=0, high=100, size=(100)).astype("str"))`, where `np.random.randint(low=0, high=100, size=(100))` generates 100 numbers with each number between 0 and 100(non-inclusive), `.astype("str")` converts all the numbers to string and `" ".join()` joins the passed string array with space among them – dumbPy Feb 21 '20 at 19:59

0 Answers0