python - Python: getting variable from user function
问题描述
im trying to make a simple text game for a school project and also make the code somewhat clean therefore I started using functions and classes.
The problem is im stuck on trying to access a variable from a function that asks the user for their username.
Here is my code
def playername():
print("What is your name?")
playername = input()
how can I access the playername variable without running the whole function again so once I run it at the start and get the username I can use that same input username later on in the code?
解决方案
store it as a variable outside of function. for example string playerName = playername() and add return playername to the end of the function "playername"
def playername():
print("What is your name?")
playername = input()
return playername
def main():
playerName = playername()
print(playerName)
main()
推荐阅读
- sql - 当源数据库中不存在表时是否可以传递空值
- npm - Sonatype - Nexus Repo NPM 包 URL 不正确
- c# - 使用 Entity Framework Core 获取多个表
- regex - 在本机反应中使用正则表达式验证电话提交
- semantic-ui - 语义 ui - 强制更新进度条
- python - 如何使用 Python 在一组点上绘制多边形(部分向内弯曲)边缘?
- c - 堆损坏问题 - C
- c# - 错误 SignalR 连接到网络负载平衡 (NLB) net::ERR_SSL_VERSION_INTERFERENCE?
- powershell - 让证书在一个月或更短的时间内到期
- http-status-code-404 - 开发和生产中如何在 NextJS 中渲染 404 页面?