Python Forum
What dose these commands mean?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What dose these commands mean?
#1
What dose these commands mean? Huh Variable, Import random, Def(Getwords, Return, Checklists)While, Print, Input, For n in range, List. From a confusinng Norwegian teenage Tongue Btw, here is the full code if you need the context.

import random

def getWords(list1):
    list=["Nils", "Gulvmatte", "Potet","Teddy", "Iskrem","Python","Kodeklubben","Hus","Fjell","Nisse","Bjørn","Elefant"]
    pos = random.randint(0, len(list)-1)
    list1.append(list[pos])
    return list1

def checkLists(list1, list2):
    return list1==list2
        
list1=[]
list2=[]
num=0
while(checkLists(list1,list2)):
    list1=getWords(list1)
    print(list1)
    input("Klikk enter når du er klar til å huske")
    for n in range(100):
        print()
    ans=input("Skriv inn ordene i riktig rekkefølge, skill dem med bindestrek -")
    list2=ans.split("-")
    num+=1
print("Bra, du klarte å huske "+str(num-1)+" ord på rad")
Reply
#2
Answering your questions would pretty much mean writing a basic Python tutorial :D
So for now, to avoid reinventing the wheel, I will use links to the excellent official Python resources. And if you will have further questions, feel free to ask!

Variable
A container of a specific data type, holding a value
Python data types

Import random
Import is a statement which is used to import/load another Python module into your program. In your case it imports random module.
Importing modules

Def(Getwords, Return, Checklists)
Definition of a function called "Def", which takes 3 input arguments when it is called.
Defining Python functions

While
A loop, that is repeated until the loop condition evaluates to False.
While statement

Print
A function with prints its arguments to the standard output.
Python print function

Input
A function which takes input from standard input, which is usually user typing into console
Input function

For n in range
For is a loop that repeats for a specific number of times. In your code, it is 100 times.
Python for loop

List
List is a data type, which can contain several pieces of data of any data type.
Python lists
Reply
#3
Hello Mads,

First, you should put your lines of code between the [python] and [/python] tags, so the result will appear as a program.

Second, if you want an explanation of a python keyword, use the following url: https://docs.python.org/3/search.html?q=import+random (replace the text in red by the keywords you want to learn about).
Reply
#4
Thx å lot ☺
Reply
#5
But umm, can you tell me what the different "words" do in this program?
Reply
#6
As @j.crater well explained, these different "words" are part of the python language and the syntax (the way the words are arranged to make something understandable) is also part of the python language. This is the same approach when you learn English words and grammar.

For example, the line 14 num=0 assigns to the variable num the value 0. Later in the program, you see on line 23 num+=1 which means take the value stored in num, add one to it, then assign the result to num.
Reply
#7
ok, thanks anyway
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020