您可以返回数字并将其作为参数传递给函数,在函数中进行循环:
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 来表示范围,当你想要一个浮点数、整数等时,你还应该转换raw_input。不要使用input。
如果要存储数据,可以使用字典,使用名称作为键,元组作为存储坐标的值:
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,它可以验证纬度和经度输入以及站数:
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)