I'm a relative beginner in python and I'm having some trouble with a code I'm creating. | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

I'm a relative beginner in python and I'm having some trouble with a code I'm creating.

I am trying to code a sequence where the user finds an item. and the system saves it def user_resp(): user_resp == str(raw_input("")) print ("you found a sword!") print ("pick up the item?") user_resp = str(raw_input("")) if user_resp == str("yes"): print ("you picked up the sword!") item == sword else: print ("you discarded the item.") I'm not sure how to define item so that I can put sword or other items into it so it can be called later such as in a fight event.

24th Oct 2017, 5:32 PM
Stef
Stef - avatar
5 Antworten
+ 9
I was thinking about something last this: https://code.sololearn.com/c9WUcF9bY92k/?ref=app
24th Oct 2017, 7:08 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 7
First of all, if you plan on testing your code in SoloLearn, it must be written in Python3, so with input() instead of raw_input() Normally, you could create a new class called Weapon() and call instances of it like: longsword, broadsword, short sword, axe and all that. Each could have an attack, defense and to-hit attribute, which would vary between the weapons. You already know something about object-oriented programming?
24th Oct 2017, 5:41 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 7
Yes, just wrote it although it's heavily borrowed from my Animal park code I wrote the other day. This is the power of OOP - you create a class to suit your needs and call objects (instances of this class) when they are needed. I would imagine you have a Player class which has, apart from traditional set of attributes like Strength, Power, Endurance, Mana and alike, also a PrimaryWeapon which can assume values of a Weapon type. You then just call its .attack() method and print out a respective message (while dealing some decent damage to whatever you are hacking ;)
24th Oct 2017, 7:28 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
actually I'm using an app called QPython so it still compiles. and no I don't have any knowledge about object oriented programming. could you show me an example of what that code would look like?
24th Oct 2017, 6:07 PM
Stef
Stef - avatar
0
XD that's awesome! did u just make that?? I've got to say I'm really impressed! and that code definitely looks like what I'm trying to do. i haven't tried compiling your code yet. but it looks right.
24th Oct 2017, 7:16 PM
Stef
Stef - avatar