I keep getting a syntax error - python-3.x

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.

Related

Specific string generation in python of 5 alphabets followed by 4 numbers. example ABCDE1234

I have used the following code but not getting the desired output. please help me.
"{}{}{}".format((random.choices(string.ascii_uppercase)for i in range(5)), random.randint(1000,9999))
I don't know exactly why it doesn't work, but I managed to get it to print the desired result using this code:
x = []
y = ""
for i in range(5):
x += random.choices(string.ascii_uppercase)
x += str(random.randint(1000,9999))
print (y.join(x))
My guess is that it's because you're trying to add a list (your method of string generation produces a list of string characters) and an integer (randint produces an integer) to a string.

when i run this code,i see: **invalid syntax in "else"**

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))

How do I return a string with non-alphabetical using the following code?

string = 'jackk982'
y = 0
def is_alpha(x):
global y
for char in x:
for num in range(97, 123):
if ord(char) == num:
y += 1
x = x[y:]
return x
print(is_alpha(string))
I cant seem to find anything wrong, but the output does not give me 982. How can I fix this?
Okay, I don't know what you were trying to do with the two for-loops but the simplest way would be the following:
string = 'jackk982'
def get_string(x):
for a in range(0, len(x)):
if x[a].isdigit():
return x[a:]
print(get_string(string))
For the string jackk982, it returns 982. For another input hahahahlol5hf it returns 5hf.
The code first checks if one of the characters in the string is a number and if so it returns the rest of the string from that character including the number.

Getting the largest and smallest word at a string

when I run this codes the output is (" "," "),however it should be ("I","love")!!!, and there is no errors . what should I do to fix it ??
sen="I love dogs"
function Longest_word(sen)
x=" "
maxw=" "
minw=" "
minl=1
maxl=length(sen)
p=0
for i=1:length(sen)
if(sen[i]!=" ")
x=[x[1]...,sen[i]...]
else
p=length(x)
if p<min1
minl=p
minw=x
end
if p>maxl
maxl=p
maxw=x
end
x=" "
end
end
return minw,maxw
end
As #David mentioned, another and may be better solution can be achieved by using split function:
function longest_word(sentence)
sp=split(sentence)
len=map(length,sp)
return (sp[indmin(len)],sp[indmax(len)])
end
The idea of your code is good, but there are a few mistakes.
You can see what's going wrong by debugging a bit. The easiest way to do this is with #show, which prints out the value of variables. When code doesn't work like you expect, this is the first thing to do -- just ask it what it's doing by printing everything out!
E.g. if you put
if(sen[i]!=" ")
x=[x[1]...,sen[i]...]
#show x
and run the function with
Longest_word("I love dogs")
you will see that it is not doing what you want it to do, which (I believe) is add the ith letter to the string x.
Note that the ith letter accessed like sen[i] is a character not a string.
You can try converting it to a string with
string(sen[i])
but this gives a Unicode string, not an ASCII string, in recent versions of Julia.
In fact, it would be better not to iterate over the string using
for i in 1:length(sen)
but iterate over the characters in the string (which will also work if the string is Unicode):
for c in sen
Then you can initialise the string x as
x = UTF8String("")
and update it with
x = string(x, c)
Try out some of these possibilities and see if they help.
Also, you have maxl and minl defined wrong initially -- they should be the other way round. Also, the names of the variables are not very helpful for understanding what should happen. And the strings should be initialised to empty strings, "", not a string with a space, " ".
#daycaster is correct that there seems to be a min1 that should be minl.
However, in fact there is an easier way to solve the problem, using the split function, which divides a string into words.
Let us know if you still have a problem.
Here is a working version following your idea:
function longest_word(sentence)
x = UTF8String("")
maxw = ""
minw = ""
maxl = 0 # counterintuitive! start the "wrong" way round
minl = length(sentence)
for i in 1:length(sentence) # or: for c in sentence
if sentence[i] != ' ' # or: if c != ' '
x = string(x, sentence[i]) # or: x = string(x, c)
else
p = length(x)
if p < minl
minl = p
minw = x
end
if p > maxl
maxl = p
maxw = x
end
x = ""
end
end
return minw, maxw
end
Note that this function does not work if the longest word is at the end of the string. How could you modify it for this case?

Binary search code not working

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.*

Resources