Small Python Program for combat in a RPG; help?

Post » Fri Aug 30, 2013 7:45 pm

In the interest of learning rather than copying, I think you should post what you think you'll need to do, then let the coders tell you why you're horribly misguided and wrong.

User avatar
Vickytoria Vasquez
 
Posts: 3456
Joined: Thu Aug 31, 2006 7:06 pm

Post » Fri Aug 30, 2013 5:31 pm

enemyAttack = randint(1, 10)userattack = randint(1, 10)print("your attack roll: " + userattack + " | " + "enemy attack roll: " + enemyAttack)
User avatar
Jimmie Allen
 
Posts: 3358
Joined: Sun Oct 14, 2007 6:39 am

Post » Fri Aug 30, 2013 4:06 pm

Is that how the string that pops up alongside it is not right up against this, as such;

"What a nice random string this is!"1

If so, I am sooo grateful to finally be rid of this nuisance. :P

User avatar
+++CAZZY
 
Posts: 3403
Joined: Wed Sep 13, 2006 1:04 pm

Post » Fri Aug 30, 2013 9:24 am

Oh, excuse me if I wasn't clear, that's what I meant! If you check the code it's already updated. ^_^

User avatar
Marquis deVille
 
Posts: 3409
Joined: Thu Jul 26, 2007 8:24 am

Post » Fri Aug 30, 2013 4:45 pm

How does the code look? I haven't tested it, but wouldn't that be ALL that is required???

See Post # 2 :smile:

Edit: OKAY GUYS, so I tested my code!! :happy:

Right off the bat, I have an error on line 12 (the first if statement), saying I have an unexpected indent.

Hmmmm? I'm looking it up online.

Edit: Okay, so I fixed the initial indentation error ---> on to the next error hahaha!

Hmm. I'm gonna place it here for all to see, although I'll be actively trying to fix it.

Traceback (most recent call last):

File "C:/Python33/kingarthurCombat.py", line 4, in

ownAttack = raw_input("Enter your own attack!")

NameError: name 'raw_input' is not defined

User avatar
christelle047
 
Posts: 3407
Joined: Mon Apr 09, 2007 12:50 pm

Post » Fri Aug 30, 2013 7:31 pm

Yeah, print displays exactly what you tell it to, so if you don't tell it to provide a gap you don't get one (and whatever is displayed after it [such as the input you're typing in] will be right next to it). If that makes sense :tongue:.

And int() is how you convert to integers, if you need a hint on that.

EDIT: I also suggest adding comments, because it's good practice and therefore a good habit to develop. Plus, it'll help you later and the people trying to help you.
User avatar
Ally Chimienti
 
Posts: 3409
Joined: Fri Jan 19, 2007 6:53 am

Post » Fri Aug 30, 2013 8:38 am

As soon as you convert it, you also need to exception handle it (like in my input validation code)


Misguided 2.7ers always go "hur dur, you're getting a literal" since input used to work that way, so I thought I'd clarify beforehand I was using the 3.x line.

You're still not validating user input

Multiple variables also are null due to never being defined. Other variables are never used (make sure to use the same variable properly throughout your code)

Post your code in a code block so we can see it properly, or upload the py file. otherwise judging indents is impossible.

Comments are useless and so are descriptive variable names. in interpreted languages like Python, all it does is cause file bloat. Save time and space, just use single-character variable names!


(the above was a joke and should not be taken seriously)
User avatar
Emma louise Wendelk
 
Posts: 3385
Joined: Sat Dec 09, 2006 9:31 pm

Post » Fri Aug 30, 2013 1:15 pm

So "above was designed for python3 where input has replaced raw_input" would have been more clarifying :wink:.

Yeah! Just call everything "i", "j" or "k"! Unless you're doing a loop or something.
User avatar
Dawn Farrell
 
Posts: 3522
Joined: Thu Aug 23, 2007 9:02 am

Post » Fri Aug 30, 2013 6:29 pm

Includes DEFRON

Yeah, I prefer python 3, BUT I keep reading it's not as complete as python 2.x??? Should I just go ahead and code in python 3.x, or for now use 2.x?

Is 3.x really as barren as some people on the internet say? I always take things with a grain of salt, but since I'm not experienced enough, I can't make a proper decision to use both, or just stick with 3.x.

User avatar
Facebook me
 
Posts: 3442
Joined: Wed Nov 08, 2006 8:05 am

Post » Fri Aug 30, 2013 5:05 pm

How do I validate user input? Perhaps I know, but don't understand how you're wording it (which is perhaps the simplest way).

Also, how do I post in a code block, and how do I upload it??

User avatar
Devils Cheek
 
Posts: 3561
Joined: Sun Aug 13, 2006 10:24 pm

Post » Fri Aug 30, 2013 8:54 pm

Not all libraries are compatible with 3.x yet. Though most that aren't are dead projects.

Basically there are a few considerations at this point:

1. If it's new code, you should use Python 3.x unless:

2. There's a library you need that isn't available in 3.x yet. In that case you should use Python 2.7.

3. If you're maintaining old code, then you may want to continue using 2.7, but should be looking/planning the migration to 3.x

Yeah, that'd probably have been better.


Woah, 3 variables? What is this, hippieville? Just use two variables and re-set them as necessary. You never need more than two variables and any more is just wasting RAM!

The code I posted earlier was how to validate user input to make sure it's an int inside a valid range.

useing code tags or uploading the file to an file hosting service like dropbox or a programming-oriented text pasting sites like http://pastebin.com/
User avatar
Pixie
 
Posts: 3430
Joined: Sat Oct 07, 2006 4:50 am

Post » Fri Aug 30, 2013 6:47 pm

Updated code ^_^

@DEFRON, thanks for the rundown of 2.x vs 3.x. I'll definitely be sticking more to 3.x now, so I'll go back and change raw_input ---> input for this change

Also, I'm reading up how to post in a code block, but it seems confusing (I would say, the posters are not writing in layman's terms).

User avatar
Dalton Greynolds
 
Posts: 3476
Joined: Thu Oct 18, 2007 5:12 pm

Post » Fri Aug 30, 2013 10:54 pm

print("Welcome!")print("Prepare yourself for combat!") ownAttack = raw_input(int("Enter your own attack!"))enemyDefense = raw_input(int("Enter your enemy's defense!"))equals = ownAttack - enemyDefense resultEnemyDefense = equals / random.randint(1, 10)  # Fixed DEFRON, I noticed right away something was wrong. Didn't mean for modulus :x results = int(results)   if result <= 2:  #Nice touch, Reneer, as to not have to waste time rewriting the same output for Result 1 or below    print("Parried, no damage.") elif result <= 4:    print("Light hit, half con loss (- armor)") elif result <= 7: # Edited from original format due to major redundancy     print("Hit, con loss (- armor)") elif result < 10:    print("Heavy hit, con loss (unmodified by armor)") else: # Fixed for optimal coding!    print("Critical hit, con = 3, if lower than 5: Con = 0")enemyAttack = raw_input(int("Enter your enemy's attack!"))ownDefense = raw_input(int("Enter your own defense!"))equals2 = enemyAttack - ownDefenseresultEnemyAttack = equals2 / random.randint(1, 10)results2 = int(results2)if result <= 2:      print("Parried, no damage.") elif result <= 4:    print("Light hit, half con loss (- armor)")elif result <= 7:     print("Hit, con loss (- armor)") elif result < 10:    print("Heavy hit, con loss (unmodified by armor)") else:     print("Critical hit, con = 3, if lower than 5: Con = 0")print("Your result: " + result)print("Enemy's result: " + result2)

Random post tinkering FTW!

User avatar
Vickey Martinez
 
Posts: 3455
Joined: Thu Apr 19, 2007 5:58 am

Post » Fri Aug 30, 2013 6:56 pm

> ownAttack = raw_input((int("Enter your own attack!"))

1. Invalid typecasting. Your code will be interpreted as trying to convert the string "Enter your Own Attack!" to an integer, which kicks an exception. You need to put it on the outside of the input and try/catch the exception (otherwise if the user types "ten" instead of the number 10, your code will have a temper tantrum)

Also, many of your variables are never used and others are never defined. I don't want to point them out directly to you, because you need to get into the habit of reading your own code.

User avatar
Josh Lozier
 
Posts: 3490
Joined: Tue Nov 27, 2007 5:20 pm

Post » Fri Aug 30, 2013 11:59 am

ownAttack = raw_input(int("Enter your own attack!"))enemyDefense = raw_input(int("Enter your enemy's defense!"))equals = ownAttack - enemyDefense resultEnemyDefense = equals / random.randint(1, 10

Is there something not being used here? I don't want to sound like a novice (which I most certainly am, but am actively working to change that!!), but it seems to me that everything is being used. For the subsequent section, the same is apparent to me.

Now HERE is where I become confused.

resultEnemyDefense = equals / random.randint(1, 10) # Look here results = int(results)   # And hereif result <= 2:  #Nice touch, Reneer, as to not have to waste time rewriting the same output for Result 1 or below    print("Parried, no damage.") elif result <= 4:    print("Light hit, half con loss (- armor)") elif result <= 7: # Edited from original format due to major redundancy     print("Hit, con loss (- armor)") elif result < 10:    print("Heavy hit, con loss (unmodified by armor)") else: # Fixed for optimal coding!    print("Critical hit, con = 3, if lower than 5: Con = 0")

Does my problem lie here?

User avatar
Lisa Robb
 
Posts: 3542
Joined: Mon Nov 27, 2006 9:13 pm

Post » Fri Aug 30, 2013 7:29 pm

ok, I'll point out just one to get you started.

>results = int(results)

What this says is treat results like an integer, and assign that integer value to results... except, what is results before this?

User avatar
Dean
 
Posts: 3438
Joined: Fri Jul 27, 2007 4:58 pm

Post » Fri Aug 30, 2013 9:20 am

Yeah. As in mathematics, whatever is in parenthesis gets done first, then passed on to the next layer of parenthesis (if any). Or to phrase it another way, what's in parenthesis is manipulated by what is outside the parenthesis.

So if you have c(b(a))), a happens, then b happens, then c.

User avatar
Jessica Nash
 
Posts: 3424
Joined: Tue Dec 19, 2006 10:18 pm

Post » Fri Aug 30, 2013 9:05 am

I think I understand it now.

resultsEnemyDefense = int(results)

I wasn't the one who made

= int(results)

It was recommended, and I went with it.

Now, I think I am realizing (and clearly overlooked it supremely), but they created a variable entitled differently than mine, didn't they?

so it really should be;

resultsEnemyDefense = int(resultsEnemyDefense)

I think so!!! Right????

User avatar
Ice Fire
 
Posts: 3394
Joined: Fri Nov 16, 2007 3:27 am

Post » Fri Aug 30, 2013 2:42 pm

Most likely what someone posted was sample code. You have to then figure out how sample code applies to your project and then integrate it.

but yes, that's right. The one thing, though, is that there's no reason to int it, when dividing an integer by an integer, you'll automatically get an integer.
User avatar
SWagg KId
 
Posts: 3488
Joined: Sat Nov 17, 2007 8:26 am

Post » Fri Aug 30, 2013 5:50 pm

ownAttack = input(int("Enter your own attack!"))enemyDefense = input(int("Enter your enemy's defense!"))

Hmm. I was returned with an error.

Traceback (most recent call last):

File "C:/Python33/kingarthurCombat.py", line 4, in

ownAttack = input(int("Enter your own attack!"))

ValueError: invalid literal for int() with base 10: 'Enter your own attack!'

Okay, so I clearly wrote these lines of code wrong (for now, I'll assume the subsequent line is also wrong).

I looked it up, so "invalid literal for int()" means that the product is not a base 10 based. My question is, how would they know this before I even entered anything in???

Perhaps I am reading over something in my code. I'm constantly checking ^_^

User avatar
Lily Evans
 
Posts: 3401
Joined: Thu Aug 31, 2006 11:10 am

Post » Fri Aug 30, 2013 10:29 pm

Both 1Samildanach and I pointed out why this is happening. Python reads your code like you want to treat the string "Enter your own attack!" as an Int.

Even after you fix it, though, what happens if a user types in "ten" or y or anything other than a number? This is why you need to validate user input.
User avatar
Symone Velez
 
Posts: 3434
Joined: Thu Sep 07, 2006 12:39 am

Post » Fri Aug 30, 2013 9:38 pm

I think I just made bounds of progress. Updated code is as follows, and can now be referred to on page 2.

print("Welcome!")print("Prepare yourself for combat!") ownAttack = input(int("Enter your own attack!"))enemyDefense = input(int("Enter your enemy's defense!"))equals = ownAttack - enemyDefense result1 = equals / random.randint(1, 10)  # Fixed DEFRON, I noticed right away something was wrong. Didn't mean for modulus :x if result1 <= 2:  #Nice touch, Reneer, as to not have to waste time rewriting the same output for Result 1 or below    print("Parried, no damage.") elif result1 <= 4:    print("Light hit, half con loss (- armor)") elif result <= 7: # Edited from original format due to major redundancy     print("Hit, con loss (- armor)") elif result1 < 10:    print("Heavy hit, con loss (unmodified by armor)") else: # Fixed for optimal coding!    print("Critical hit, con = 3, if lower than 5: Con = 0")enemyAttack = input(int("Enter your enemy's attack!"))ownDefense = input(int("Enter your own defense!"))equals2 = enemyAttack - ownDefenseresult2 = equals2 / random.randint(1, 10)if result2 <= 2:      print("Parried, no damage.") elif result2 <= 4:    print("Light hit, half con loss (- armor)")elif result2 <= 7:     print("Hit, con loss (- armor)") elif result2 < 10:    print("Heavy hit, con loss (unmodified by armor)") else:     print("Critical hit, con = 3, if lower than 5: Con = 0")print("Your result: " + result)print("Enemy's result: " + result2)
User avatar
Bird
 
Posts: 3492
Joined: Fri Nov 30, 2007 12:45 am

Post » Fri Aug 30, 2013 2:56 pm

The int() function takes either a numeral type (integer or floating point number) or a string which can be interpreted as an integer, and then returns an integer. For example, you can't pass a string like "Hello World" and expect to get a number out of it, because "Hello World" isn't a number.

User avatar
Tiff Clark
 
Posts: 3297
Joined: Wed Aug 09, 2006 2:23 am

Post » Fri Aug 30, 2013 1:43 pm

Working actively on solving the user input issue. I think I may have figured it out.

User avatar
The Time Car
 
Posts: 3435
Joined: Sat Oct 27, 2007 7:13 pm

Post » Fri Aug 30, 2013 7:53 pm

#Experimental code, not inserted in rough draft of final productownAttack = input(int("Enter your own attack!"))enemyDefense = input(int("Enter your enemy's defense!"))equals = ownAttack - enemyDefense result1 = equals / random.randint(1, 10)  # Fixed DEFRON, I noticed right away something was wrong. Didn't mean for modulus :xwhile result1 != int(1, 10) # Validating User Input    ownAttack = input(int("Enter your own attack!")    enemyDefense = input(int("Enter your enemy's defense!")    equals = ownAttack - enemyDefense    result1 = equals / random.randint(1, 10)

Intuition tells me this is wrong.

Edit: Maybe not wrong, but not optimal.

User avatar
josh evans
 
Posts: 3471
Joined: Mon Jun 04, 2007 1:37 am

PreviousNext

Return to Othor Games