26

True. What makes .NET so nice is that you can mix managed and unmanaged code. Also, about pointers, pointers in C++ are not as scary as some people make them out to be. In fact, they're very easy to work with. You usually never have to do manual pointer cleanup because C++ usually handles that for you either upon program exit or upon scope exit. And with C++11, 14 and 17's new STL components, you now have std::smart_ptr and std::shared_ptr to use too, which both free themselves when no longer used.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.

Thumbs up +1

27

so one of my first many questions to come.
when creating strings, why cant I just use tripple quotes or ticks all the time? is there a disadvantige in doing so?

Thumbs up

28

You basically use triple quotes or triple ticks when you want to write multiline strings. This does not mean that string needs to be necessarily multiline, but writing single-line strings in tripple quotes makes no sense, although it's not an error.
Of course, instead of using multiline strings, you can just use escape sequences such as \n or \r, but it makes things less readable for code writer. For example:
"I'm a good boy.\nI'm the best guy in school.\nMy friends really like me."
Compare with this:
"""I'm a good boy.
I'm the best guy in school.
My friends really like me."""
You see, the second version as multiline string is much more readable.

Thumbs up

29

Let me add something:
I personally prefere to enclose strings in quotes, unless I have the following situation, for example, if I wanna have a message box that displays the user to click a button. I want the name of that button to be quoted, so I will enclose this in apostrophes like this:
'Please click "OK" to continue'
That way, I don't need to escape quotes to force displaying them inside a string by using escape sequence \" (backslash quote).

Thumbs up

30

I have a slight issue. when ever trying to type a script in a notepad and pasting it into the python the ctrl v doesn't work. anyway I can paste?

Thumbs up

31

you can do by pressing alt plus space, edit, paste

...

Thumbs up

32

I usually just save to a file with notepad, then run it in python by importing . IIRC, this doesn't work so well if you want to test changes, since I don't think python will update the module if you try to import it again, unless it failed to import via an error or something. Could have that backward, though.

Some of my games
Keep up to date by following @Jeqofire on twitter!
Ear Ninja?

Thumbs up

33

so now I'm getting into if statements, and just pasting the contense of the file doesn't work.
I want to get input from the user then use if statements to print out different messages depending on what was entered, however, when I paste it into the python prompt it just sits there. no errors, no anything. what am I missing here?

Thumbs up

34

When you start something like an if, while, class, or function, Python expects an indented block to follow. The editor therefore waits until you enter an unindented line, or leave a blank line, before it tries to execute the code.
If you're pasting an if with a block attached, I'm not really sure what's going on.

Some of my games
Keep up to date by following @Jeqofire on twitter!
Ear Ninja?

Thumbs up

35 (edited by magurp244 2018-02-25 03:05:18)

Another way to do it would be to save your script in notepad or similar text editor and run it from a command prompt, as python files are just plain text files with a renamed extension. For example: "python yourscript.txt", though to avoid confusion between file types you should probably save your scripts with a *.py extension instead of *.txt.

-BrushTone v1.3.1: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

Thumbs up

36

I'm not sure I understand? When do I paste in a blank line?  If you're talking  about me having code after the if statement, yes, I do.

Thumbs up

37

You need to press enter so that Python knows you're done with the indented part.

Some of my games
Keep up to date by following @Jeqofire on twitter!
Ear Ninja?

Thumbs up

38

for example you do this

think this is the interpreter
>>> input = raw_input("enter something")
>>> if input == 'hi':
...  print "hello"
...
this means after you write the code you'll have to press enter one more time and the interpreter will execute your code.
note: I'm using indentations with 1 space only

...

Thumbs up

39 (edited by amerikranian 2018-02-25 17:07:43)

so this is what i'm trying to paste into my python prompt. I think if someone can point out what am I doing wrong it would clear it up for me: (Note, I'll use a > symbol to represent a single space as AG forum doesn't like python's indentation.)
>user_input = input("enter something")
>if user_input == "hi"
>>print("hello")

I'm sorry for so many questions guys, I'm trying to understand this.

Thumbs up

40

you are missing the colons after the condition if
try putting this
>user_input = input("enter something")
>if user_input == "hi":
>>print("hello")

...

Thumbs up

41 (edited by amerikranian 2018-02-25 18:57:04)

alright: more problems. Yea! Who loves problems?
When I pasted in the following, the python prompt returned me a big fat nothing.
I will post the code below, using > again to represent a single space.
>user_input = input("enter something")
>if user_input == "hi":
>>print("hello") #this causes an error with the indentation
That script does nothing at all besides the error.

It also should be noted that I have tried something like this:
>user_input = input("enter something.")
>if user_input == "hi":
>print("hello") #the script returns another error about the indentation.

Finally, I have tried something like this:
>user_input = input("enter something")
>if user_input == "hi":
print("hello") #this is the only part that works. The input and the if statement get skipped.

Thumbs up

42

I am using a book called how to think like a computer scientist found at the following link
http://openbookproject.net/thinkcs/python/english3e/
If you go to chapter 3, you'll see that the book suggests you to enhance the turtle program by allowing the user to set the title of the window and such. Trouble is, the input statement doesn't work. I'll post the code below, or at least the important part.
import turtle
wn = turtle.Screen
title = input("enter something")
wn.title(title)

If I run this, it would make the title the
wn.title(title)
Line instead of asking for input. I tried getting input before initializing the screen but, no luck.

Thumbs up

43

Try saving it to a file, and run that. If the results are the same, something odd is happening (maybe spaces are showing up as the wrong character or something like that?). If it works from a file, then the interpreter is having trouble with pasting some things.

Some of my games
Keep up to date by following @Jeqofire on twitter!
Ear Ninja?

Thumbs up

44

This might be a bit silly, but how do you run a file from an interpreter? Do you just paste in the file path?

Thumbs up

45

From the Python interpreter, you import it. If the file is HelloWorld.py, you would type
>import HelloWorld
And that would do it. I'm not sure that there's an easy way to repeat this without restarting the interpreter, though, if you make changes afterward.

Some of my games
Keep up to date by following @Jeqofire on twitter!
Ear Ninja?

Thumbs up

46

Does the file have to be in a certain location?

Thumbs up

47

No. Anywhere will do.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.

Thumbs up

48 (edited by pauliyobo 2018-02-27 07:02:19)

or you can run it from the CMD open the command prompt in the path in which your file is, and run python filename.py
post 42:
This may not work, but you can try this
import turtle
I'll use the greater sighn for indentation

wn = turtle.screen
while 1:
>title=str(input("enter the screen title"))
>wn.title(title)
>break

if the script should keep asking you the title, remove the break statement.
REGARDS

...

Thumbs up

49

so, consider the following code: Note: indentation is a greater than sign:
def my_fun(a):
>a = a + 1
>x = a
>print(a)
>print(x)

x = 3
my_fun(x)

So I now have a question. If you run this, you will be printed a value of 4 4. Now, here's the fun part. If you type in x in the interpreter it will say, clearly, 3, but wait. I had a statement in the my_fun function that should have set x to 4, so why is it 3? Ferther more, if you type in a, the following will appear:
Traceback (most recent call last):                                             
  File "<stdin>", line 1, in <module>                                           
NameError: name 'a' is not defined                                             
but but but... I clearly set a to 4! So why, why in the world does the stupid thing reset and acts as though I have never defined a variable called a? After all, I did do it. So my question is how? How can I change the variable outside of a function instead of declaring a variable that lasts only till a function finishes running? Is it something to do with the return statement? I'd be really grateful for any help and would also appreciate if you provided a code snippid or some such.

Thumbs up

50 (edited by thggamer 2018-04-06 00:29:32)

In Python, when you run the "my_fun" function, the variables are created
inside the function.
When the function returns or when it ends, Python removes the
variables that are inside the function and restores the
variables that are declared outside it.
This is called a namescope (the function has a namescope which their
variables are declared, and the script has another).
When you tipe x in the interpreter, you are referencing the x of the
script namescope because the function has already ended.
Edit: to modify a variable inside a function, declare it as global so it
can be seen on the script namescope. See the example:
>>> def set_x_and_print_it():
...     global x
...     x = 3
...     print(x)
...
>>> set_x_and_print_it()
3
>>> x
3

Thumbs up