Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Can anyone tell me why this has a syntax error? I've run this exact code before and it worked perfectly. The line in strong text is where Python tells me the syntax error is. Thanks, everyone!
import random
count = 0
while count < 10:
attackerLV = 20
attackerST = 20
attackerSK = 20
baseAtkPwr = 20
attackPWR = ((random.randint(85,100) * (baseAtkPwr + attackerLV + attackerST + attackerSK)) // 100
**defenderLV = 20**
defenderCON = 20
defenderSKa = 20
baseDefPwr = 20
defensePWR = (((random.randint(85,100)) * (baseDefPwr + defenderLV + defenderCON + defenderSKa)) // 4) // 100
damage = attackPWR - defensePWR
if damage <= 1:
damage = 1
print(str(attackPWR))
print(str(defensePWR))
print(str(damage))
print()
count = count + 1
You missed a parenthesis here:
attackPWR = ((random.randint(85,100) * (baseAtkPwr + attackerLV + attackerST + attackerSK)) // 100
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I only got this far with the answer. I don't understand how recursive functions work. I would really appreciate any help. And also, if someone could explain what base and recursive calls are, that'd be great.
def multi(n):
if n==1:
return 4
Please try this code :
def multi(n):
return (5 + multi(n-1)) if n>0 else 0;
this function is a 5n, here's the explanation:
when n = 1 the call is like this :
output : 5 + multi(0) --> multi(0) = 0, so 5 + 0 = 5
It's something like this :
multi(3)
|
-- 5 + multi(2)
|
--- 5 + (5 + multi(1))
|
---- 5 + (5 + (5 + multi(0)))
|
----- 5 + 5 + 5 + 0
5n --> if n = 3 --> 5(3) = 15
output multi(3) = 15, as same as 5(3)=15.
5(3) = 5+5+5 = 15
5(2) = 5+5 = 10
in recursion, you'll do it like this:
5(0) = 0
5(1) = 5 + 0 = 5
5(2) = 5 + 5 + 0 = 10
...
5(n) = 5 + f(n-1), where f(0) = 0, so n must greater than 0. To prevent infinite loop.
Case for 5(3) :
1. 5(3) = 5 + f(2)
2. f(2) = 5 + f(1)
3. f(1) = 5 + f(0)
4. f(0) = 0
So, you will get 5(3) = 5 + (5 + (5 + 0)) = 15
Hope this help you to understand it :D
Recursive function is a function that makes a call to it self.
for example:
def multi(n):
if n>0:
return n * multi(n-1)
else:
return 0
To prevent infinit recursion you have to define a case where the function does not make a call to it self.(in our example it's in else statement when n==1).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Today, i have trying to resolve a small star pyramid :
Input:
5 1
Output:
*
**
***
****
Code:
x = 1
y = 0
m, e = map(int, raw_input().split())
while x < m:
print "\n" * y, "*" * e
m -= 1
e += 1
I did that but there is a better solution?? Thanks =)
I think this can be solved more easily:
stop, first = map(int, raw_input().split())
for i in range(stop - 1):
print '*' * (i + first)
just for fun >:)
class c:
def __init__(s,m,e):
s.e , s.m = sorted([e, m])
s.r = 42
def __iter__(s):
return s
def next(s):
if s.m < s.e:
t = "".join(chr(s.r) for _ in range(s.m))
s.m += 1
return t
else:
raise StopIteration
print "\n".join(c(*map(int,raw_input().split())))
n = int(raw_input())
for i in range(n): print "*"*i
This appears to do what your program intends to do, however I can't quite tell because of the issues I raised in my comment above.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For example
(x + y)^4 = x^4 +(4x^3)y + (6x^2)y^2 + + 4xy^3 + y^4
I'm using python 3.3.2 and I don't know where to start, I need a little guidance. I'm not asking for the answer to it, just the general steps to make this program work. I've done a few other programs before, and this is probably pushing the limit on what I can do.
SymPy already do that for you:
>>> import sympy
>>> x, y = sympy.symbols("x y")
>>> formula = (x + y) ** 4
>>> formula
(x + y)**4
>>> formula.expand()
x**4 + 4*x**3*y + 6*x**2*y**2 + 4*x*y**3 + y**4
If you need a visualization as a string with "^", you can do:
>>> str(formula.expand()).replace("**", "^")
'x^4 + 4*x^3*y + 6*x^2*y^2 + 4*x*y^3 + y^4'
The code below can be found on this site.
Usage
python binomialexpansion.py 3
Output
x^3 + 3x^2y + 3xy^2 + y^3
Code
import sys
power = int(eval(sys.argv[1]))
strpower = str(power)
coeffs = []
if power == 0:
print 1
exit()
if (power+1) % 2 == 0:
turningp = (power+1)/2
counter = 1
else:
turningp = (power+2)/2
counter = 2
for i in range(1, power+2):
if i == 1:
sys.stdout.write("x^"+strpower+" + ")
coeffs.append(1)
continue
if i == power+1:
print "y^"+strpower,
coeffs.append(1)
break
if i > turningp:
co = coeffs[turningp-counter]
counter = counter+1
else:
co = ((power-(i-2))*coeffs[i-2])/(i-1)
coeffs.append(co)
sys.stdout.write(str(co))
if power-(i-1) == 1:
sys.stdout.write("x")
else:
sys.stdout.write("x^"+str(power-(i-1)))
if i-1 == 1:
sys.stdout.write("y ")
else:
sys.stdout.write("y^"+str(i-1)+" ")
sys.stdout.write("+ ")
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
How can I print the full float result of a simple operation in python? My code does:
if( int(array_Y[counter2]) == int(round(float(elem[0])))):
if(int(round(float(elem[0]))) == 0):
negatiu_verdader += 1
if(int(round(float(elem[0]))) == 1):
positiu_verdader += 1
counter = counter + 1
counter2 = counter2 + 1
error = float(1.0000- (1.0000 * counter / counter2))
print " ERROR!!!!!!!!!!!!!!!!!!!!!!!! :" + ("{0:.15f}".format(round(error,2)))
But the error is always: 0.420000000000000 or 0.230000000000000 but I would like the error to be: 0.43233213213232.
You are rounding your error down to two decimal places by calling round(error, 2):
>>> round(0.43233213213232, 2)
0.43
Don't do that if you want to show more precision:
>>> format(round(0.43233213213232, 2), '.15f')
'0.430000000000000'
>>> format(0.43233213213232, '.15f')
'0.432332132132320'
You do a lot of redundant work in your code, simplify it down a little:
elem_rounded = int(round(float(elem[0])))
if int(array_Y[counter2]) == elem_rounded:
if not elem_rounded:
negatiu_verdader += 1
elif elem_rounded == 1:
positiu_verdader += 1
counter += 1
counter2 += 1
error = 1.0 - (1.0 * counter / counter2)
print " ERROR!!!!!!!!!!!!!!!!!!!!!!!! :{0:.15f}".format(error)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
This is a very simple piece of code that I wrote but if there is a way to make it more pythonic then I would love to know. Thanks!
def money():
current_salary = float(input("What is your current salary? "))
years = int(input("How many years would you like to look ahead? ")) + 1
amount_of_raise = float(input("What is the average percentage raise you think you will get? "))
amount_of_raise = amount_of_raise * 0.01
while years > 1:
years = years - 1
new_salary = current_salary + (current_salary * amount_of_raise)
current_salary = new_salary
print('Looks like you will be making', new_salary,' in ', years,'years.')
money()
Extended assignment operators
amount_of_raise = amount_of_raise * 0.01
years = years - 1
x = x * y can be shortened to x *= y. Same thing for -.
amount_of_raise *= 0.01
years -= 1
Iteration and counting
while years > 1:
years = years - 1
Counting down causes your printouts to display backwards. I would count up. The Pythonic way to count uses range:
for year in range(1, years + 1):
print('Looks like you will be making', new_salary,' in ', years,'years.')
Computing new salary
new_salary = current_salary + (current_salary * amount_of_raise)
current_salary = new_salary
I'd probably just simplify that to:
current_salary += current_salary * amount_of_raise
Or even better is to give a 5% raise by multiplying by 1.05. In code that is:
current_salary *= 1 + amount_of_raise