函数没有返回正确的值
function not returning the correct value
我的功能有问题,也没有 return输入正确的值。函数 return 文件路径取决于用户选择。我检查了选择输入,但无论选择什么我只输入 return 一个值。
def pick_sql_version(): # might be redundant
sql_year = ['2008', '2014', '2016']
for i, val in enumerate(sql_year):
print(i + 1, " SQL Server Management Studio ", val)
sql_version = input("Pick the SSMS version: ")
# depending on the sql version path might vary
if sql_version == 1: # SSMS 2008
path = '' # which value to use?
elif sql_version == 2: # SSMS 2014
print (sql_version)
path = '"C:/Program Files (x86)/Microsoft SQL Server/120/Tools/Binn/ManagementStudio/Ssms.exe"'
print (path)
else: # SSMS 2016
print(sql_version)
path = '"C:/Program Files (x86)/Microsoft SQL Server/130/Tools/Binn/ManagementStudio/Ssms.exe"'
print(path)
return path
with input("Pick the SSMS version: ")
你正在读取你输入的字符串,这意味着如果你输入 1
,你会得到 '1'
,如果你输入 2
你会得到 '2'
.
您应该通过检查您的输入是 '1'
还是 '2'
来解决这个问题(您也可以按照某些评论中的建议将您的输入转换为 int
,但是我认为它只会给您带来问题:例如,如果您输入无法转换为 int
):
的内容,它将引发错误
if sql_version == '1': # SSMS 2008
path = '' # which value to use?
elif sql_version == '2': # SSMS 2014
print (sql_version)
忍不住再写一个版本给你。
使用 dictionaries
的强大功能,您可以使代码更具可读性。
让我们将键 "1"
与值 "120"
以及键 "2"
与值 "130"
连接起来。我们使用这些插入到使用 str.format()
的路径中。
最后,通过使用 dictionary.get()
,我们可以传递一个默认值,如果用户输入无效(不是 1
或 2
),它将使用值 120。
def pick_sql_version(): # might be redundant
# create an easy list to pick from
sql_year = ['2008', '2014', '2016']
for i, val in enumerate(sql_year, start= 1):
print("{} SQL Server Management Studio {}".format(i, val))
sql_version = input("Pick the SSMS version: ")
# depending on the sql version path might vary
version_d = {
'1': '120',
'2': '130'
}
# If invalid path is chosen fall back to default (120)
t='C:/Program Files (x86)/Microsoft SQL Server/{}/Tools/Binn/ManagementStudio/Ssms.exe'
path = t.format(version_d.get(sql_version, '120'))
return path
我的功能有问题,也没有 return输入正确的值。函数 return 文件路径取决于用户选择。我检查了选择输入,但无论选择什么我只输入 return 一个值。
def pick_sql_version(): # might be redundant
sql_year = ['2008', '2014', '2016']
for i, val in enumerate(sql_year):
print(i + 1, " SQL Server Management Studio ", val)
sql_version = input("Pick the SSMS version: ")
# depending on the sql version path might vary
if sql_version == 1: # SSMS 2008
path = '' # which value to use?
elif sql_version == 2: # SSMS 2014
print (sql_version)
path = '"C:/Program Files (x86)/Microsoft SQL Server/120/Tools/Binn/ManagementStudio/Ssms.exe"'
print (path)
else: # SSMS 2016
print(sql_version)
path = '"C:/Program Files (x86)/Microsoft SQL Server/130/Tools/Binn/ManagementStudio/Ssms.exe"'
print(path)
return path
with input("Pick the SSMS version: ")
你正在读取你输入的字符串,这意味着如果你输入 1
,你会得到 '1'
,如果你输入 2
你会得到 '2'
.
您应该通过检查您的输入是 '1'
还是 '2'
来解决这个问题(您也可以按照某些评论中的建议将您的输入转换为 int
,但是我认为它只会给您带来问题:例如,如果您输入无法转换为 int
):
if sql_version == '1': # SSMS 2008
path = '' # which value to use?
elif sql_version == '2': # SSMS 2014
print (sql_version)
忍不住再写一个版本给你。
使用 dictionaries
的强大功能,您可以使代码更具可读性。
让我们将键 "1"
与值 "120"
以及键 "2"
与值 "130"
连接起来。我们使用这些插入到使用 str.format()
的路径中。
最后,通过使用 dictionary.get()
,我们可以传递一个默认值,如果用户输入无效(不是 1
或 2
),它将使用值 120。
def pick_sql_version(): # might be redundant
# create an easy list to pick from
sql_year = ['2008', '2014', '2016']
for i, val in enumerate(sql_year, start= 1):
print("{} SQL Server Management Studio {}".format(i, val))
sql_version = input("Pick the SSMS version: ")
# depending on the sql version path might vary
version_d = {
'1': '120',
'2': '130'
}
# If invalid path is chosen fall back to default (120)
t='C:/Program Files (x86)/Microsoft SQL Server/{}/Tools/Binn/ManagementStudio/Ssms.exe'
path = t.format(version_d.get(sql_version, '120'))
return path