Question

Q2. Booleans and testing (a) Define the following function. 4 marks def valid(x, y, z): #valid takes three numbers and it ret

Hi, This question is from python unit

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def valid(x,y,z):
    positives = 0
    negatives = 0
    if(x>0):
        positives+=1
    if (y > 0):
        positives += 1
    if (z > 0):
        positives += 1
    if (x < 0):
        negatives += 1
    if (y < 0):
        negatives += 1
    if (z < 0):
        negatives += 1
    if(positives>=1 and negatives<=1):
        return True
    else:
        return False
    
def test_valid():
    print(valid(1,2,3))
    print(valid(-1, -2, -3))
    print(valid(-1, -2, 3))
    print(valid(-1, 2, 3))
    print(valid(1, -2, -3))
    print(valid(1, -2, 0))
    print(valid(1, 0, 0))
    print(valid(0, 5, 0))
Add a comment
Know the answer?
Add Answer to:
Hi, This question is from python unit Q2. Booleans and testing (a) Define the following function....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

  • 1. Define a function in python that returns the sum of the following 4 lists. Remember...

    1. Define a function in python that returns the sum of the following 4 lists. Remember to store and re- use your code instead of summing the values for each list 4 times. Hence, define a function that takes as an argument a list and returns the sum of values. Copy the following lists and paste them in a Python file after you define the sum_of_values function Call the function and pass to it a different list each call as...

  • Write a Python file containing the following functions. Also turn in the output from testing the...

    Write a Python file containing the following functions. Also turn in the output from testing the functions. All arguments to the functions may be hard-coded. Function 1 takes a list of strings as a parameter and returns a list of strings consisting of all the strings in the original list that have identical consecutive letters. For example: fun1 ([llama, good, cat, abba, abab, 112, dog]) returns [llama, good, abba, 112] Function 2 takes an integer (n), representing the number of...

  • Please use python 3 programming language Write a function that gets a string representing a file...

    Please use python 3 programming language Write a function that gets a string representing a file name and a list. The function writes the content of the list to the file. Each item in the list is written on one line. Name the function WriteList. If all goes well the function returns true, otherwise it returns false. Write another function, RandomRange that takes an integer then it returns a list of length n, where n is an integer passed as...

  • 1)def toggle_cell(row, column, cells): • Return value: Flip the cell in the column of row from...

    1)def toggle_cell(row, column, cells): • Return value: Flip the cell in the column of row from alive to dead or vice versa. Return True if the toggle was successful, False otherwise. • Assumptions: o cells will be a two-dimensional list with at least one row and one element in that row. o row and column are index values of cells • Notes: o Cells is being edited in place. Note that the return value is a Boolean and not a...

  • Finish each function python 3 LList.py class node(object): """ A version of the Node class with...

    Finish each function python 3 LList.py class node(object): """ A version of the Node class with public attributes. This makes the use of node objects a bit more convenient for implementing LList class.    Since there are no setters and getters, we use the attributes directly.    This is safe because the node class is defined in this module. No one else will use this version of the class. ''' def __init__(self, data, next=None): """ Create a new node for...

  • need the code in .c format #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <math.h> struct _cplx double re,...

    need the code in .c format #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <math.h> struct _cplx double re, im; // the real and imaginary parts of a complex number }; typedef struct _cplx Complex; // Initializes a complex number from two real numbers Complex CmplxInit(double re, double im) Complex z = { re, im }; return z; // Prints a complex number to the screen void CmplxPrint(Complex z) // Not printing a newline allows this to be printed in the middle of...

  • Python Question: Define the function high_score that consumes a list of integers (representing scores in a...

    Python Question: Define the function high_score that consumes a list of integers (representing scores in a game) and produces an integer representing the highest score in the list. Ignore scores less than 100, and stop processing values if you encounter -999. If the list is empty, return the value None instead. It is up to you to decompose this function (or not) however you want. Here is my code so far: from cisc108 import assert_equal def high_score(scores: [int])->int: max_num =...

  • ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and...

    ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and including the given value. This function has to be recursive; you may not use loops! For example: Test Result print('%d : %d' % (5, sum_up_to(5))) 5 : 15 Q2, Write a recursive function that counts the number of odd integers in a given list. This function has to be recursive; you may not use loops! For example: Test Result print('%s : %d' % ([2,...

  • implement function 2 in python. name the source file as h1.py. A test file h1_test.py is...

    implement function 2 in python. name the source file as h1.py. A test file h1_test.py is provided below! h1_test.py import re import math import pytest from h1 import (change, strip_quotes, scramble, say, triples, powers) def test_change(): assert change(0) == (0, 0, 0, 0) assert change(97) == (3, 2, 0, 2) assert change(8) == (0, 0, 1, 3) assert change(250) == (10, 0, 0, 0) assert change(144) == (5, 1, 1, 4) assert change(97) == (3, 2, 0, 2) assert change(100000000000)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT