In: Computer Science
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
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: