Small Python Program for combat in a RPG; help?

Post » Fri Aug 30, 2013 3:57 pm

it's wrong.

FIrst, you still haven't fixed

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

You're still trying to treat "Enter your own attack!" as an integer, which you can't as it's not anything compatible with an Integer,

I know what you're trying to do, you're trying to typcast the input as an int, but that's not what you're doing. Look at 1Samildanach's last post to see what you're doing wrong.

Also

result1 != int

is not valid syntax at all. The correct way to determine a type is type(variable), but that's still not what you want to do. an int divided by an int is always an int, so it'd always be true.

Validating user input is something along the lines of what I posted here: http://www.gamesas.com/topic/1472064-small-python-program-for-combat-in-a-rpg-help/?p=22972835

it's not the exact code you need, since I'm not sure what exactly your rules for valid input are, but it should give you an idea. It would also need to be put in at the right spot and use the correct variable names for your code.
User avatar
Brιonα Renae
 
Posts: 3430
Joined: Mon Oct 22, 2007 3:10 am

Post » Fri Aug 30, 2013 10:46 pm

#Experimental code, not inserted in rough draft of final productownAttack = input("Enter your own attack!") #FIXEDenemyDefense = input("Enter your enemy's defense!")equals = int(ownAttack) - int(enemyDefense)#FIXED 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)

Okay, okay... I think I've got it!

Although feel free to burst the bubble if need be. Let me just say so far, thanks to EVERYONE who has helped me so far! This is active learning, not copy + paste, and I have learned A LOT in the last few hours. Honestly, it's this sort of experience I need, instead of walking through exercise tutorials, which also include babystepping you if need be. You guys are honest to the T, and I like it!

User avatar
James Wilson
 
Posts: 3457
Joined: Mon Nov 12, 2007 12:51 pm

Post » Fri Aug 30, 2013 9:17 pm

You're not validating any input.

I'm not sure what exactly will happen with your while loop, but I do know it's nothing you want.

Here's what I suggest you do:

1. delete your while loop entirely

2. Enter "ten" instead of "10" for your input.

3. See what happens.

You will then understand why you need to validate input.
User avatar
jeremey wisor
 
Posts: 3458
Joined: Mon Oct 22, 2007 5:30 pm

Post » Fri Aug 30, 2013 7:50 pm

@DEFRON, I've been trying to post this for the past few minutes, but the forums gave me some issues.

userinput >= 1 min = 1max = 10

It's a start... Hmmm

User avatar
Ann Church
 
Posts: 3450
Joined: Sat Jul 29, 2006 7:41 pm

Post » Fri Aug 30, 2013 11:36 pm

 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!'

BINGO!!!!

So I DID fix the int problem, and so I was finally able to enter INPUTS. And I see. Okay, right, so that's what I expected. I'm trying to recall how to ensure that what is entered is an int. But you said DEFRON not to use type?

User avatar
Sarah Knight
 
Posts: 3416
Joined: Mon Jun 19, 2006 5:02 am

Post » Fri Aug 30, 2013 10:50 am

ownAttack = input("Enter your own attack!") #FIXEDenemyDefense = input("Enter your enemy's defense!")equals = int(ownAttack) - int(enemyDefense)#FIXEDimport randomresult1 = equals / random.randint(1, 10)  # Fixed DEFRON, I noticed right away something was wrong. Didn't mean for modulus :xuserinput >= 1 min = 1max = 10

I did import random.

User avatar
Kay O'Hara
 
Posts: 3366
Joined: Sun Jan 14, 2007 8:04 pm

Post » Fri Aug 30, 2013 9:39 am

I don't know what your code looks like as of this moment, so I can't say what you're doing wrong.

I can say with 100% certainty* that I've given you everything you need to solve the problem of the user typing in "ten" instead of 10. It's just a matter of you figuring out how it works and how to incorporate it into your code.

*Actually, I think there is a small syntax error in my code, but no logical errors. Too lazy to test it though to see.
User avatar
Svenja Hedrich
 
Posts: 3496
Joined: Mon Apr 23, 2007 3:18 pm

Post » Fri Aug 30, 2013 12:34 pm

print("Welcome!")print("Prepare yourself for combat!") ownAttack = input("Enter your own attack!") #FIXEDenemyDefense = input("Enter your enemy's defense!")equals = int(ownAttack) - int(enemyDefense)#FIXEDimport randomresult1 = equals / random.randint(1, 10)  # Fixed DEFRON, I noticed right away something was wrong. Didn't mean for modulus :xuserinput >= 1 min = 1max = 10if 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)

Updated code ^_^

DEFRON, I'm going through all of the info and I'll figure it out soon enough. Thanks for all the input so far!!

User avatar
Jenna Fields
 
Posts: 3396
Joined: Mon Dec 11, 2006 11:36 am

Post » Fri Aug 30, 2013 10:43 am

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

Did you try that? Because it seems to me that that's what you want to be doing. The input() call should then accept the user input and be typecasted as an int before it's stored into ownAttack.

User avatar
Francesca
 
Posts: 3485
Joined: Thu Jun 22, 2006 5:26 pm

Post » Fri Aug 30, 2013 9:50 pm

while ownAttack and enemyDefense >= 1 min = 1max = 100

Experimental at the moment...

User avatar
Lil Miss
 
Posts: 3373
Joined: Thu Nov 23, 2006 12:57 pm

Post » Fri Aug 30, 2013 9:02 pm

Effectively, the OP's code already does that. He typecasts during the arithmetic instead of during input.

The problem the OP is facing is one of how to deal with the user typing a non-integer-compatible string. The OP has all the instructions on how to do it already, just needs to figure out how to implement it in his own code. As such I do ask you not give the OP the exact solution, but let him figure out how to implement it in his own code for himself using the information we've given him.
User avatar
Devin Sluis
 
Posts: 3389
Joined: Wed Oct 24, 2007 4:22 am

Post » Fri Aug 30, 2013 7:30 pm

That's my second DIRECT hint! I can't believe I couldn't see that...

I thought I was doing (a(b©)), but (a(b©)) was actually (b(a©)).

Thanks! :) I know I should have saw that, but I just didn't. Again, thank you.

User avatar
Alada Vaginah
 
Posts: 3368
Joined: Sun Jun 25, 2006 8:31 pm

Post » Fri Aug 30, 2013 5:34 pm

And I am grateful for this.

Again, thanks to everyone's input so far. :) I'm already twice the better programmer I was than when I started a few hours ago. Again, thank you all.

@DEFRON, still trying to figure it out.

User avatar
Emily Jeffs
 
Posts: 3335
Joined: Thu Nov 02, 2006 10:27 pm

Post » Fri Aug 30, 2013 8:08 pm

ownAttack = input("Enter your own attack!") #FIXEDenemyDefense = input("Enter your enemy's defense!")equals = int(ownAttack) - int(enemyDefense)#FIXEDimport randomresult1 = equals / random.randint(1, 10)  # Fixed DEFRON, I noticed right away something was wrong. Didn't mean for modulus :xwhile type(ownAttack.int) and type(enemyDefense.int)

Took a quick water break. Here we go!

Edit: Would this BEGIN to validate the user input?

Wait, have to brush up on how to use type... I know you said not to, but I am having trouble thinking of what else to use.

It's returning invalid syntax anyways.

I have to give it more thought.

User avatar
David John Hunter
 
Posts: 3376
Joined: Sun May 13, 2007 8:24 am

Post » Fri Aug 30, 2013 8:43 pm

Oh, okay. To be honest, this is the first time I've ever tried to debug any Python code at all, so I'm effectively making shots in the dark with my suggestions. :tongue:

You can let him try, but he's clearly fumbling about right now without a whole lot of direction. If you give him the solution now (or at least another part of it) then he might end up learning better from the example than if you just leave it up to him to figure out on his own.

Besides, this is just one exercise. It's alright if the absolute maximum potential for learning isn't met this one time. If he continues practicing with his programming (and he seems plenty eager to do so), there will be plenty more opportunities for him to learn what it is that he's doing later on.

User avatar
Lyndsey Bird
 
Posts: 3539
Joined: Sun Oct 22, 2006 2:57 am

Post » Fri Aug 30, 2013 11:25 am

No

Validating user input should take place at the same time you get user input. Please take a look at my example of validating user input.

http://www.gamesas.com/topic/1472064-small-python-program-for-combat-in-a-rpg-help/?p=22972835

Ask questions about the bits you don't understand. I was expecting you to ask a ton of questions about what's going on in there, because I KNOW there is code in there you've never seen before (since you've never heard of an array before, I know there are things you aren't familiar with).

That's where I suggest you start. Ask questions about that until you understand it. Please try to ask specific questions.

Edit: reposting relevant code for easier reference (and fixed two syntax errors):

userinput = 0min = 1max = 10while(not(userinput >= min and userinput <= max)):    try:        userinput = int(input("enter a number "))        if(not(userinput >= min and userinput <= max)):            print("number not in valid range, try again")    except ValueError:        print("you did not enter a number")
User avatar
NeverStopThe
 
Posts: 3405
Joined: Tue Mar 27, 2007 11:25 pm

Post » Fri Aug 30, 2013 9:29 pm

On this note, I hate languages that don't have static types. Such quackery. :3
User avatar
Adam Porter
 
Posts: 3532
Joined: Sat Jun 02, 2007 10:47 am

Post » Fri Aug 30, 2013 11:23 am

That's a lot of questions. Which one exactly should he start by asking?

I really think you're asking too much of the OP right now. If you give him the solution, that won't destroy any opportunity he has to learn why the solution is the solution in the first place. In fact, I'd say he couldn't learn what the solution is on his own at this point, because he's trying to write something that is simply beyond his skill level.

That said, giving him the solution to this problem would probably help him to understand the things he has somewhat more of a grasp on, so he can (hopefully) begin to understand everything else in the future.

I know, right?! :tongue:

User avatar
CHANONE
 
Posts: 3377
Joined: Fri Mar 30, 2007 10:04 am

Post » Fri Aug 30, 2013 12:23 pm

print("Welcome!")print("Prepare yourself for combat!") ownAttack = input("Enter your own attack!") #FIXEDmin = 1max = 100enemyDefense = input("Enter your enemy's defense!")min = 1max = 100while type(ownAttack and enemyDefense)is int

Working on it...

User avatar
Killah Bee
 
Posts: 3484
Joined: Sat Oct 06, 2007 12:23 pm

Post » Fri Aug 30, 2013 6:25 pm

He can ask any and all he wants, starting whatever the first "huh" that pops in his head. Won't bother me none. So far he's asked no questions, which has really shocked me.

Strictly speaking, his code already works for him perfectly (provided he never types anything invalid in). All this is is an exercise of me trying to teach him something (specifically, input validation and try/catch, as I'm sure you figured out) so giving him the "extra credit bonus answer" would be completely pointless. It's not needed so long as he's the only person who plans on using this code and giving it to him (any more than I already have) wouldn't help him.

I'm just trying to help him understand the difference between "ten" and "10" and more importantly, how to handle it.
User avatar
alicia hillier
 
Posts: 3387
Joined: Tue Feb 06, 2007 2:57 am

Post » Fri Aug 30, 2013 4:29 pm

Okay, thanks guys for sticking with me through a lot so far.

I agree with both of you, but maybe not quite to the extent that Sheridan is suggesting, but perhaps I do need a shove in the right direction @DEFRON.

But I'm reading the code and previous posts, and I'm sticking with it guys ^_^ Updates are on their way

User avatar
Jacob Phillips
 
Posts: 3430
Joined: Tue Aug 14, 2007 9:46 am

Post » Fri Aug 30, 2013 5:46 pm

I understand the difference between a str and an int.

But I'm trying to figure out how to ensure the input is an int, since that is what I require.

My first "huh".... How do I do that. Well it obviously involves denoting what str and what an int is. I don't want a str as my input.

I could always accept 10 as a string and convert it to an int...... OBSERVATION

Well, wouldn't it actually have to be inputted as a string???????

User avatar
Glu Glu
 
Posts: 3352
Joined: Sun Apr 01, 2007 5:39 am

Post » Fri Aug 30, 2013 11:15 pm

OBSERVATION

since I define my parameters as such;

ownAttack = input("Enter your own attack!") #FIXEDmin = 1max = 100enemyDefense = input("Enter your enemy's defense!")min = 1max = 100

Doesn't that mean that I must check a while statement right beneath?

No, I should have a while statement right underneath, and min/max beneath that. Right??

ownAttack = input("Enter your own attack!") #FIXEDwhile ownAttackmin = 1max = 100enemyDefense = input("Enter your enemy's defense!")while enemyDefensemin = 1max = 100# As such

Although I have to obviously finish it...

User avatar
Lou
 
Posts: 3518
Joined: Wed Aug 23, 2006 6:56 pm

Post » Fri Aug 30, 2013 5:03 pm

Converting to an integer using int() won't work. Or, rather, it won't fix your problem if the user types something other than 10.
User avatar
Albert Wesker
 
Posts: 3499
Joined: Fri May 11, 2007 11:17 pm

Post » Fri Aug 30, 2013 10:12 pm

Yes it does, and it's already being typecasted as an int.

As DEFRON has been saying, there's nonetheless something else to consider. If you enter "10", that will be entered as a string and correctly converted to an int using the int() function, and everything will be smooth sailing. However, if you input the word "ten", your program will bomb, because it does not automatically know that the word "ten" is the same as "10."

So the solution to that problem, then, is to check what the user is entering and see whether it's a proper number (like "82") or just garbage (ie "bacon" or even "ten"). Now there are many ways to do this, and as I've been trying to express to DEFRON, that's where it may be best for him to lead by example, because I don't know how to write code like that in Python. :tongue:

It's not that shocking. :P He only knows about half of what he's asking in the first place.

It's not pointless. It improves his program and gives him something to tinker and experiment with, if not in this project, then in future ones.

User avatar
Terry
 
Posts: 3368
Joined: Mon Jul 09, 2007 1:21 am

PreviousNext

Return to Othor Games