首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >随机穿越“囚徒困境”,但没有成功

随机穿越“囚徒困境”,但没有成功
EN

Stack Overflow用户
提问于 2016-11-03 01:14:36
回答 1查看 156关注 0票数 0
代码语言:javascript
运行
复制
import random

def move():   
    myScore = 0
    theirScore = 0
    Options=['b','c']
    myChoice=random.choice(Options)
    theirChoice=random.choice(Options)
    if myChoice == 'b' and theirChoice == 'b':
        myScore = myScore-250
        theirScore = theirScore-250
    if myChoice == 'b' and theirChoice == 'c':
        myScore = myScore + 100
        theirScore = theirScore - 500
    if myChoice == 'c' and theirChoice == 'b':
        myScore = myScore - 500
        theirScore = theirScore + 100
    if myChoice == 'c' and theirChoice == 'c':
        myScore = myScore + 0
        theirScore = theirScore + 0
    return myChoice
    return myScore
    return theirChoice
    return theirScore

上面是我用Python写的代码。它应该随机选择串连或背弃,用字符串'c‘和'b’表示,并相应地更改分数,然后返回所做的选择和新的分数。这是一个一次性使用的程序,没有输入。下面是我的程序中发生的事情:

代码语言:javascript
运行
复制
In: move
Out: '<function __main__.move>'

我不知道为什么我会得到这个输出,任何帮助我都很感激,谢谢。

EN

回答 1

Stack Overflow用户

发布于 2016-11-03 01:22:48

首先,为了在这里有任何输出,您首先需要提供一个变量,输出可以解包到该变量中。

代码语言:javascript
运行
复制
myChoice = move()

此外,只会运行第一个返回语句,因此您只会在函数外部收到"myChoice“的值。您可以通过在同一行上返回所有四个值来解决此问题:

代码语言:javascript
运行
复制
return (myChoice, myScore, theirChoice, theirScore)

然后,您还需要使用函数调用将这四个包都解压缩:

代码语言:javascript
运行
复制
(myChoice, myScore, theirChoice, theirScore) = move()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40385891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档