Chapter 6 You finally can make a program that uses raw_input. Note NOT input, in
ID: 3627765 • Letter: C
Question
Chapter 6You finally can make a program that uses raw_input. Note NOT input, input is to get python commands to process, whereas raw_input is to get strings.
1. Write a program that prompts the user to type in a sentence. Then get that sentence and split what they typed into a word array. Then using a for loop, print out the words one per line.
2. Using formatted output, read in a file of numbers, then print out the numbers - 3 per line as numbers that are in neat columns by using a formatting string to make the numbers 8 characters wide with 2 decimal places.
3. Using the same program as above, make the output go to a file called output.txt
Explanation / Answer
1.string = raw_input('Please enter a sentence: ')
array = string.split(" ");
for s in array:
print s
2. textfile = open("Numbers.txt")
nums = textfile.read().split(' ')
for i in range(len(nums)):
print "%7.2f" % float(nums[i]), #the comma is so that it doesn't go to the next line
if i%3==2:
print ' ' #this will go to the next line every three numbers
3. textfile = open("Numbers.txt")
outputfile = open("Output.txt", 'r+')
nums = textfile.read().split(' ')
for i in range(len(nums)):
outputfile.write("%7.2f" % float(nums[i]))
if i%3==2:
outputfile.write(" ")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.