python - Python:要求打印时不输出
问题描述
尝试从它们各自的函数中获取变量 compHand 和 playerHand 并在主函数中打印它们。输出无而不是选择的选项
def GetCompHand():
compHand= random.randint(1,3)
if compHand==1:
compHand="r"
elif compHand==2:
compHand="p"
elif compHand==3:
compHand="s"
def GetPlayerHand():
playerHand= input("Enter r, p, or s:")
if playerHand.lower() == "r":
print("You picked rock")
elif playerHand.lower()=="p":
print("You picked paper")
elif playerHand.lower()=="s":
print("You picked scissors")
else:
print("Please enter ONLY r, p, or s")
return GetPlayerHand()
def main():
pWins = 0
cWins = 0
ties = 0
compHand=GetCompHand()
playerHand=GetPlayerHand()
print(compHand)
print(playerHand)
main()
我的问题是为什么它没有输出两次而不是 r、p 或 s 用于任何一个函数。
解决方案
你的两个函数都没有'return'语句,除了你递归回同一个函数的一种情况(你真的应该尽量不要这样做,顺便说一句)。所以你的函数返回 None 因为如果你没有明确告诉它返回某些东西,它们就是定义要做的。
看起来你想添加:
return compHand
在你的第一个函数的底部,和
return playerHand
在第二个的底部
哦...欢迎来到 StackOverflow!
推荐阅读
- react-native - scrollTopTop 侦听器在组件中不起作用(React-Native)
- java - 引起:java.lang.ClassNotFoundException:org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
- kotlin - 我是否以这种方式实现适配器模式?
- javascript - 使用 PHP 刷新/更新从 mySQL 加载的数据表中的数据
- python - 执行 conda-build 找不到任何文件
- php - 子目录中的 Wordpress 实例。REST API 灾难的秘诀?
- aws-api-gateway - 如何将当前时间戳设置为 AWS API Gateway 内集成请求中的变量
- assembly - 向 -Inf 四舍五入的快速浮点到整数公式:它是如何工作的?
- php - 数据库错误?WordPress 添加新页面显示产品永久链接
- python - 我如何解决 Vs 代码中的 Import re 属性错误