6.\x05Write a program that will allow the user to input a se

1个回答

  • #!/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 = floo