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)
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...
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...
USE PYTHON : # Problem Set 04: - Write a function to seek for all even...
USE PYTHON : # Problem Set 04: - Write a function to seek for all even numbers and odd numbers in the middle of two number A and B. Print even and odd numbers in 1 and 2020 (including both these two numbers) # Problem Set 05: - A website requests an user to input his account password. - Write a program to examize the validity of the password. - The valid password must consists of: - At least 1...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the #file and return the contents.# # # - If the contents of the file can be interpreted as # an integer, return the contents as an integer. # - Otherwise, if the contents of the file can be # interpreted as a float, return the contents as a # float. # - Otherwise, return the contents of the file as a # string. #...
Write a function for checking the speed of drivers. This function should have one parameter: speed....
Write a function for checking the speed of drivers. This function should have one parameter: speed. 1. If speed is less than 70, it should print “Ok”. 2. Otherwise, for every 5km above the speed limit (70), it should give the driver one demerit point and print the total number of demerit points. For example, if the speed is 80, it should print: "Points: 2". 3. If the driver gets more than 12 points, the function should print: “License suspended”...
When a function can take another function as a parameter, how can type-checking occur? (Programming languages)
When a function can take another function as a parameter, how can type-checking occur? (Programming languages)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT