Should I do anything to make my code more pythonic? [closed] - python

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

Related

I tried to create a basic Slingshot algorithm... Basicly i tried to eliminate every problems in my code but now it doesn't do anything [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 months ago.
Improve this question
from math import sin, cos
def Sling():
b_h = float(input("Input starting speed: "))
b_angl = float(input("Input starting angle: "))
vy = b_h * sin(b_angl)
vx = b_h * cos(b_angl)
g = 9.8
t = (vy/8)
Max_height = (vy - (-g*t*t)) # max height of the throw
tx = 0
Projectile_CS = (vy * tx) - (-g*tx*tx) # current spot of the projectile
while Projectile_CS >= 0:
tx += 0.1
else:
print("Done")
Max_distance = tx * vx
print("Max")
for x in range(round(float(Max_height)/5)):
print("*")
print("0", "_" * t, "_" * round((Max_distance)/5))
print("The maximum height is: ", round(Max_height))
print("Max distance is: ", round(Max_distance))
return
Sling()
After the while Projectile_CS >= 0: part of the code, it doesn't do anything, or looks like it. I can stop the code or restart with debugging but it doesn't help.
You never update Projectile_cs in the while loop, therefore you get stuck inside it
Your while loop continues until Projectile_speed value is < 0, but since you never change projectile_speed value in the while loop it will forever be < 0
For your code to work you have to update your variable in the while loop, according to what you did earlier it would probably look something like that
Projectile_CS = (vy * tx) - (-g*tx*tx)
while Projectile_CS >= 0:
tx += 0.1
Projectile_CS = (vy * tx) - (-g*tx*tx)

calculate years compound interest python [closed]

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 2 years ago.
Improve this question
So i have this task, i need to create formula in python, I have all the program running good, but i just can't think of the formula to count years, that's compound interest
what i need to count is how many years will it take to get to the target(lets say from 1500 to 2000)
Got this formula t = ln(A/P) / n[ln(1 + r/n)], but i don't get the right answer
https://www.thecalculatorsite.com/articles/finance/compound-interest-formula.php
Also tried this
https://www.algebra.com/algebra/homework/logarithm/logarithm.faq.question.117944.html
update.
Thanks for help! answer in comments for those who have same issue
This might work:
from math import log
p_start = 1500 # Beginning principal
r = 4.3 # Interest rate
p_end = 2000 # Ending principal
"""
# Lambda function
# n == number of compounding periods per year.
# Default n is 12, or monthly compounding
# Formula Breakdown
round((
log(A / P) /
(n * (
log(1 + (r/n))
)
)
# Add 0.5 to account for needing to round up.
) + 0.5
# Round to zero decimal places
, 0)
"""
get_time = lambda A, P, r, n=12: round((log(A / P) / (n * (log(1 + (r/n))))) + 0.5, 0)
# If interest rate isn't a percentage, convert it
if r > 0:
r /= 100
get_time(p_end, p_start, r, 1) # 7.0 using n = 1, or compounded once per year.
EDIT: Adding solution for comment below:
def calculate_years_to_target(principal, rate, target, n=12):
if rate > 0:
rate /= 100
years = round((math.log(target / principal) / (n * (math.log(1 + (rate/n))))) + 0.5, 0)
return years
calculate_years_to_target(1500, 4.3, 2000, 1)
Compound Interest Calculator:
import math
P = float(input("Enter the initial deposit: "))
A = float(input("Enter the final amount: "))
N = float(input("Enter the number of times the interest is applied per time period: "))
T = float(input("Enter the number of time periods elapsed: "))
interestRate = N*(pow(A/P, 1/(N*T))-1)*100
print("The interest rate is " + str(interestRate) + "%.")

Bouncing Ball Distance [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to make a program that allow me to enter a height of a ball to be dropped and put in the number of bounces and give me the total distance. I am having trouble with my equation and see some in-sight please.
h = int(input("Enter hieght"))
b = int(input("Enter Number of Bounces"))
for eachPass in range (b):
d = h * 0.6
if b >= 1:
bounce1 = h + d
if b >= 2:
bounce2 = (d * 0.6) + bounce1 + d
print (bounce2)
While I can't be sure, I believe you want something like this instead
h = int(input("Enter height"))
b = int(input("Enter Number of Bounces"))
bounce1 = 0
bounce2 = 0
d = h * 0.6
for eachPass in range(b):
if eachPass == 1:
bounce1 = h + d
if eachPass == 2:
bounce2 = bounce1 + h + d
print str(bounce2)

is there possible to resolve this star pyramid [closed]

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.

Explain this syntax error [closed]

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

Categories

Resources