执行用户定义的python次数的函数

时间:2015-05-03 09:22:35

标签: python for-loop return user-input execute

我有两个功能,一个要求用户输入三个输入,另一个要求用户输入一个数字。我想创建一个for循环,以便第一个函数运行用户在第二个函数中输入的次数。

这是我的代码:

def get_user_input():
    name = raw_input("What is the name?")
    bas_lat = float(input("What is the latitude?"))
    bas_long = float(input("What is the longitude?"))
    print name, "has a latitude of ", bas_lat, 
    print "degrees and a longitude of ", bas_long

def get_number_stations():
    number_of_stations = float(input("How many stations are there?"))
    print "There are ", number_of_stations, 
    print "stations to have their distance from"
    print "from the headquarters determined"
    return number_of_stations

main()

1 个答案:

答案 0 :(得分:0)

您可以返回数字并将其作为参数传递给函数,在函数中进行循环:

def get_user_input(t):
    for _ in xrange(t):
        name = raw_input("What is the name?")
        # raw_input not input
        bas_lat = float(raw_input("What is the latitude?"))
        bas_long = float(raw_input("What is the longitude?"))
        print "{} has a latitude and longitude of ({},{})".format(name, bas_lat, bas_long)


def get_number_stations():
    # get int to pass to range
    number_of_stations = int(raw_input("How many stations are there?"))
    print "There are {} stations to have their distance " \
          "from from the headquarters determined.".format(number_of_stations,) 
    return number_of_stations


# get number of stations/times to loop returned from get_number_stations
t = get_number_stations()
# pass t to get_user_input
get_user_input(t)

你需要int作为范围,当你想要一个浮点数,int等时你也应该投射raw_input。不要使用input

如果要存储数据,可以使用dict,使用名称作为键,使用元组作为存储坐标的值:

def get_user_input(t):
    data = {}
    for _ in xrange(t):
        name = raw_input("What is the name?")
        bas_lat = float(raw_input("What is the latitude?"))
        bas_long = float(raw_input("What is the longitude?"))
        print "{} has a latitude and longitude of ({},{})".format(name, bas_lat, bas_long)
        data[name] = (bas_lat,bas_long)
    return data

您还可能希望try/except捕获无法转换为浮动的无效输入:

def get_user_input(t):
    data = {}
    for _ in xrange(t):
        name = raw_input("What is the name?")
        while True:
            try:
                bas_lat = float(raw_input("What is the latitude?"))
                bas_long = float(raw_input("What is the longitude?"))
                print "{} has a latitude and longitude of {},{}".format(bas_lat, bas_long)g
                # if all data was valid break
                break
            except ValueError:
                # else we got data that could not be cast
                # so print message and aska again
                print("Invald input")
                continue
        data[name] = (bas_lat,bas_long)
    return data

将验证作为一个函数validate可能会更好一点,它将一个字符串作为参数,一个类型_type可以验证lat和long输入以及站点的数量: / p>

def validate(s ,_type):
    while True:
        try:
            return _type(raw_input(s))
        except ValueError:
            print("Invalid input")


def get_user_input(t):
    data = {}
    for _ in xrange(t):
        name = raw_input("What is the name? ")
        bas_lat = validate("What is the latitude? ", float)
        bas_long = validate("What is the longitude? ", float)
        print "{} has a latitude and longitude of ({},{})".format(name, bas_lat, bas_long)
        data[name] = (bas_lat, bas_long)
    return data


def get_number_stations():
    number_of_stations = validate("How many stations are there? ",int)
    print "There are {} stations to have their distance " \
          "from from the headquarters determined.".format(number_of_stations)
    return number_of_stations



t = get_number_stations()

get_user_input(t)

您可以在循环中重复调用get_user_input,但由于您没有将任何新信息传递给get_user_input,这将毫无意义。

输出:

How many stations are there?s
Invalid input
How many stations are there?2
There are 2 stations to have their distance from from the headquarters determined.
What is the name? foo
What is the latitude? 0.123
What is the longitude? 1.12s
Invalid input
What is the longitude? 1.345
foo has a latitude and longitude of (0.123,1.345)
What is the name? bar
What is the latitude? 1.234
What is the longitude? 2.345
bar has a latitude and longitude of (1.234,2.345)