如何在函数内打印输入变量的结果
How to print the results of an input variable inside a function
这是我的代码,输入变量是 map1,用户被提升为输入文件路径,我想在用户输入位置后 print('map saved to', map1)
,但出现错误 NameError: name 'map1' is not defined
.当用户输入时,变量被定义并被赋予一个值。为什么不打印?
def question2(two):
map1 = input('where do you want to save the map? please enter filepath with image type e.g C:/map.jpg: ')
fig, ax = plt.subplots(figsize = (15,12))
filtered_buildings.plot(ax = ax, color = 'red', edgecolor = 'black',)
Highway.plot(ax = ax, color = 'black')
Tunnel.plot(ax = ax, color = 'green', alpha = 0.5, edgecolor = 'black',)
Tunnel_buffer.plot(ax = ax, facecolor='none', edgecolor = 'black',)
ctx.add_basemap(ax, source=ctx.providers.OpenStreetMap.Mapnik)
plt.title('Filtered Buildings')
plt.savefig(fname=map1, dpi=300)
if __name__ == '__main__':
do_again = 'start_string'
while not do_again in ['yes', 'y', 'Yes', 'No', 'n', 'no']:
do_again = input("Would you like to save the final map? (y/n)? ").lower()
if do_again == 'yes' or do_again == 'y' or do_again == 'Yes':
print('Saving Map...')
print('map saved to', map1)
question2()
打印语句
print('Saving Map...')
print('map saved to', map1)
应该直接在 plt.savefig(fname=map1, dpi=300)
之后和 if __name__ == '__main__':
之前
正确的代码是:
def question2():
map1 = input('where do you want to save the map? please enter filepath with image type e.g C:/map.jpg: ')
fig, ax = plt.subplots(figsize = (15,12))
filtered_buildings.plot(ax = ax, color = 'red', edgecolor = 'black',)
Highway.plot(ax = ax, color = 'black')
Tunnel.plot(ax = ax, color = 'green', alpha = 0.5, edgecolor = 'black',)
Tunnel_buffer.plot(ax = ax, facecolor='none', edgecolor = 'black',)
ctx.add_basemap(ax, source=ctx.providers.OpenStreetMap.Mapnik)
plt.title('Filtered Buildings')
plt.savefig(fname=map1, dpi=300)
print('Saving Map...')
print('map saved to', map1)
if __name__ == '__main__':
do_again = 'start_string'
while not do_again in ['yes', 'y', 'Yes', 'No', 'n', 'no']:
do_again = input("Would you like to save the final map? (y/n)? ").lower()
if do_again == 'yes' or do_again == 'y' or do_again == 'Yes':
question2()
这是我的代码,输入变量是 map1,用户被提升为输入文件路径,我想在用户输入位置后 print('map saved to', map1)
,但出现错误 NameError: name 'map1' is not defined
.当用户输入时,变量被定义并被赋予一个值。为什么不打印?
def question2(two):
map1 = input('where do you want to save the map? please enter filepath with image type e.g C:/map.jpg: ')
fig, ax = plt.subplots(figsize = (15,12))
filtered_buildings.plot(ax = ax, color = 'red', edgecolor = 'black',)
Highway.plot(ax = ax, color = 'black')
Tunnel.plot(ax = ax, color = 'green', alpha = 0.5, edgecolor = 'black',)
Tunnel_buffer.plot(ax = ax, facecolor='none', edgecolor = 'black',)
ctx.add_basemap(ax, source=ctx.providers.OpenStreetMap.Mapnik)
plt.title('Filtered Buildings')
plt.savefig(fname=map1, dpi=300)
if __name__ == '__main__':
do_again = 'start_string'
while not do_again in ['yes', 'y', 'Yes', 'No', 'n', 'no']:
do_again = input("Would you like to save the final map? (y/n)? ").lower()
if do_again == 'yes' or do_again == 'y' or do_again == 'Yes':
print('Saving Map...')
print('map saved to', map1)
question2()
打印语句
print('Saving Map...')
print('map saved to', map1)
应该直接在 plt.savefig(fname=map1, dpi=300)
之后和 if __name__ == '__main__':
正确的代码是:
def question2():
map1 = input('where do you want to save the map? please enter filepath with image type e.g C:/map.jpg: ')
fig, ax = plt.subplots(figsize = (15,12))
filtered_buildings.plot(ax = ax, color = 'red', edgecolor = 'black',)
Highway.plot(ax = ax, color = 'black')
Tunnel.plot(ax = ax, color = 'green', alpha = 0.5, edgecolor = 'black',)
Tunnel_buffer.plot(ax = ax, facecolor='none', edgecolor = 'black',)
ctx.add_basemap(ax, source=ctx.providers.OpenStreetMap.Mapnik)
plt.title('Filtered Buildings')
plt.savefig(fname=map1, dpi=300)
print('Saving Map...')
print('map saved to', map1)
if __name__ == '__main__':
do_again = 'start_string'
while not do_again in ['yes', 'y', 'Yes', 'No', 'n', 'no']:
do_again = input("Would you like to save the final map? (y/n)? ").lower()
if do_again == 'yes' or do_again == 'y' or do_again == 'Yes':
question2()