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.
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.
enemyAttack = randint(1, 10)userattack = randint(1, 10)print("your attack roll: " + userattack + " | " + "enemy attack roll: " + enemyAttack)
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.
Oh, excuse me if I wasn't clear, that's what I meant! If you check the code it's already updated.
How does the code look? I haven't tested it, but wouldn't that be ALL that is required???
See Post # 2
Edit: OKAY GUYS, so I tested my code!!
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
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.
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??
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).
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 modulusresults = 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!
> 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.
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?
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?
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.
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????
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
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 modulusif 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)
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.
Working actively on solving the user input issue. I think I may have figured it out.
#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 moduluswhile 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.