while using def function i got this error [closed] - if-statement

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
def even_or_odd(num):
if num%2==0:
return "the number {} is Even".fromat(num)
else:
return "the number {} is Odd".format(num)
AttributeError
Traceback (most recent call
last) in
----> 1 even_or_odd(20)
in even_or_odd(num)
1 def even_or_odd(num):
2 if num%2==0:
----> 3 return "the number {} is Even".fromat(num)
4 else:
5 return "the number {} is Odd".format(num)
AttributeError: 'str' object has no attribute 'fromat'

You should change the line to
return "the number {} is Even".format(num)
The error is just a typo.

Related

How to Create a List that holds 2 types of variable types in Scala [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Im new to scala so please go easy on me lol.
I need to create a list of where each spot holds an Int,String. So like [(1,"string1"),(2,"String2")...]
for example, Ive tried
val string1 = "something"
val string2 = "something"
List[Int,String] = List[(1,string1), (2,string), (3,string3),(4,string4),(5,string5)]
and I get the error - identifier expected but integer literal found.
How exactly would I get something like this to work?
(1,"string1") is a tuple containing an Int and a String, so type of list should also be a tuple - (Int, String):
val string1 = "something"
val string2 = "something"
// ... rest of string values
val list: List[(Int,String)] = List((1,string1), (2,string2), (3,string3),(4,string4),(5,string5))

How do i do arithmetic for all elements of a list [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 5 years ago.
Improve this question
I need to find the biggest factor of the number 600851475143
so in order for doing that i want to find all primes smaller that this number
number = input("enter max number:")
def findprime (number):
prime = [1,2]
for i in range (2,number):
if(i%)
how do i preform arithmetic's for all numbers in a list?
To find the largest factor, find the smallest one and divide. And you only need to check up to the sqrt of the number:
factor = 0
for i in range (2, int(number**0.5) + 1):
if number%i == 0:
factor = i
break
if factor: print(number/factor)
else: print number, 'is prime'

How to sum a matrice of matrices [closed]

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 5 years ago.
Improve this question
I have a matrix that each spot in it is another matrix, and I need to print a new matrix which is a form from the sum of each matrix in the same index .
>>sum_matrices([[[1,2,3],[4,5,6]],[[11,12,13],[14,15,16]],[[21,22,23],[24,25,26]],[[31,3 2,13],[34,35,36]]])
[[64, 68, 52], [76, 80, 84]]
I have this code which adds 2 matrices together.
def sum_matrices(mat_lst):
result = []
for i in range(len(mat_lst)):
rows=[]
for j in range(len(mat_lst)):
rows.append(add_matrices(mat_lst[i][j]))
result.append(rows)
return result
Try like this,
def sum(matrix):
result = matrix[0][:]
for i in range(1, len(matrix)):
for j in range(len(result)):
for k in range(len(result[j])):
result[j][k] += matrix[i][j][k]
print result

How to use regex for a persons name? [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 7 years ago.
Improve this question
Basically here is my code and I cannot find the problem with this, So I'm looking for advice.
name = raw_input('What\'s your name? ')
if not re.match(r'[A-Za-z- ]+', name):
print 'Invalid name.\n' #error message
You must need to add end of the line anchor. Without the anchor foo? would be considered as a valid one. That is, it won't print the message Invalid name for this name.
if not re.match(r'[A-Za-z- ]+$', name):
print 'Invalid name.\n'
Example:
>>> s = 'foo?'
>>> if not re.match(r'[A-Za-z- ]+', s):
print('Invalid name.\n')
>>> if not re.match(r'[A-Za-z- ]+$', s):
print('Invalid name.\n')
Invalid name.

In SML, how do I remove the first occurence of a pattern from a list and then return the removed item and the rest of the list? [closed]

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 8 years ago.
Improve this question
I'm trying to write an SML function that takes as parameters a list and an item. If the item is present, the function should return a tuple containing the list without the first such occurence of that item and the item just removed. If there are no occurences of the item in the list, the function should return NONE or something similar to indicate this absence.
Try this:
fun same_string(str, lst) =
case lst of
[] => NONE
|x::xs => case same_string(str, xs) of
NONE => if str = x
then SOME(xs)
else NONE
|SOME xs' => SOME (x :: xs')