繁体   English   中英

如何将参数从函数返回到另一个函数?

[英]How do I return an argument from a function into another?

我目前正在尝试创建一种扑克游戏,如果您输入一个整数(要玩多少轮),然后让人类和计算机根据您输入的整数生成卡片并决定每轮谁获胜。 我的问题是如何将一个参数从一个函数返回到另一个函数? 我在函数poker()中有 x, y ,我想用它在函数totalwins()进行比较。

import random

Deck = ['Diamond', 'Hearts', 'Jack', 'Spade']
Number = [1,2,3,4,5,6,7,8,9,10,11,12]


def poker(x,y):
    Times = int(input('How many rounds would you like to play: '))
    
    while Times > 0:
        Human = random.choice(Deck)
        Human_value = random.choice(Number)
        Computer = random.choice(Deck)
        Computer_value = random.choice(Number)
        
    
        print('Human: ', Human , Human_value)
        print('Computer: ', Computer, Computer_value)
        
        if Human_value > Computer_value:
            print('Human wins')
            x +=1
        elif Human_value < Computer_value:
            print('Computer wins')
            y += 1
        else:
            print('Tie')

        Times -= 1
poker(0,0)


def totalwins():
    
    if x > y:
        print('Human wins!')
    else:
        print('Computer wins!')
totalwins()

代码很乱,但这里是如何将返回值传递给下一个函数


Deck = ['Diamond', 'Hearts', 'Jack', 'Spade']
Number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]


def poker():

    # instead of passing x, y as 0 i am initializing inside
    x = 0
    y = 0

    Times = int(input('How many rounds would you like to play: '))

    while Times > 0:
        Human = random.choice(Deck)
        Human_value = random.choice(Number)
        Computer = random.choice(Deck)
        Computer_value = random.choice(Number)

        print('Human: ', Human, Human_value)
        print('Computer: ', Computer, Computer_value)

        if Human_value > Computer_value:
            print('Human wins')
            x += 1
        elif Human_value < Computer_value:
            print('Computer wins')
            y += 1
        else:
            print('Tie')

        Times -= 1

        # returning x, y to the next funcction
        return x, y


def totalwins():
    x, y = poker()
    if x > y:
        print('Human wins!')
    else:
        print('Computer wins!')


totalwins()

也许是扑克结束时的回报声明? 就像是:

return (x,y)

然后在调用 poker 时捕获值:

x,y = poker(0,0)

然后更改 totalwins 的参数列表以接受xy并调用:

totalwins(x,y)

或者你可以将xy全局,但我不会走那条路

一旦 while 循环在poker函数内部结束total_wins(x,y)您就调用函数total_wins(x,y)

由于变量xypoker函数内递增,因此应在其中调用函数total_wins ,如下面更正后的代码所示:

import random
Deck = ['Diamond', 'Hearts', 'Jack', 'Spade']
Number = [1,2,3,4,5,6,7,8,9,10,11,12]

def totalwins(x,y):
    if x == y:
      print('It\'s a tie!')
    elif x > y:
        print('Human wins!')
    else:
        print('Computer wins!')

def poker(x,y):
    Times = int(input('How many rounds would you like to play: '))
    
    while Times > 0:
        Human = random.choice(Deck)
        Human_value = random.choice(Number)
        Computer = random.choice(Deck)
        Computer_value = random.choice(Number)
    
        print('Human: ', Human , Human_value)
        print('Computer: ', Computer, Computer_value)
        
        if Human_value > Computer_value:
            print('Human wins')
            x +=1
        elif Human_value < Computer_value:
            print('Computer wins')
            y += 1
        else:
            print('Tie')

        Times -= 1
    totalwins(x,y)
poker(0,0)
  • PS:在total_wins函数中也增加了Tie的条件。
  • 注意:确保将 total_wins 函数放在 poker 函数之前。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM