6.\x05Write a program that will allow the user to input a series of integers,inputting 0 to stop.The program will then print out the sum of those integers.7.\x05Write a program that will allow the user to input a series of words,inputting "exit" to s

来源:学生作业帮助网 编辑:六六作业网 时间:2024/04/29 04:40:58
6.\x05Writeaprogramthatwillallowtheusertoinputaseriesofintegers,inputting0tostop.Theprogramwillthenp

6.\x05Write a program that will allow the user to input a series of integers,inputting 0 to stop.The program will then print out the sum of those integers.7.\x05Write a program that will allow the user to input a series of words,inputting "exit" to s
6.\x05Write a program that will allow the user to input a series of integers,
inputting 0 to stop.The program will then print out the sum of those integers.
7.\x05Write a program that will allow the user to input a series of words,
inputting "exit" to stop.Output the number of words that were entered.
9.\x05A building has one elevator and ten floors.Ask the user what floor they
want to go to.Output if they are going up or down and how many floors away it
is.The elevator should continually run until an invalid floor is entered.
Sample:
\x05You are on floor 1.
What floor would you like to go to?5
\x05You will be going up 4 floors.
\x05You are on floor 5.
What floor would you like to go to?3
\x05You will be going down 2 floors.
\x05You are on floor 3.
What floor would you like to go to?3
\x05You are already on floor 3.
\x05You are on floor 3.
What floor would you like to go to?11
There is no floor 11.Please exit the elevator.
大家如果需要翻译的话请私信我,不过我相信都很简单:)
3小时之内解答出来的话 有奖励50分

6.\x05Write a program that will allow the user to input a series of integers,inputting 0 to stop.The program will then print out the sum of those integers.7.\x05Write a program that will allow the user to input a series of words,inputting "exit" to s
#!/usr/bin/env python
# coding: utf-8
#
# filename: baidu_qa.py
# author: Tim Wang
# date: Feb., 2014


def getInteger(prompt):
    while 1:
        try:
            return int(raw_input(prompt))
        except:
            pass


def sumofinputinteger():
    """
    Write a program that will allow the user to input a series of integers,
    inputting 0 to stop. The program will then print out the sum of those integers.
    """
    collector = []
    while 1:
        num = getInteger('Enter a number ("0" for stop): ')
        if num == 0:
            break
        collector.append(num)
    print "Sum of those integer(s): %d" % sum(collector)


def countofinputwords():
    """
    Write a program that will allow the user to input a series of words,
    inputting "exit" to stop. Output the number of words that were entered.
    """
    collector = []
    while 1:
        word = raw_input('Enter a word ("exit" for stop): ')
        if word == "exit":
            break
        collector.append(word)
    print "the number of words that were entered: %d" % len(collector)



def elevator():
    """
    A building has one elevator and ten floors. 
    Ask the user what floor they want to go to. 
    Output if they are going up or down and how many floors away it is. 
    The elevator should continually run until an invalid floor is entered.
    """
    def _getfloor():
        while 1:
            try:
                floor = int(raw_input("what floor you want to go to (1-10)? "))
                if 1 <= floor <= 10:
                    return floor
                else:
                    return None
            except:
                continue
    
    current = 1
    while 1:
        floor = _getfloor()
        if floor is None:
            break
        if floor < current:
            print "Going down"
        elif current < floor:
            print "Going up"
        else:
            print "You are here"
        current = floor