Write Python code for creating an empty array, then use a loop to generate 5 random values
Question:
Write Python code for creating an empty array, then use a loop to generate 5 random values and insert them into the array, and finally use another loop to read the values in the array and print them out. The steps for your code are:
- Create empty array
- Create a for loop that repeats 5 times
- Inside the for loop, generate a random number and append it to the array
- Create another for loop that prints the elements of the array.
P2.
Write a Python program that keeps reading in names from the user, until the user enters 0. Once the user enters 0, you should print out all the information that was entered by the user. Use this case to test your program:
- Input:
John
Marcel
Daisy
Samantha
Nelson
Deborah
0
================
Output:
John
Marcel
Daisy
Samantha
Nelson
Deborah
Hints:
- Create an array Names to hold the input names.
- Use the Names.append(x) function to add a new name to the Names array. The input x to the append(x) function represents a name that should be added to the Names array.
- Once the user enters 0, stop reading in data, and start printing out all the data from the Names array in the same order that they were entered.
P3.
Write a Python program that reads in 5 numeric values, stores them in an array, and then prints them in the reverse order that they were entered. Use this case to test your program:
- Input:
10
27
23
12
81
================
Output:
81
12
23
27
10
Hints:
- Create an array Numbers to hold the input numbers.
- Use a FOR loop with a counter that goes from 1 to 5 in order to read in 5 numeric values. Use the append() function to add numbers to the array.
- Use another FOR loop to print the 5 numeric values in reverse order. The loop counter should be used as the index to access the values in the Numbers array. In order to print the numbers in reverse order, the loop counter should start at 4 and goes down to 0. This can be done by reversing the range of the FOR loop as follows:
- for c in reversed( range (5) ):
P4.
Write a Python function called string_times(msg, n) that takes two input parameters:
- String msg
- Integer Number n
And it prints out the msg as many times as n. Here are two examples for how the function should work:
- The function call string_times('Hi',2) should print out 'HiHi'
- The function call string_times('Hello',3) should print out 'HelloHelloHello'
You MUST use a while loop in your solution.
Hints:
- Create a counter j and set it equal to 0.
- Then create a while loop with the condition ( j < n).
- Inside the while loop, print the msg once, then increment j by 1.
P5.
John has a weather station in his house. He has been keeping track of the fastest wind speed for each day for two weeks. He would like to know the average wind speed over the two weeks, the days on which the highest wind speed and the lowest wind speed were recorded, and the difference between the highest wind speed recorded and each day’s average wind speed.
Develop a solution for this problem that includes:
- PAC
- Algorithm
- Python Code