python - 如何在另一个函数中使用一个函数收集的数据
问题描述
所以基本上我是一个纯粹的初学者,试图做这个学校作业。
def function1():
while True:
choice1 = (input("Random text")).lower()
if choice1 in ('option1', 'option2'):
break
else:
print("Even more random text")
return choice1
def function2():
rightEvenOdd = 'random word'
if choice1 == rightEvenOdd:
print('Yay')
else:
print('Not yay')
NameError: name 'choice1' is not defined
第二个函数无法访问锁定在第一个函数中的变量,我不知道如何让它访问它。请帮忙。
解决方案
您可以将函数 1 调用到函数 2 中,因为您已经从 function1 返回了 choice1 值。例如 :
def function2():
choice1 = function1()
rightEvenOdd = 'random word'
if choice1 == rightEvenOdd:
print('Yay')
else:
print('Not yay')
推荐阅读
- css - 使用 Vuejs3 CLI 的路由器哈希链接
- javascript - 如何在没有外部模块的情况下使用 node.js 将授权标头添加到获取请求
- protocol-buffers - gRPC-Gateway 转码中的自定义 URL 参数
- html - Firefox 密码下拉悬停事件使悬停离开菜单
- clickhouse - clickhouse中插入查询中的正则表达式
- javascript - 为什么console.logging在函数外按预期工作,但在一个函数内这样做会将我的值标记为未定义?
- android - Android 11 IjkMediaPlayer 和 GiraffePlayer2 问题
- mysql - cmake:“cxx11”不是公认的标准
- memory-management - Segmentation VS Paging 性能差异及在OS中的应用
- qt - 如何向 Qml 立方体实体添加 6 个自定义纹理?