Question

In: Computer Science

Task 04: Running Average Write a function average that does not take any parameter and asks...


Task 04: Running Average

Write a function average that does not take any parameter and asks the user to enter a positive number in a loop. The loop terminates when the user enters a negative number and the average is printed on the console. The function does not return any value. Save the function in a PyDev library module named functions.py

Write a main program named t04.py that tests the function.

Sample run:
Enter a positive number: 2
Enter a positive number: 5
Enter a positive number: 7
Enter a positive number: 8
Enter a positive number: 1
Enter a positive number: -1
Average: 4.6
i have an hour, help please

Task 02: Number Guessing Game:
Write a function guess_number in functions.py, which receives a parameter num and repeatedly asks the user to guess num until the user gets it right. On each user guess the functions prints one of the following:
you win!!! – if the user enters the correct number
go big – if the user enters a value less than the num
go less – if the user enters a value greater than the num
The function does not return anything.

In the file t02.py , write a main program that asks the user to enter a number between 1 and 99 (both inclusive). The program calls guess_number if the user entered a number with within the above range, and prints Number out of range otherwise.

Sample Run:
Enter a number to guess (1 – 99): 0
Number out of range

Sample Run:
Enter a number to guess (1 – 99): 10
Guess the number: 20
go less
Guess the number: 11
go less
Guess the number: 9
go big
Guess the number: 10
you win!!!

PYTHON PLEASE

Solutions

Expert Solution

Implemented the code as per the requirement. As python is indentation specific, you may not get the formatted text while copying the code,
so I'm attaching the screenshots of the code for reference. Please make sure when you are executing the below code you have same format, especially tabs.

Please comment if any modification required or if you need any help.

code for first question: (Running Average)

=================

def average():
    sum = 0
    count = 0
    while(True):
        num = int(input("Enter a positive number: "))
        if(num<0):
            break
        sum = sum+num
        count = count+1
    print(float(sum)/count)

average()

code screenshot:

output:

code for second question: (Number Guessing Game)

===================

def guess_number(num):
    while(True):
        inp = int(input("Guess the number: "))
        if(inp<=0 or inp>=100):
            print("Number out of range")
        elif(inp<num):
            print("Go big")
        elif(inp>num):
            print("Go less")
        else:
            print("You win!!!")
            break
def main():
    inp = int(input("Enter a number to guess (1 – 99): "))
    if(inp<=0 or inp>=100):
        print("Number out of range")
    else:
        guess_number(inp)

main()

code screenshot:

output:


Related Solutions

Problem: Grading Function #Write a function called getScore() that does not take any parameter. Inside the...
Problem: Grading Function #Write a function called getScore() that does not take any parameter. Inside the function, it asks the user to input scores for 3 items (Each of the items is out of 100). The final score is calculated by 20% of item1 + 30% of item2 + 50% of item3. After calculating the final score, the function returns it to the caller. The scores are floating-point numbers. #Write another function called getLetterGrade() that takes a float parameter and...
Write a Racket function that will take a list of numbers as a parameter and return...
Write a Racket function that will take a list of numbers as a parameter and return true if they all are positive, and false otherwise. You are NOT required to check the type of elements in the list. (please do on racket language)
Write a Racket function that will take a list of numbers as a parameter and return...
Write a Racket function that will take a list of numbers as a parameter and return true if they all are positive, and false otherwise. You are NOT required to check the type of elements in the list.
In this lab we will write 3 functions: GenData: This function will take an integer parameter...
In this lab we will write 3 functions: GenData: This function will take an integer parameter and return a vector with that many random integers generated in the range of 0 and 100 (rand()%101). Seed the random number generator with 22. Mean(): This function will take a vector and return the mean. Variance(): This function will take a vector and return the population variance, as: [Sum for values[( x_i - Mean )^2]] / (number of items) In Main: Use GenData...
Write a function pretty_print, which takes one parameter that can be any type of namedtuple. It...
Write a function pretty_print, which takes one parameter that can be any type of namedtuple. It "pretty prints" the contents of the namedtuple, including both the names of its fields and their values. This is subject to a few rules. Each field and its value are displayed on one line, with the name of the field appearing first, followed by a colon and a space, followed by the value of the field converted to a string. The fields should be...
Create a function named ‘dividing’. This function will take one parameter value, which will be an...
Create a function named ‘dividing’. This function will take one parameter value, which will be an integer. For the function you will use ‘Exception Handling’ with the use of the try and except statements to try and return the results of a number divided by the parameter value given to the function. If the parameter passed to the function is the integer value of zero then your function should return ‘You tried to divide by zero!’ through the use of...
using java language only write a code Have the function StringChallenge(str) take the str string parameter...
using java language only write a code Have the function StringChallenge(str) take the str string parameter being passed and return the number of words the string contains (e.g. "Never eat shredded wheat or cake" would return 6). Words will be separated by single spaces. Examples Input: "Hello World" Output: 2 Input: "one 22 three" Output: 3 ------- you have the following code edit it to get the result mport java.util.*; import java.io.*; class Main {   public static String StringChallenge(String str)...
This function takes in a string as a parameter and prints the average number of characters...
This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per word for each sentence with 1 decimal precision (see test cases below). Assume a sentence always ends with a period (.) or when the string ends. Assume there is always a blank space character (" ") between each word. Do not count the blank spaces between words or the periods...
Please write the code in c++ Write a function with one input parameter that is a...
Please write the code in c++ Write a function with one input parameter that is a vector of strings. The function should count and return the number of strings in the vector that have either an 'x' or a 'z' character in them. For example, when the function is called, if the vector argument contains the 6 string values, "enter", "exit", "zebra", "tiger", "pizza", "zootaxy" the function should return a count of 4. ("exit", "zebra", "pizza", and "zootaxy" all have...
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT