java调用python函数没有返回数据_Python:从函数返回时没有值

本文档描述了一个程序问题,该程序在Java中调用Python函数以获取玩家得分。当输入的得分超过5时,playerscore()函数会重新启动,导致返回None。在playercreator()函数中,尝试打印score时遇到错误,因为函数在错误输入情况下未返回有效值。解决方案在于确保playerscore()在所有路径上都有返回值。
摘要由CSDN通过智能技术生成

我正在编写一个程序,将3名玩家及其分数存储在列表中,然后在最后打印出来 . 简单的东西真的,但是我试图从一个名为playerscore()的函数中调用玩家得分的值,这会阻止你输入得分> 5 .

当你使用正确的值运行它时,这工作正常,但是如果输入的值不正确> 5则会再次启动playercore函数并允许输入新值但返回“None”

teamlist = []

def playercreator():

counter=0

while counter < 3:

name = playername()

score = playerscore()

print(score) #check - this one goes wrong when returned after the if/else in playerscore()

teamlist.append(name+" "+str(score))

counter = counter + 1

def playername():

name=input("What name do you want for your player?\n")

return (name)

def playerscore():

global teamtotal

score=input("input score?\n")

print(score) #check

if int(score)>5:

print("Your attack score must be between 0 and 5")

print(score) #check

playerscore()

else:

return int(score)

playercreator()

for x in teamlist:

print(x)

例如,这些是输入和输出:

What name do you want for your player?

p1

input score?

3

What name do you want for your player?

p2

input score?

6

Your attack score must be between 0 and 5

input score?

5

What name do you want for your player?

p3

input score?

2

p1 3

p2 None

p3 2

我觉得有一些显而易见的东西让我失踪 . 谁能指出我正确的方向?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值