[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Beginner Python
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 66
Thread images: 6
File: Not Working.png (21 KB, 943x557) Image search: [Google]
Not Working.png
21 KB, 943x557
Alright /g/ I'm attempting to learn python,
I decided to try and write something really easy but i'm having an issue getting it to work.
Can any of you kind anons help me?

>suppose to enter your name and then the next text will say your name depending on what you filled in
>pic related; What I have so far
>>
File: Issue.png (10 KB, 676x344) Image search: [Google]
Issue.png
10 KB, 676x344
Here's my dilemma
>>
>>51768957
I'm a python scrub too, but from what I can see you're using the old string formatting method (the %s).

Have a look here:
https://pyformat.info/#simple
>>
File: Screenshot_1.png (35 KB, 954x658) Image search: [Google]
Screenshot_1.png
35 KB, 954x658
>>51769019
>>51769019
Shuold it look something like this?
>>
File: Screenshot_2.png (7 KB, 676x445) Image search: [Google]
Screenshot_2.png
7 KB, 676x445
>>51769042
I must be doing something wrong?
>>
>>51769042
No, like this,
("Well {} do you have business with the king?" .format(name))
>>
File: Screenshot_3.png (9 KB, 678x450) Image search: [Google]
Screenshot_3.png
9 KB, 678x450
>>51769074
That worked thanks anon but now another issue has come up with the "if" command
got any ideas on that?
>>
>>51769106
== "Yes"

instead of == Yes
>>
>>51769178
I actually figured that out right before this refreshed also one last thing anon how do i write my code in the box for text on /g/? I don't come here often
>>
>>51769195
Put it inside [ code] [ /code] without the spaces between [ and code
>>
>>51769195
Code tags \ and \
>>
>>51769214
fug
>>
>>51769205
 if King_Business == "Yes": 
>>
>>51769214
wat
>>
>>51768957
>state thou's business
It's thine you illiterate
>>
>>51769227
On to the next part of my python quest
I want to add if you say "quest" the king will give you a sword how would i add that onto
 print raw_input("State thine\'s business!") 

I was thinking maybe something like
 if King_Business == "Yes":
print raw_input("State thine\'s business!")
if Business == "quest":
print = ("here {} take this sword!" .format(name))
>>
>>51768957
buddy, if you need this much help learning then you probably aren't going to make it
>>
>>51769298
Whats wrong with asking for help? i just started learning it an hour ago...
>>
>>51769289
have_sword = False
if King_Business == "Yes":
print raw_input("State thine business!")
if Business == "quest":
print = ("here {} take this sword!" .format(name))
have_sword = True

Something like this?
Also fixed thine
>>
Instead of using several if statements you should use elif or else

If this:
Then this
Elif this:
Then that
Else:
Comedy option
>>
>>51769317
Do I need to define "bisuness as a string first or could i just
 have_sword = False
if King_Business == "Yes":
print raw_input("State thine business!")
if scr(Business) == "quest":
print = ("here {} take this sword!" .format(name))
have_sword = True
>>
>>51769306
When you ask for help with the most basic possible stuff, as in, you're learning syntax, it means you lack a good resource to learn the fundamentals of the language. You need one for when we're not here to hold your hand.

When you ask for help with every single litte bug, you won't fully understand how to debug efficiently alone and you may not even understand how the solution we give fixes your code. As a result, you will be at a complete loss for handling the larger bugs that we do not have the time to help you with.

Trust me, buy/torrent a textbook and learn the language that way. The codacademy-type of websites are awful; avoid them as they will just hold your hand and have you jump through hoops without developing your understanding.
>>
>>51769317
That code looks terrible, won't that give you an error? Look at the documentation for the if method
>>
>>51769331
Whats the difference exactly?
just trying to learn
>>51769340
Yeah sorry if it looks like shit i'm only an hour into learning
>>51769339
and alright i'll torrent a book i was using code academy ironically
what book would you recommend
and also i did do research on my issues but for some reason it makes sense when i know it's coming fro another person
>>
>>51769306
Asking for help is great, but remember that we are mostly condescending assholes that pretend to never have struggled to learn this. If you want to learn python there is a excellent course on udacity. Com called cs101 i think. The courseware is available free of charge if you don't want to pay for the degree. And it is excellent, most likely the best way to learn programming. After that course you will be able to do most stuff with it.


Programming look strange for a while, then suddenly one day you just 'get it' and stuff you don't know are easy to pick up.
>>
>>51769349
I don't know any good books on Python, hell I don't even know Python. Maybe some other anon can recommend a text.

If you did things MY way, you would start with the more useful and difficult C++. (This is, of course, under the assumption that you're trying to become a programmer.) For that language, I recommend Absolute C++ by Walter Savitch. If you're just coding up stuff in Python for shits and giggles, probably do this >>51769380.
>>
>>51769380
Thanks Anon I'm not gonna try and pretend i'm not retarded when it comes to this but i can say an hour ago I didn't know what a string a boolean or a variable was haha :P
>>
>>51769396
I wan't to be a Computer programmer I'm a network engineer at the moment
>>
>>51769410
Learn something like C++ first then. It's more difficult, but ultimately the experience will be more beneficial. If you want, I suppose you could learn both at the same time - I'm not sure how difficult that would be though.
>>
>>51769410
>>51769426
Alright Anon I'll look into learning both might hang around /g/ more and learn more shit and maybe get in on some source projects
>>
>>51769349
The difference between elif and else in a if statement and two ifs is that you get all cases. For example you have just yes and no. What if the value is "horse" then neither the if statements catches that.

If you have code like this:
> if this == that:
> then this
> elif this == fuck:
> then nigger
>else:
> then whore

Then it will catch all the instances where the variable 'this' is neither 'that' or 'nigger'
>>
>>51769448
so basically it means if someone says the keyword it will get the "true" response but if it's anything else it would just fill in the "else" ??
>>
>>51769401
Python is a great language to start with. In just one month you can do stuff that would have taken you a year to learn how to do on just C as your first language. And when you know python then other languages are easier to pick up. There is a lot of detail you don't have to deal with when programming in python like declaring what data type to use and that sort of thing. That can be useful, but will likely just get in the way when you are trying to learn basics. A good python book is "learn python the hard way" (which is the easy way)
>>
>>51768957
it's: state thine business, faggot
>>
>>51769448
 elif King_Business == "Yes":
print raw_input("State thine\'s business!")
else:
print("don't waste the king\'s time!")


Like that?
>>
>>51769493
Yes, and if you want several keywords then use "elif " because then you avoid lots of extra lines of code and it looks better
>>
>>51769512
I personally think it's easier to go from hard (C) to easy (python) instead of the other way around, and "a year to learn" is a bit of an exaggeration, but whatever floats your boat.
>>
File: Screenshot_4.png (2 KB, 339x64) Image search: [Google]
Screenshot_4.png
2 KB, 339x64
>>51769530
Could you give me an example of the sorts? I'm getting this after putting in "elif"
>>
>>51769525
The first one must always be "if", elif (or else if) is used just like in English when you have more options
>>
>>51769540
Like this perhaps?
 if King_Business == "Yes":
print raw_input("State thine's business!")
elif King_Business == "Quest":
print ("why yes {} I do happen to have a quest!" .format (name))
else:
print("don't waste the king's time!")
[/code}
>>
>>51769537
Probably, but it takes a lot more time to learn, look at a C book and look at the last chapters. There you will learn stuff that is covered in chapter 2 of most python books. It's much faster to learn and it's more motivating when you are able to wield some power
>>
>>51769566
Yes, that looks correct, but be careful with whitespace in python. That looks like it might give you a error ( your 'if' has a space in front of it)
>>
>>51769599
I just ran it and it ran OK :D
>>
I have one last question for now Anons
How would i make it to where someone could examine and item by using the command in game "examine" without having to define "examine differently for each item basically making exam a universal command to print a definition about an item?
>>
>>51769646
well what you could do is make an object that contains a label and a string

then make a vector full of items

then make a function where someone types examine and the name of the item it searches through your vector for an item that matches the one you're looking for, and prints out the string associated with said weapon
>>
>>51769646
Just make "examine" a function or a class. (that is the short answer) but you have a little more to learn before we get to that. You will learn about those things in the end of the udacity course
>>
>>51769666
Thats exactly how if only i knew what a vector was haha... can i get an example?
>>
>>51769682
A vector might be a list [1,2,3, "piss" ] or i guess a dictionary {1:"death", 45:76, "mongo":" faggot}, or if you use a library like scypy or matplotlib then it might actually refer to a actual mathematical vector.
>>
>>51769682
I'm not familiar with python at all, and it is slightly complicated (not super hard, but still a bit out of your scope)

Like >>51769680 said, I would recommend just practice and reading a bit more first.

Conceptually though, a vector (at least in C++) is essentially a stack of things. Like you could have an int vector, and you can basically have a pile of integers.

Say we had a vector
vector<int> numbers;

//we could add numbers to our vector called numbers

numbers.push_back(1); // numbers contains 1
numbers.push_back(2); // numbers contains 1, 2
numbers.push_back(3); // numbers contains 1, 2, 3

//vectors work as first in last out, meaning the first number you add will be the last number you get out

numbers.pop_back(); // numbers contains 1, 2

//you can also declare a vector of objects, like in your case, items (though you would have to make an item object)
vector<Items> inventory;


If you really want to skip ahead, you're going to have to look up lists, objects, and classes. Chances are you're going to just have to do lessons in order to get this.

[spoiler]Hopefully this posts properly and I don't get bullied[/spoiler]
>>
Would it be something like this?

 
Examine = from Item_List
if Item_Id_found == "yes":
print Definition from Item_list
else:
print "No Item Under that name was found"

thats the only way as of now i could imagine it would be
>>
>>51769762
I really don't know python syntax at all, but that would be almost pseudo-code for the function find the item.

I was thinking more like this, assuming your code ended up working on a basis of [command] [variables]
//IE Examine Sword
//function call would be something like
Examine(Sword);

//which would call a function doing something like
Examine (string ItemName)
{
bool itemFound = false; // A boolean variable that is false if the item isn't found
for (int i = 0; i < inventory.size(); i++) { // A for loop that iterates through your vector of inventory items
if (inventory[i].getItemLabel() == ItemName){ // checks to see if the item at i in your inventory is the item you're looking for
itemFound = true; // if it is, itemFound is marked as true
cout << inventory[i].getItemDescription(); // prints out the item's message
}
if (itemFound == false){ // itemFound should only be false if the item was not found
cout << "No Item Under that name was found" // if it isn't found, tell the user
}
}


this is all C++ [spoiler]and probably wrong[/spoiler] but it might help?
>>
>>51769859
are spoilers disabled on /g/?
>>
>>51769859
It's starting to help i'm trying to convert it this is what I've gotten so far
#attempting to pull definitions from a list with this so I don't have to add individual strings for each item
Examine = from Item_List
if Item_Id_found == "yes":
print Definition from Item_list
else:
print "No Item Under that name was found"

class Item_List:


def function(Examine):
print 1, 2, 3,
Kings_Sword = "The Finest sword you have ever seen! It seems to be glowing with some sort of magic..."

just have to make the class with all the item i'd's i'm assuming then it can pull from that list and execute hopefully
>>
>>51769876
I honestly have no idea how python works, so maybe? I'd have to look it up but I'm too tired to do so and I'm supposed to be working on a lab report, but conceptually you're going about it the right way though you might have to rework your code to get all this fun stuff to work.
>>
>>51769901
Alright thanks man!
Appreciate everyone in this thread helping me out i'm gonna do some more research on object and classes and try to get this working!
>>
>>51769911
I'd really recommend just reading about python in order, and when working on your program try to conceptualize it on paper first, then convert that into code.

Otherwise you might end up writing some initial code, then trying to build the rest of your code around what you first made.

It really helps when you plan out how you're going to do everything from the beginning.

Like one thing I imagine being problematic is how are you going to read different commands? Your very first picture is fine since its just a few if statements, but what about once you're done with the dialogue? How are you going to handle the commands for going in a direction (assuming this is going to be like your standard text adventure) or examining items?
>>
This is the very very basic stuff in python. I recommend getting a book... heck even a youtube tutorial would help you a lot.

Trust me, you won't make it far if you give up and ask for help on image boards
>>
>>51769941
Well I just wanted to get a grasp of it so i have initiative to stay with it and I already created dialogue and that's cool as shit otherwise i might have given up but i'm pretty hell bent on it now this was more or less self encouragement I'm just gonna take baby steps and work my way up til i can code this shit with no help at all and i can be helping others the way you all helped me today
>>
>>51769876
Oh shit, look at that code. At least you are trying out different stuff. You manage to define a function almost correctly although you should not call it function. A examine function should look something like this :

> def examine(variable-to-be-examined) :
> return result-from-examination
>>
>>51769980
Like I said though, make sure to write out what you're planning before you code, it will help a lot. Like right now your dialogue with the king is fairly binary, you either answer yes or no. With boolean variables you could set flags to show certain things have been said, allowing more options.
>>
>>51769911
I think you are jumping a bit ahead, it might confuse you. Do this course

https://www.udacity.com/course/intro-to-computer-science--cs101

For free by pressing the blue "access course materials button" it's worth it as fuck
>>
>>51770017
Will do
>>51770000
also nice quads, yeah i was just guessing with all that code hoping it was something along those lines :P
>>
>>51769980
>>51770025
Also like another anons keep stressing, read tutorials in order. It can be exciting to try out the new stuff and get your game up and running ASAP, but if you skip around you'll probably end up fucking yourself.
>>
>>51770032
Yeah I'm pretty excited about all this i keep jumping around with new ideas that keep popping in my head i go from wanting an item list to wanting to define combat and damage so i should probably just learn the language first
>>
>>51770025
Good luck, I learned tons of python when I did a python based linear algebra course on coursera.org as well, after you finished udacity course there are tons of courses on coursera that involves python knowledge. Some of them are directly game development related. Python is not the best language to make games in, but I believe it is easier to pick up skills in it that you can use in other languages. The python language is pretty straightforward and won't get in your way too much. Also when you come to it, try to REALLY learn 'comprehensions' in python. It is a great skill to know. Suddenly many lines of code can be written in one line
>>
>>51770044
Its alright to have ideas, but just write them down for now and at most try to write it out in pseudo-code. Write it out kinda using your standards for coding, but in normal language kinda like
//examining an item

examine(item)
for ( 0 to number of items ) {
if (item found)
print out item description
}
if (item isnt found)
no item found
Thread replies: 66
Thread images: 6

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.