D9M2W
Posts: 37
Joined: Tue Dec 30, 2014 5:34 pm
Location: West Midlands, UK

Struggling a bit with Python

Sat May 14, 2016 5:38 pm

Hello guys and gals,

Feel a bit stupid to be honest writing this post. But I am honestly struggling a bit to get my head around some of Python.

I am going through a course my company told me to do, on Coursera, which is the following (in case anyone is interested):
https://www.coursera.org/learn/interact ... me/welcome

But I just can't get my head around somethings, like how to put together the small bits of Python I have learnt to create tiny programs.
Hell even the functions have been throwing me out. Considering this is my career path now it really is getting me down because I feel so stupid that I cannot grasp what I supposed to be learning =/

The first program is basically a rock paper scissors game where the computer plays against itself and each side picks different every time and also prints who wins. Which has just got me down.

Anyone else really struggled in the early steps? Or am i that stupid :L

User avatar
Cancelor
Posts: 757
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Struggling a bit with Python

Sat May 14, 2016 5:43 pm

Sounds like a typical learning curve, it happens to us all.

Do you have a raspberry pi to practice on?

Start simple and practice lots. Tutorials can be a bit dry some times.
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

D9M2W
Posts: 37
Joined: Tue Dec 30, 2014 5:34 pm
Location: West Midlands, UK

Re: Struggling a bit with Python

Sat May 14, 2016 6:08 pm

Cancelor wrote:Sounds like a typical learning curve, it happens to us all.

Do you have a raspberry pi to practice on?

Start simple and practice lots. Tutorials can be a bit dry some times.
I do indeed mate I have both a Pi 2 and a B+.

I cant help but feel stupid about it, it is quite demoralizing. Do you think I should look through CodeAcademy as a bare starting point, then come back to the Coursera course?

User avatar
B.Goode
Posts: 8987
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Struggling a bit with Python

Sat May 14, 2016 6:13 pm

Congratulations on your honesty.

Most people struggle, at least a bit, when confronted with a new programming language. The syntax, the commands, the built-in libraries, the whole 'culture'. There probably isn't an easy way through this, you simply have to get familiar and comfortable through exposure and experience.

The other aspect might be that some people don't immediately 'get' the 'big picture' of 'computational thinking' - of breaking problems into solvable steps, of modelling a real-world situation in an artificially-created environment. (But as a gamer that bit is probably second nature?)

Do you know which aspect is uppermost for you, or is it the cocktail of the two things?

I don't know anything about the Coursera offering, but as a registered student do you have access to tutors/mentors? Do you have an online community with other students registered on the course? Asking specific questions about the course material might be a better way of moving forward, rather than throwing a wide net here where you may get well-intentioned advice that is at odds with what the course material is trying to get across.

I have previously provided links to other sources of help and advice, but perhaps for now you should focus on the material from the course your employer has referred you to?

A lot of words there: really @cancelor nailed it: "Sounds like a typical learning curve, it happens to us all... Start simple and practice lots. "

PS: and your follow-up reminded me to point out what it says on the initial page of the Coursera website:
While the class is designed for students with no prior programming experience, some beginning programmers have viewed the class as being fast-paced. For students interested in some light preparation prior to the start of class, we recommend a self-paced Python learning site such as codecademy.com.
I think you know the answer...

D9M2W
Posts: 37
Joined: Tue Dec 30, 2014 5:34 pm
Location: West Midlands, UK

Re: Struggling a bit with Python

Sat May 14, 2016 6:25 pm

Indeed you have I remember your name - Job changed in the past a bit so I know longer needed to learn! However this time really seems I have to as Ill be moving into a development department soon, and will have to learn C# at some point, but they are starting with Python to get things moving

I am a big gamer, so you would think Id understand - It seems to be that I can understand things when I really rip things a bit line by line, however in the whole big picture I cant seem to stitch things together, which gets me down.

Like with the rock, paper, scissors example, i know I need functions for each player and random numbers to be generated for each players choice and then compare each answer and print the win.
But I can't seem to piece together how to do it in the big picture, like I know how each component works on it;s own. but together, I am at a loss (Might have explained that badly though)

I have loaded Codeacademy back up, Ill need new notebooks before long the amount im writing and drawing

ghans
Posts: 7878
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Struggling a bit with Python

Sun May 15, 2016 7:56 am

Does this article resonate with you ? It certainly did for
me. Some advice is also offered.

https://www.vikingcodeschool.com/posts/ ... -damn-hard


ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

Re: Struggling a bit with Python

Sun May 15, 2016 10:08 am

I remember the Coursera course, I took it a few years ago. I learned a lot from it. Like you said "each component works on it's own", so do one thing at a time. That is what this assignment is about. The helper functions will work independent of the main function. If the parameter is valid they will return a valid value. The main function will call both helper functions and then determine the winner of the round.

First you need a function to convert a number to a string
0 = rock
1 = Spock
2 = paper
3 = lizard
4 = scissors

Code: Select all

define function that take an int as a parameter (it will return a string)
    if parameter equals
        value is a string
    elif parameter equals
        value is a string
    elif parameter equals
        value is a string
    elif parameter equals
        value is a string
    elif parameter equals
        value is a string
    elif parameter equals
        value is a string
    (else is optional and takes more steps to account for)
    else
        value is not valid
    return the string value
Then you need a function to convert string to number
rock = 0
Spock = 1
Paper = 2
lizard = 3
scissors = 4

Code: Select all

define function that take a string as a parameter (it will return an int)
    if parameter equals
        value is an int
    elif parameter equals
        value is an int
    elif parameter equals
        value is an int
    elif parameter equals
        value is an int
    elif parameter equals
        value is an int
    elif parameter equals
        value is an int
    (else is optional and takes more steps to account for)
    else
        value is not valid

    return the int value
Then you need a function to determine the winner of the game

Code: Select all

define function that take a string as a parameter (it will not return anything just print the result)

    player var = function to convert string to int
    computer var = a random number in the range 0 to 5 (use the random moduel to make this part work)
    diff = get the difference of the 2 numbers modulus  the number of choices
    print the player and computer choices (use the functions to get what you need)
    # logic
    if the diff is 0 it's a tie (print the result)
    elif the diff is 1 the player wins (print the result)
    elif the diff is 2 the player wins (print the result)
    else the computer wins (print the result)
Lastly call the function that determines the winner of the game 5 different times by random or by choice
example

Code: Select all

function("rock")
function("Spock")
function("paper")
function("lizard")
function("scissors")
or

Code: Select all

for a in range(5):
  function(random.choice(["rock", "Spock", "paper", "lizard" ,"scissors"])
One step at a time.
Make the first 2 functions work. They work independent of anything else.
The third uses the first 2 so it will work after the first 2 work.

D9M2W
Posts: 37
Joined: Tue Dec 30, 2014 5:34 pm
Location: West Midlands, UK

Re: Struggling a bit with Python

Mon May 16, 2016 8:38 am

Morning

I did do it in the end, the first 2 functions were pretty easy, but I have to say I really did seek help with the second, because I just couldn't figure out how to slap all the steps together =/

Going to plod along though - and also getting quite through the codecademy course now as well, really hoping it starts to click at some point though!

I think I need some super easy Raspberry Pi projects to do as well

D9M2W
Posts: 37
Joined: Tue Dec 30, 2014 5:34 pm
Location: West Midlands, UK

Re: Struggling a bit with Python

Mon May 16, 2016 8:56 am

ghans wrote:Does this article resonate with you ? It certainly did for
me. Some advice is also offered.

https://www.vikingcodeschool.com/posts/ ... -damn-hard


ghans
Thanks for that link, it most certainly does resonate!

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

Re: Struggling a bit with Python

Mon May 16, 2016 8:56 am

D9M2W wrote:Morning

I did do it in the end, the first 2 functions were pretty easy, but I have to say I really did seek help with the second, because I just couldn't figure out how to slap all the steps together =/

Going to plod along though - and also getting quite through the codecademy course now as well, really hoping it starts to click at some point though!

I think I need some super easy Raspberry Pi projects to do as well
Don't worry about the RPI, python is python. RPI is hardware. One thing at a time, python first then hardware.

scotty101
Posts: 3750
Joined: Fri Jun 08, 2012 6:03 pm

Re: Struggling a bit with Python

Mon May 16, 2016 10:54 am

Rock Paper Scissor Lizard Spock

Code: Select all

from Tkinter import *
from ttk import *
import random
import sys

new_options = ["Rock","Paper","Scissors","Lizard","Spock"]

rules = [("","",""),
    ("Scissors","Paper","cut"),
("Paper","Rock","covers"),
("Rock","Lizard","crushes"),
("Lizard","Spock","poisons"),
("Spock","Scissors","smashes"),
("Scissors","Lizard","decapitate"),
("Lizard","Paper","eats"),
("Paper","Spock","disproves"),
("Spock","Rock","vaporizes"),
("Rock","Scissors","crushes")]

def computerRandom():
    options = ["Rock","Paper","Scissors","Lizard","Spock"]
    randomChoice = random.randint(0,len(options)-1)
    computer_choice.set(options[randomChoice]) ##added into the program
    return options[randomChoice]

def comparison(humanChoice, computerChoice):
    global rules
    
    win = 0
    if humanChoice == computerChoice:
        win = 0
    else:
        ruleIdx = 0
        for rule in rules:
            if (humanChoice == rule[0]) and (computerChoice == rule[1]):
                win = ruleIdx
            elif (humanChoice == rule[1]) and (computerChoice == rule[0]):
                win = -(ruleIdx)
            else:
                pass
            ruleIdx += 1
        
    print humanChoice, computerChoice, win
    return win

def play():

    
    humanChoice = player_choice.get() ##Modified this line

    computerChoice =  computerRandom()


    
   
        
    result = comparison(humanChoice, computerChoice)

    if result == 0:
        text = "Its a draw"
    elif result < 0:
        text = "%s %s %s - You Lose" % (rules[-result][0],rules[-result][2],rules[-result][1])
    elif result > 0:
        text = "%s %s %s - You Win" % (rules[result][0],rules[result][2],rules[result][1])
    result_set.set(text)

##Setup of main GUI
root = Tk()
root.title ('Rock Paper Scissors')

mainframe = Frame(root, padding = '3 3 12 12')
mainframe.grid(column=0, row = 0, sticky=(N,W,E,S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0,weight=1)

##Variables
player_choice = StringVar()
computer_choice = StringVar()
result_set = StringVar()

##Layout of GUI
##Player
Label(mainframe, text='Player').grid(column=1, row = 1, sticky = W)
Radiobutton(mainframe, text ='Rock', variable = player_choice, value = 'Rock').grid(column=1, row=2, sticky=W)
Radiobutton(mainframe, text ='Paper', variable = player_choice, value = 'Paper').grid(column=1, row=3, sticky=W)
Radiobutton(mainframe, text ='Scissors', variable = player_choice, value = 'Scissors').grid(column=1, row=4, sticky=W)
Radiobutton(mainframe, text ='Lizard', variable = player_choice, value = 'Lizard').grid(column=1, row=5, sticky=W)
Radiobutton(mainframe, text ='Spock', variable = player_choice, value = 'Spock').grid(column=1, row=6, sticky=W)

##Computer
Label(mainframe, text='Computer').grid(column=3, row = 1, sticky = W)
Radiobutton(mainframe, text ='Rock', variable = computer_choice, value = 'Rock').grid(column=3, row=2, sticky=W)
Radiobutton(mainframe, text ='Paper', variable = computer_choice, value = 'Paper').grid(column=3, row=3, sticky=W)
Radiobutton(mainframe, text ='Scissors', variable = computer_choice, value = 'Scissors').grid(column=3, row=4, sticky=W)
Radiobutton(mainframe, text ='Lizard', variable = computer_choice, value = 'Lizard').grid(column=3, row=5, sticky=W)
Radiobutton(mainframe, text ='Spock', variable = computer_choice, value = 'Spock').grid(column=3, row=6, sticky=W)

##Play
Button(mainframe, text="Play", command = play).grid(column = 2, row = 2, sticky = W)

##Result
Label(mainframe, textvariable = result_set).grid(column = 1, row = 7, sticky =W, columnspan = 2)

root.mainloop()


Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

D9M2W
Posts: 37
Joined: Tue Dec 30, 2014 5:34 pm
Location: West Midlands, UK

Re: Struggling a bit with Python

Mon May 16, 2016 5:51 pm

scotty101 wrote:Rock Paper Scissor Lizard Spock

Code: Select all

from Tkinter import *
from ttk import *
import random
import sys

new_options = ["Rock","Paper","Scissors","Lizard","Spock"]

rules = [("","",""),
    ("Scissors","Paper","cut"),
("Paper","Rock","covers"),
("Rock","Lizard","crushes"),
("Lizard","Spock","poisons"),
("Spock","Scissors","smashes"),
("Scissors","Lizard","decapitate"),
("Lizard","Paper","eats"),
("Paper","Spock","disproves"),
("Spock","Rock","vaporizes"),
("Rock","Scissors","crushes")]

def computerRandom():
    options = ["Rock","Paper","Scissors","Lizard","Spock"]
    randomChoice = random.randint(0,len(options)-1)
    computer_choice.set(options[randomChoice]) ##added into the program
    return options[randomChoice]

def comparison(humanChoice, computerChoice):
    global rules
    
    win = 0
    if humanChoice == computerChoice:
        win = 0
    else:
        ruleIdx = 0
        for rule in rules:
            if (humanChoice == rule[0]) and (computerChoice == rule[1]):
                win = ruleIdx
            elif (humanChoice == rule[1]) and (computerChoice == rule[0]):
                win = -(ruleIdx)
            else:
                pass
            ruleIdx += 1
        
    print humanChoice, computerChoice, win
    return win

def play():

    
    humanChoice = player_choice.get() ##Modified this line

    computerChoice =  computerRandom()


    
   
        
    result = comparison(humanChoice, computerChoice)

    if result == 0:
        text = "Its a draw"
    elif result < 0:
        text = "%s %s %s - You Lose" % (rules[-result][0],rules[-result][2],rules[-result][1])
    elif result > 0:
        text = "%s %s %s - You Win" % (rules[result][0],rules[result][2],rules[result][1])
    result_set.set(text)

##Setup of main GUI
root = Tk()
root.title ('Rock Paper Scissors')

mainframe = Frame(root, padding = '3 3 12 12')
mainframe.grid(column=0, row = 0, sticky=(N,W,E,S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0,weight=1)

##Variables
player_choice = StringVar()
computer_choice = StringVar()
result_set = StringVar()

##Layout of GUI
##Player
Label(mainframe, text='Player').grid(column=1, row = 1, sticky = W)
Radiobutton(mainframe, text ='Rock', variable = player_choice, value = 'Rock').grid(column=1, row=2, sticky=W)
Radiobutton(mainframe, text ='Paper', variable = player_choice, value = 'Paper').grid(column=1, row=3, sticky=W)
Radiobutton(mainframe, text ='Scissors', variable = player_choice, value = 'Scissors').grid(column=1, row=4, sticky=W)
Radiobutton(mainframe, text ='Lizard', variable = player_choice, value = 'Lizard').grid(column=1, row=5, sticky=W)
Radiobutton(mainframe, text ='Spock', variable = player_choice, value = 'Spock').grid(column=1, row=6, sticky=W)

##Computer
Label(mainframe, text='Computer').grid(column=3, row = 1, sticky = W)
Radiobutton(mainframe, text ='Rock', variable = computer_choice, value = 'Rock').grid(column=3, row=2, sticky=W)
Radiobutton(mainframe, text ='Paper', variable = computer_choice, value = 'Paper').grid(column=3, row=3, sticky=W)
Radiobutton(mainframe, text ='Scissors', variable = computer_choice, value = 'Scissors').grid(column=3, row=4, sticky=W)
Radiobutton(mainframe, text ='Lizard', variable = computer_choice, value = 'Lizard').grid(column=3, row=5, sticky=W)
Radiobutton(mainframe, text ='Spock', variable = computer_choice, value = 'Spock').grid(column=3, row=6, sticky=W)

##Play
Button(mainframe, text="Play", command = play).grid(column = 2, row = 2, sticky = W)

##Result
Label(mainframe, textvariable = result_set).grid(column = 1, row = 7, sticky =W, columnspan = 2)

root.mainloop()


Thanks mate! :)

D9M2W
Posts: 37
Joined: Tue Dec 30, 2014 5:34 pm
Location: West Midlands, UK

Re: Struggling a bit with Python

Mon May 16, 2016 5:52 pm

tom.slick wrote:
D9M2W wrote:Morning

I did do it in the end, the first 2 functions were pretty easy, but I have to say I really did seek help with the second, because I just couldn't figure out how to slap all the steps together =/

Going to plod along though - and also getting quite through the codecademy course now as well, really hoping it starts to click at some point though!

I think I need some super easy Raspberry Pi projects to do as well
Don't worry about the RPI, python is python. RPI is hardware. One thing at a time, python first then hardware.
Yeah you're right to be honest, probably best to not bite off more than can be chewed.

Just kind of feeling a bit stupid to be honest!

MarkTF
Posts: 301
Joined: Tue Mar 03, 2015 4:59 pm

Re: Struggling a bit with Python

Tue May 17, 2016 11:27 am

Just kind of feeling a bit stupid to be honest!
One of my sisters (in grad school at the time) used to say, "The more I learn, the dumber I get."

Shorthand for the realization that the body of human knowledge is vast and mastering even a narrow portion of it can be difficult.

D9M2W
Posts: 37
Joined: Tue Dec 30, 2014 5:34 pm
Location: West Midlands, UK

Re: Struggling a bit with Python

Tue May 17, 2016 12:00 pm

MarkTF wrote:
Just kind of feeling a bit stupid to be honest!
One of my sisters (in grad school at the time) used to say, "The more I learn, the dumber I get."

Shorthand for the realization that the body of human knowledge is vast and mastering even a narrow portion of it can be difficult.
Wise words, because I am certainly feeling it myself at the moment.
Which is annoying as my career path is that of a developer, and my degree will be starting in October in software development.
It sounds silly but it is just worrying me a little bit! Determined not to fail as it isn't in my nature, but feeling stupid isn't helping here sadly.

User avatar
Cancelor
Posts: 757
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Struggling a bit with Python

Tue May 17, 2016 12:36 pm

Reminds me of this one! ...

The more you study, the more you know. The more you know, the more you forget. The more you forget, the less you know. So why study?
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

D9M2W
Posts: 37
Joined: Tue Dec 30, 2014 5:34 pm
Location: West Midlands, UK

Re: Struggling a bit with Python

Tue May 17, 2016 2:55 pm

Cancelor wrote:Reminds me of this one! ...

The more you study, the more you know. The more you know, the more you forget. The more you forget, the less you know. So why study?
It's a bit of an evil circle isn't it!

User avatar
Cancelor
Posts: 757
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Struggling a bit with Python

Tue May 17, 2016 4:36 pm

Only to a cynic :lol: You will only forget a fraction of what you learn. :ugeek:
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

D9M2W
Posts: 37
Joined: Tue Dec 30, 2014 5:34 pm
Location: West Midlands, UK

Re: Struggling a bit with Python

Tue May 17, 2016 5:45 pm

Cancelor wrote:cynic
TBH you me summed up ;)

D9M2W
Posts: 37
Joined: Tue Dec 30, 2014 5:34 pm
Location: West Midlands, UK

Re: Struggling a bit with Python

Tue May 24, 2016 10:43 am

So little bit of an update - I actually signed up to one month of Codecademy Pro, as they have project and quizs etc plus an 8 hour+ project after the course. Looks pretty good so far, so going through this now, probably coming up to half way through now :)

User avatar
buja
Posts: 507
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: Struggling a bit with Python

Sun Jun 19, 2016 9:29 am

I took the (free) Python course on Codecadamy. I think it's very good. Unlike other on-line courses I have seen, this one has no boring videos, but makes you do stuff in an interactive way.

The screen on the left contains some explanation and a task. The middle screen is an editor, usually with some lines of code you need to change or add some code to (you don't need to type entire programs). The right hand screen is the output window. You run your code and the "course engine" compares the result with its expectations.
If they match you can go on to the next step. If not you must adjust your code (but if you want you can go to the next step manually).

I found it sometimes useful to copy the code to my own computer and IDLE to experiment a bit more.

Return to “Python”