高分求两个python编程问题!

1)Write a Python program that asks the user to enter a set of integer numbers and then computes and prints the average of the numbers. The program should start by printing the following message: “Do you want to enter numbers Y/N:” If the user enters “Y”, then the program asks the user to enter an integer. Following the input, the program asks if the user wants to continue or not. As long as the answer is “Y”, the program will keep asking the user to enter integers. In order to compute the average you will be accumulating the sum of the integers entered inside the while loop. When the users enters “N” the program should quit the while loop and proceed with the computation of the average of the numbers. In this processing, if the count of the numbers is 0 then the program should be able to catch the ZeroDivisionError exception i.e. you must use the try-except-else block for this segment of the program.
PLEASE NOTE:
1. You only need to check/handle exceptions when you are computing the average. For all other inputs, you may assume that the user always enters a valid value.
2. You must complete this program without using the list class or the sum and average functions.
Sample Output
Do you want to enter numbers Y/N : Y
Enter number : 5
Do you want to continue entering new numbers: Y/N : Y
Enter number : -1
Do you want to continue entering new numbers: Y/N : Y
Enter number : 2
Do you want to continue entering new numbers: Y/N : Y
Enter number : 94
Do you want to continue entering new numbers: Y/N : N
The average of 4 numbers : 25.0
Another Sample Output
Do you want to enter numbers Y/N : N
ZeroDivisionError occured. Average cannot be calculated!

2) Consider the following list of animals:
animals = ['ostrich', 'antelope', 'bear', 'monkey', 'otter', 'snake','iguana','tiger','eagle']
a) Write a Python program that uses a for loop and if statement to print the names of animals that begin with a vowel.
b) Write a Python program that uses a for loop and if statement to print the names of animals that have more than 5 letters.
python版本3.3.0

第1个回答  2013-02-05
import sys
from code import InteractiveConsole
def calc_average():
console = InteractiveConsole()
sum_value = 0;
count_value = 0;
while (True):
choice = console.raw_input('Do you want to enter numbers Y/N: ')
if (choice == 'Y' or choice == 'y'):
num = console.raw_input('Enter number: ')
sum_value += int(num)
count_value += 1
elif (choice == 'N' or choice == 'n'):
break
try:
avg_value = sum_value / count_value
print('The average of %d numbers: %.1f' % (count_value, avg_value))
except ZeroDivisionError:
print('ZeroDivisionError occured. Average cannot be calculated!')
def print_begin_with_vowel(word_list):
for v in word_list:
if (v[0] in ['a', 'e', 'i', 'o', 'u']):
print(v, end=', ')
print()
def print_more_than_5_letters(word_list):
for v in word_list:
if (len(v) > 5):
print(v, end=', ')
print()
if __name__ == '__main__':
animals = ['ostrich', 'antelope', 'bear', 'monkey', 'otter',
'snake','iguana','tiger','eagle']
a = len(sys.argv) > 1 and sys.argv[1] or '1'
if (a == '1'):
calc_average()
elif (a == '2'):
print_begin_with_vowel(animals)
elif (a == '3'):
print_more_than_5_letters(animals)
第2个回答  2013-02-06
#!/usr/bin/env python3
# encoding: utf-8
"""
1)Write a Python program that asks the user to enter a set of integer numbers and then computes and prints the average of the numbers. The program should start by printing the following message: “Do you want to enter numbers Y/N:” If the user enters “Y”, then the program asks the user to enter an integer. Following the input, the program asks if the user wants to continue or not. As long as the answer is “Y”, the program will keep asking the user to enter integers. In order to compute the average you will be accumulating the sum of the integers entered inside the while loop. When the users enters “N” the program should quit the while loop and proceed with the computation of the average of the numbers. In this processing, if the count of the numbers is 0 then the program should be able to catch the ZeroDivisionError exception i.e. you must use the try-except-else block for this segment of the program.
PLEASE NOTE:
1. You only need to check/handle exceptions when you are computing the average. For all other inputs, you may assume that the user always enters a valid value.
2. You must complete this program without using the list class or the sum and average functions.
Sample Output
Do you want to enter numbers Y/N : Y
Enter number : 5
Do you want to continue entering new numbers: Y/N : Y
Enter number : -1
Do you want to continue entering new numbers: Y/N : Y
Enter number : 2
Do you want to continue entering new numbers: Y/N : Y
Enter number : 94
Do you want to continue entering new numbers: Y/N : N
The average of 4 numbers : 25.0
Another Sample Output
Do you want to enter numbers Y/N : N
ZeroDivisionError occured. Average cannot be calculated!

2) Consider the following list of animals:
animals = ['ostrich', 'antelope', 'bear', 'monkey', 'otter', 'snake','iguana','tiger','eagle']
a) Write a Python program that uses a for loop and if statement to print the names of animals that begin with a vowel.
b) Write a Python program that uses a for loop and if statement to print the names of animals that have more than 5 letters.
"""

def getNumber():
entmsg = "Enter number: "
loopflag = input("Do you want to enter numbers y/N:")
while loopflag.upper() == 'Y':
n = input(entmsg)
try:
yield int(n)
except:
pass
loopflag = input("Do you want to enter numbers y/N:")

def run1():
count, summary = 0, 0
for n in getNumber():
count += 1
summary += n
try:
print("The average of %(cnt)d numbers: %(avg).1f" % dict(
cnt=count,
avg=summary/count
))
except ZeroDivisionError:
print("ZeroDivisionError occured. Average cannot be calculated!")

def printwithfilter(lst, filter):
for word in lst:
if filter(word):
print(word)

if __name__ == '__main__':
run1()
animals = ['ostrich', 'antelope', 'bear', 'monkey', 'otter', 'snake','iguana','tiger','eagle']
vowels = set(('a','e','i','o','u'))
print("\n")
print("animals that begin with a vowel:")
printwithfilter(animals, filter=lambda x: x[0] in vowels)
print("\n")
print("animals that have more than 5 letters:")
printwithfilter(animals, filter=lambda x: len(x)>5)
第3个回答  2013-02-05
1)
#!/bin/python
total=0;sum=0;
flag=raw_input("Do you want to enter numbers Y/N:")
if flag=="Y" :
try:
sum+=int(raw_input("Enter number:"));
total+=1;
while True:
flag=raw_input("Do you want to continue entering new numbers: Y/N:");
if flag=="Y" :
sum+=int(raw_input("Enter number:"));
total+=1;
continue;
else :
break;
except ValueError:
print "error happen"
exit(-1)
try:
average=1.0*sum/total
except ZeroDivisionError:
print "ZeroDivisionError occured.Average cannot be calculated"
exit(-1)
else:
print "The average of %d numbers :%.1f"%(total,average)

2)
animals = ['ostrich', 'antelope', 'bear', 'monkey', 'otter', 'snake','iguana','tiger','eagle']
for a in animals:
if a[0]=='a' or a[0]=='o' or a[0]=='i' or a[0]=='e' or a[0]=='u':
print a
print "....................."
for a in animals:
if len(a)>=5:
print a本回答被提问者采纳
第4个回答  2013-02-05
这个不好排版 , give me your e-mail , I can send you the answer.
相似回答