0

I am trying to make a program for my college and I am stuck. My first problem is I have nothing in my console when I start the program. There should be some data from lista, but there is nothing. Second problem is I do not know how to make a single number from list items. I have to get two numbers, brojA and brojB. BrojA should be a number made by numbers from first part of the list items, and brojB should be a number from second part of items in list. Any ideas how to do that?

def fun(R):
    lista = []
    import random
    for i in range(0, R+1):
        a = random.randint(0, 9)
        lista.append(a)
    print(lista)

Here is the problem, when I call the method, there is nothing in list, but should be some numbers from for loop.

    prviBr = []
    drugiBr = lista
    for i in range(0, lista.length()/2):
        prviBr.append(lista[i])
        drugiBr.remove(drugiBr[i])
    brojA =
    brojB =

And here I need to get two numbers from each part of list items.

def main():
    ok = 0
    while ok == 0:
        R = int(input('Unesi broj: '))
        if R % 2 == 0 and R > 7:
            ok = 1
    fun(R)


if __name__=='__main__':
    main()

Its version 3.6 python.

  • 2
    This isn't a tutorial. If you get stuck, post a specific question with code that demonstrates the problem. Provide all inputs, and the expected output. [mcve] You have to show that you put effort into it, and ask for help on what you have done. Never post a problem and ask how to start, please. – Kenny Ostrom Jan 20 '19 at 17:57
  • what python version is this? – Paritosh Singh Jan 20 '19 at 17:57
  • 3.6 python version –  Jan 20 '19 at 17:59
  • @KennyOstrom I edited my post. Hope it is clearer now. –  Jan 20 '19 at 18:01
  • Isn't your function `fun` lacking `return`? – Daweo Jan 20 '19 at 18:03
  • Yes, but shouldnt it print anyway? –  Jan 20 '19 at 18:04
  • Is not clear to me how do you want to compose these two numbers. Just place the digits one next to the other? Let say you pick 2 and 4 from the list. Is the final result supposed to be 24? – Valentino Jan 20 '19 at 18:14
  • Yes, my result should be 24 –  Jan 20 '19 at 18:15
  • Ok, just to be sure: `a number made by numbers from first part of the list items` here you just what to line up the numbers of the list? No more random shuffling? – Valentino Jan 20 '19 at 18:20
  • no shuffling, just line up the numbers. Yes –  Jan 20 '19 at 18:23

1 Answers1

1

Here is a working code.

def fun(R):
    lista = []
    import random
    for i in range(0, R):
        lista.append(str(random.randint(0, 9)))
    print(lista)

    brojA = int(''.join(lista[0:(R // 2)]))
    brojB = int(''.join(lista[(R // 2):]))
    print(brojA, brojB)

def main():
    while True:
        R = int(input('Unesi broj: '))
        if R % 2 == 0 and R > 7:
            break
    fun(R)


if __name__ == '__main__':
    main()

So, let me explain your problems so that the answer is more useful to you.

First error is here: if __name__=='__name__': The name should be __main__. It does not print nothing because this first if statement fails and the main function is not called at all.

Second, I honestly don't understand your logic behing your second code snippet. In python this: drugiBr = lista with lists is generally not useful (have a look here). Beside this point, is not clear to me what you want to achieve.

So I rewrote this part using the string join() method. First, I save numbers as strings in the list: lista.append(str(random.randint(0, 9))) otherwise join() will raise an error.

Here brojA = int(''.join(lista[0:(R // 2)])) I create the numbers. ''.join() takes the elements of the iterable inside parentheses (should be strings) and create a single string joining them. I use the integer division operator // (if I write R / 2 the result is a float, and slicing fails). The slicing is on the first half of lista. The same for brojB, but slicing over the second half of lista.

I also rewrite the white loop in main(). Your is not wrong, but this is more readable in my opinion.

This script prints:

Unesi broj: 10
['5', '4', '7', '9', '6', '3', '0', '6', '6', '1']
54796 30661

Of course 10 is the input I gave and the numbers are random, but it shows how it works.

Hope it helps.

EDIT

My bad, I added an unnecessary +1. Now is correct and the list is divided halfway.

Valentino
  • 7,291
  • 6
  • 18
  • 34