Python Forum
Need help I’m new at this - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Need help I’m new at this (/thread-24425.html)



Need help I’m new at this - Pelaw - Feb-13-2020

I have a number generator app I’m building. When i get the return i get the numbers looking like this. [2, 3, 5, 6] how can i get only the numbers without the [ ] marks. Any help will be appreciated


RE: Need help I’m new at this - buran - Feb-13-2020

you get a list of numbers and [ and ] are visible when you print that list. Just print the element of the list, not the list itself.
Of course showing your code would help to help you more...


RE: Need help I’m new at this - Pelaw - Feb-13-2020

Here is my code:

import random
import ui

def Function():
    v = ui.View()
    v.background_color = 'white'
    v.background_frame = '0, 0, w, h'
    
    for i in range(1,5):
        b = ui.Button()
        b.frame=(-15+i*70,400,60,70)
        b.border_width= 1.5
        b.border_color = 'black'
        b.title = str(i)
        b.background_color='lightgray'
        b.action = b_action
        v.add_subview(b)
        b.tint_color = 'red'
        
    l = ui.Label(
    alignment=ui.ALIGN_CENTER,
    bg_color='white',
    border_color='black',
    border_width=1.5,
    frame=(50, 100, 100, 50),
    name='l', font=('Copperplate', 18))
    
    
    l.text_color='red'
    l.number_of_lines = (2)
    l.frame = (12,100,350,80)
    v.add_subview(l)
    v.present('screen')
    
def b_action(sender):
    x = sender.title
    l = sender.superview['l']
    if x == '1': l.text = 'Megabucks   '+str(Lottery.lotteryLogic (1, 46, 6))
    elif x == '2': l.text = 'Lucky 4 Life    '+str(Lottery.lotteryLogic (1, 48, 5))+str(Lottery.lotteryLogic (1, 18, 1))
    elif x == '3': l.text = 'Powerball   '+str(Lottery.lotteryLogic (1, 69, 5))+str(Lottery.lotteryLogic (1, 26, 1))
    elif x == '4': l.text = 'Mega Mil   '+str(Lottery.lotteryLogic (1, 70, 5))+str(Lottery.lotteryLogic(1, 25, 1))

class Lottery:
    def __init__( self ): 
        self.a = 0

    def lotteryLogic(startPosi, endPosi, interateNumber):
        a = random.sample (range(startPosi, endPosi), interateNumber)
        a.sort()
        return a

if __name__ == '__main__':
    Function()



RE: Need help I’m new at this - buran - Feb-13-2020

instead of str(Lottery.lotteryLogic(1, 46, 6)
you can do ', '.join(Lottery.lotteryLogic(1, 46, 6))

However, there is much bigger problem in your code - Lottery.lotteryLogic
This should be an instance method, and you should pass self as first argument. Probably draw() would be better name for this method
Then create an instance and call the method.
You may pass max value when instantiate the object and only call the draw method with number of balls you want
Finally curently you can get repeating numbers (i.e. drawing one number, that is already in the previous 5)

I don't know ui module and cannot advise, but creating gui in a function is bit odd.


RE: Need help I’m new at this - Pelaw - Feb-13-2020

Thanks i will work on it. Im confuse with the (created an instance and call the method)


RE: Need help I’m new at this - Pelaw - Feb-13-2020

Im stuck if there’s any way you can help me set this up. Im learning and want to be good at it but running to some stuff i cant fix. Sorry for the inconvenience


RE: Need help I’m new at this - ndc85430 - Feb-14-2020

Have you taken on a project that's too big right now? If you don't understand fundamental things like "creating an instance", perhaps you need to spend more time on the basics?


RE: Need help I’m new at this - Pelaw - Feb-15-2020

Ok thanks