I'm a new Python programmer and when i run this code in idle python,i see: invalid syntax in "else"
def rloan(principal, annual_interest_rate, duration , number_of_payments):
r = annual_interest_rate/(12*100)
n = duration*12
p = number_of_payments
if r>0 :
remain = (principal*((1+r)**n-(1+r)**p)/((1+r)**n-1)
else:
remain = principal*(1-(p/n))
return remain
print(rloan(1000.0,4.5,5,12))
I'm probably missing something very simple; however, I haven't been able to find the answer on my own, please help me
You forgot to close a bracket.
remain = (principal*((1+r)**n-(1+r)**p)/((1+r)**n-1))
Correct this line and it should work.
Edit:
I think your print statement is also not indented properly.
Here is a working code:
def rloan(principal, annual_interest_rate, duration , number_of_payments):
r = annual_interest_rate/(12*100)
n = duration*12
p = number_of_payments
if r>0 :
remain = (principal*((1+r)**n-(1+r)**p)/((1+r)**n-1))
else:
remain = principal*(1-(p/n))
return remain
print(rloan(1000.0,4.5,5,12))
Related
records = [["chi", 20.0],["beta", 50.0],["alpha", 50.0]]
a = len(records)
i = 0
b = []
while i < a:
print(records[i][1])
b.append(records[i][1])
i = i + 1
print(b)
c = len(b)
#from previous question
unique_list = []
for el in b:
if el not in unique_list:
unique_list.append(el)
else:
print ("Element already in the list")
print(unique_list)
second_lowest_score = unique_list[1]
print(second_lowest_score)
names_list = []
g = 0
while g < a:
if records[g][1] == second_lowest_score:
names_list.append(records[g][0])
g = g + 1
print(names_list)
What I want to do is to append the names of records which have the second lowest score (50) to the names_list. However, the while loop gives me no result. There is no syntax error so I am not sure why my code is wrong. However, when I use the previous while loop for appending the numbers, it seems to work fine. Is it not possible to use an if statement in a while loop?
This is a pretty simple problem. The g variable is not getting incremented if the if statement does not run, so the loop will endlessly continue on the same value of g.
The fix for this is to move the increment of g outside of the if statement (but still in the while loop). That way it will continue past values even if they do not match the if condition.
if records[g][1] == second_lowest_score:
names_list.append(records[g][0])
g = g + 1
I need to replace ANSII characters with UNICODE (Sinhala). I use lists with a loop to do that as follows,
for i in range (len(charansi)):
for j in range (len(charUni)):
s = charansi[i] + ansimod[j]
v = charUni[i] + modUni[j]
textSource = textSource.replace(s, v)
if we use n + uu as ANSII input, it should give නූ as Unicode out put. But instead of that, it gives න ූ
to clarify more,
charansi = n
ansimod = uu
charUni = න
modUni = ූ
this න and ූ must join without spaces. I think ZWJ (\u200D) plays a role here. so i tried
v = u"\u200D".join((consonantsUni[i], vowelModifiersUni[j]))
gives same result.
How do I fix this issue?
Your question is a bit confusing, but this simply works:
#coding:utf8
charansi = 'n'
ansimod = 'uu'
charUni = 'න'
modUni = 'ූ'
v = s.replace(charansi+ansimod,charUni+modUni)
print(v)
Output:
නූ
Create a working example of the problem if this isn't what you want.
You could also use the following to make the characters more clear. At least on my browser, the modifier didn't display very well.
charUni = '\N{SINHALA LETTER DANTAJA NAYANNA}'
modUni = '\N{SINHALA VOWEL SIGN DIGA PAA-PILLA}'
Good afternoon everyone,
I'm trying to sort out names which are already sorted in alphabetical order. I can't figure out why my program isn't working. Any tips or pointers would be nice. Thanks.
def main():
names = ['Ava Fiscer', 'Bob White', 'Chris Rich', 'Danielle Porter', 'Gordon Pike', 'Hannah Beauregard', 'Matt Hoyle', 'Ross Harrison', 'Sasha Ricci', 'Xavier Adams']
input('Please enter the name to be searched: ', )
binarySearch
main()
def binarySearch(names):
first = 0
last = len(names) - 1
position = -1
found = False
while not found and first <= last:
middle = (first + last) / 2
if names[middle] == value:
found = True
position = middle
elif arr[middle] > value:
last = middle -1
else:
first = middle + 1
return position
What does it mean that the program isn't working? Is it a syntax error or is the problem in the wrong results?
With the code you pasted, there are several indentation problems, but besides that, lines:
input('Please enter the name to be searched: ', )
binarySearch
are also syntactically incorrect, the comma is redundant and only the function name appearing just like that is plain wrong. If you are interested in the correctness of your algorithm, it seems alright, but the boundaries can always be tricky. My code below is working and syntactically correct, if you find it helpful. (names are numbers, but that is irrelevant in this case)
names = [1,2,4,5,6,8,9]
def bs(n):
start = 0
end = len(names)
while end - start > 0:
m = (start+end)/2
if names[m] == n:
return m
elif n < names[m]:
end = m
else:
start = m + 1
return -1
print (bs(1))
print (bs(6))
print (bs(9))
print (bs(3))
print (bs(10))
print (bs(-8))
Another thing I would like to point out is that this kind of binary search is already in the python standard library, the bisect module. However, if you are writing your own for practice or for any other reason that is just fine.
if you are using python 3.* then you are going to want to change
m = (start+end)/2
to
m = (start+end)//2
When you do /2 it outputs a float in 3.*
I'm trying to make everything look tidy in a file but I'm having a problem.
I am asking for Details and if it is more then 27 bytes long then I need to find the nearest previous space and put it on a new line.
(using python 3.2)
I am starting with this:
Details = input ("Details: ")
delen = len(Details)
if delen >27:
#Code Here
else:
pass
Is this possible? If so could you give me a hand please?
Cheers
str.rfind is your friend.
Details = input ("Details: ")
if len(Details) > 27:
nearest_space = Details.rfind(' ', 0, 27)
first, second = Details[:nearest_space], Details[nearest_space:]
else:
first, second = Details, None
Note that rfind will raise an exception if no space is found in Details.
I'm no Python expert. There are probably some fancy ways of doing this. I would keep it simple and do:
Detail = input ("Details: ")
line_length = 27
delen = len(Detail)
pos = 0
while pos < delen:
print Detail[pos:pos+line_length]
pos += line_length
I keep getting a syntax error with the following line of code:
# If the user asked for celsius then x will be a Celsius number converting to Fahrenheit number
if x = = c
Do you have any idea?
The = = should be ==
if x == c:
Make sure your using == instead of = = (if its not clear)-> replace =[SPACE]= with ==.
Add a colon as well to removing the space.