Question

In: Computer Science

Part A: CHALLENGE ACTIVITY 7.6.2: For loop. Printing a list. Write the Python code to accomplish...

Part A: CHALLENGE ACTIVITY 7.6.2: For loop. Printing a list.

Write the Python code to accomplish the following tasks:

• Input a string from the keyboard that represents three or more stock prices separated by spaces

o Example: ‘34.62 76.30 85.05’

• Convert the string into a Python list of floats

• Print each price in the list on a separate line with a dollar sign ($) as the first character on each line

• Print the output such that the decimal points are aligned vertically even if the user enters values such as ‘1.23 23.45 345.56’

Part B: Based on CHALLENGE ACTIVITY 7.7.3: Hourly temperature reporting.

Write the Python code to perform the following tasks:

• Use the same list of floats from part A

• Take as input from the user a two-character string from the keyboard to use as a separator during printing

o Example: user should enter something like ‘->’, ‘=>’, ‘<>’

• Write a loop that will print all elements of the list to the same line on the display

o Each element should be separated from other elements by the user-chosen separator characters including a space on each side. The space and separator should not be printed after the last number.

Example output (using input 1.23 23.45 345.56 and ->):

Enter three or more stock prices separated by spaces: 1.23 23.45 345.56

$ 1.23

$ 23.45

$ 345.56

Enter a two-character separator: -> 1.23 -> 23.45 -> 345.56

Solutions

Expert Solution

SOLUTION-

1)

CODE-

s = input('Enter stock prices separated by spaces: ')
prices = [float(v) for v in s.split()]  
for p in prices:
   print('${:7.2f}'.format(p))

OUTPUT-

2)

CODE-

string = input('Enter three or more stock prices seperateed by spaces : ')
lst =string.split()

for i in lst:
    print('$',i)
  
sep=input('Enter a two character sepearator: ')
for i in range(len(lst)):
    print(lst[i], end=' ')
    if i != len(lst) - 1:
        print(sep, end=' ')

OUTPUT-

IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

Write a python code which prints triangle of stars using a loop ( for loop )...
Write a python code which prints triangle of stars using a loop ( for loop ) Remember what 5 * "*" does The number of lines of output should be determined by the user. For example, if the user enters 3, your output should be: * ** *** If the user enters 6, the output should be: * ** *** **** ***** ****** You do NOT need to check for valid input in this program. You may assume the user...
Code in python Write a while loop code where it always starts form 2. Then it...
Code in python Write a while loop code where it always starts form 2. Then it randomly chooses a number from 1-4. If the number 4 is hit then it will write “TP” if the number 1 is hit then it will write”SL”. It will rerun the program every time the numbers 1 and 5 are hit. The code should also output every single number that is randomly chosen. 2 of the same numbers can't be chosen back to back...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that calculates the amount of money a person would earn over a period of time if his or her salary is 1 penny for the first day, 2 pennies for the second day, 4 pennies for the third day, and continues to double each day. 1.      The program should ask the user for the number of days the employee worked. 2.      Display a table showing the salary...
Important: please use python. Using while loop, write python code to print the times table (from...
Important: please use python. Using while loop, write python code to print the times table (from 0 to 20, incremented by 2) for number 5. Add asterisks (****) so the output looks exactly as shown below.   Please send the code and the output of the program. ****************************************************************** This Program Shows Times Table for Number 5 (from 0 to 20) Incremented by 2 * ****************************************************************** 0 x 5 = 0 2 x 5 = 10 4 x 5 = 20 6...
Write a Python loop that goes through the list and prints each string where the string...
Write a Python loop that goes through the list and prints each string where the string length is three or more and the first and last characters of the strings are the same. Test your code on the following three versions of the list examples: examples = ['abab', 'xyz', 'aa', 'x', 'bcb'] examples = ['', 'x', 'xy', 'xyx', 'xx'] examples = ['aaa', 'be', 'abc', 'hello'].
(Python) a) Using the the code below write a function that takes the list xs as...
(Python) a) Using the the code below write a function that takes the list xs as input, divides it into nss = ns/nrs chunks (where nrs is an integer input parameter), computes the mean and standard deviation s (square root of the variance) of the numbers in each chunk and saves them in two lists of length nss and return these upon finishing. Hint: list slicing capabilities can be useful in implementing this function. from random import random as rnd...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the functions defined below. You are expected to re-use these functions in implementing other functions in the file. Include a triple-quoted string at the bottom displaying your output. Here is the starter outline for the homework: a. def count_character(text, char): """ Count the number of times a character occurs in some text. Do not use the count() method. """ return 0 b. def count_sentences(text): """...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the functions defined below. You are expected to re-use these functions in implementing other functions in the file. Include a triple-quoted string at the bottom displaying your output. Here is the starter outline for the homework: g. def big_words(text, min_length=10): """ Return a list of big words whose length is at least min_length """ return [] h. def common_words(text, min_frequency=10): """ Return words occurring at...
1. Write Python code to - (a) ask user to enter a list of animals, one...
1. Write Python code to - (a) ask user to enter a list of animals, one item at a time, until “done” to terminate input (b) randomly display a selected item from the list, starting with “I like ______”. When run, the output should look something like this: Enter an item; 'done' when finished: cats Enter an item; 'done' when finished: dogs Enter an item; 'done' when finished: guinea pigs Enter an item; 'done' when finished: done You are done...
(Python) This is my code for printing a roster for a team. When I print to...
(Python) This is my code for printing a roster for a team. When I print to the console, it makes the first player's name show up as number 2, and it says [] (its just blank for 1). How can I fix that so the first player's name is 1, not skipping 1 and going to 2. def file_to_dictionary(rosterFile): myDictionary={} myDict=[]    with open(rosterFile,'r') as f: for line in f:    (num,first,last,position)=line.split() myDictionary[num]= myDict myDict=[first, last, position] print (myDictionary) return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT