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 1 year ago.
Improve this question
As the home assignment, I have two code cells. In the first one, I need to write a code, while the second check the written function (it was made by a professor)
[Cell1]
def is_even_number(num):
'''
Returns True if num is even number, otherwise False
Parameters
----------
num : an integer
Returns
-------
True if num is even number, otherwise False
'''
# YOUR CODE HERE
while num:
if num % 2 == 0:
return True
elif num % 2 != 0:
return False
[Cell2]
assert_true(is_even_number(2), msg='2 is an even number.')
assert_true(not is_even_number(1), msg='1 is not an even number')
As a result of run Cell2, I see the message NameError: name 'assert_true' is not defined
I think it should be assertTrue . If it's using the module unittest. Here's the link
Related
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 6 months ago.
Improve this question
This code gives exactly what is says but how and why?
To my understanding, for instance the is_odd function if called with any value, the value will diminish into zero and will return True in the is_even() section and so will become False in is_odd() in turn. So every is_odd check supposed to become False but it's not, it just works on every odd/even check, why and how?
#!/bin/python
def is_even(x):
if x == 0:
return True
else:
return is_odd(x-1)
def is_odd(x):
return not is_even(x)
print(is_odd(2))
print(is_even(2))
print(is_odd(3))
print(is_even(3))
Output:
False
True
True
False
Note that is_even does not always return True but can also return is_odd(n-1), which in turn return not is_even(n-1), and so on.
Example for is_odd(3):
is_odd(3)
not is_even(3)
not is_odd(2)
not not is_even(2)
not not is_odd(1)
not not not is_even(1)
not not not is_odd(0)
not not not not is_even(0)
not not not not True
not not not False
not not True
not False
True
In general, you get a cascade of N negations for an even number, or N+1 negations for an odd number, and if the number of those negations is even, the result is True.
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 months ago.
The community reviewed whether to reopen this question 5 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I am told that the following python-code does return me a true/false statement wether at least one element of the list "nums" is divisible by 7 (yeah, and for a 0). But let's say that the first value in the list is 14, so clearly divisible by 7.
I then would expect a return of "true" due to the "for" loop. And then the script continues and gives me the return "false", no matter what happend before that? Or does the skript abort after it finds a "true"? I thought I really understood how "return" works in python, but apparently not.
def has_lucky_number(nums):
"""Return whether the given list of numbers is lucky. A lucky list contains
at least one number divisible by 7.
"""
for num in nums:
if num % 7 == 0:
return True
return False
I would be very gratefull for some help here.
EDIT: apparently this seems to be unclear: I want to know WHY this skript returns "True" if nums has an element [14]. I know that it does that. But I would not expect it to do that because I would expect the last thing the code does to be to always return "False" due to the last line.
In most languages 'return' means you return a value and exit the function to the callee.
If you want it to "return" multiple values, essentially you want it to either return a list, or you want the function to be a generator.
A generator is something that yields rather than returns multiple values. So in your example you could instead do something like this:
def has_lucky_number(nums):
"""Return whether the given list of numbers is lucky. A lucky list contains
at least one number divisible by 7.
"""
for num in nums:
if num % 7 == 0:
yield True
else:
yield False
Which you would then use like this:
for value in has_lucky_number([1,2,5,14]):
# do something
Which would yield multiple values (four in total).
the 'return False' statement will only execute after the for loop. But if you enter another return statement before that it won't get executed.
Rule of thumb return statement ALWAYS exit functions.
return stop the for and return True if a number can be divided by 7. I tried with has_lucky_number([14,16]) and returns me True
The function end immediately after the first return statement.In this case,if the first number in the list is divisble by 7, the function will return True. If you want to see if all the numbers in the list are divisble by 7. You need to check them then return false or true at the end. You can use counter to see wich number is divisible by 0 then if the counter is equal to the list lenghtit returns true.
'return' means you return a value and exit the function to the callee.
If you want it to "return" multiple values, you can return a list.
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 12 months ago.
Improve this question
I just started learning py and i tried to create a simple program that checks whether a pin is long a 4 or 6 digits or not, but somehow it doesn't work.
def validate_pin(pin):
if len(pin) == 4 or len(pin) == 6:
return true
else:
return false
The value passed to validate_pin() would need to be of type str for this to make sense. Consider a PIN number of 0001. That's a valid PIN number but when expressed as int it isn't. Therefore:
def validate_pin(pin):
return len(pin) == 4 or len(pin) == 6
Python booleans are capitalized, so you have to change true to True and false to False
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 2 years ago.
Improve this question
I'm trying to make an average but for some reason when I try to make one it doesn't work.
I have global variables and array defined at the begining of my document :
vent_moyenne_km = []
compteur_moyenne=0
I have one of my function that is called every X time. In that one, I calculate a velocity with some value that are display on a label of my interface. that part is working, but not the mean
global compteur_moyenne
compteur_moyenne += 1
ventkmh = (vent_1[3][0]*256 + vent_1[4][0]) /100 *3.6
label_vent2_2.config(text= "%.2f" % ventkmh)
vent_moyenne_km.append("%.2f" % ventkmh)
vent_1.clear()
if compteur_moyenne == 5:
compteur_moyenne = 0
print(vent_moyenne_km)
label_vent4_2.config(text=statistics.mean(vent_moyenne_km))
vent_moyenne_km.clear()
of course in my imports I have :
import statistics
When I comment the line label_vent4_2.config(text=statistics.mean(vent_moyenne_km)), everything works and I see in the terminal my array with 5 values. I also tried numpy and even tried to make a for items in array: then add then manually, and everytime I get the error : class 'IndexError'
I'm really not sure how to fix that.
For calculating an average of a list just use numpy:
def function():
value = random.randint(0,1)
return value
list = []
for i in range(100):
list.append(function())
if i%5 == 0:
print(np.average(list))
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
Hi all I have read through the previous answers to this question and can get the code to run. What I want to understand is why my code doesn't run.
Thanks
def collatz(number):
if number % 2 == 0:
return number // 2
elif number % 2 == 1:
return 3 * number + 1
print('Enter a number')
number = int(input())
while number != 1:
print(int(collatz(number)))
You are not updating number in your while loop so you are stuck in an infinite loop.
You should assign return value of collatz to number back, to update number.
while number != 1:
number = collatz(number)
print(number)