简体   繁体   English

如何保存随机输出以在 python 的另一个函数中使用它

[英]How can I save the random output to use it in another function in python

I am new to python3 I am trying very hard to add the output of the function, is there any way that I can save an output of an random integer so that i can altogether add it please help me.我是python3的新手,我非常努力地尝试添​​加函数的输出,有什么方法可以保存随机整数的输出,以便我可以完全添加它,请帮助我。

def dice():
    import random
    rollball = int(random.uniform(1, 6))
    return (rollball)

def dice2():
    import random
    rollball = int(random.uniform(1, 6))
    return (rollball)

print(dice())
input("you have 10 chances left")
print(dice2())
input("you have 9 chances left")
print(dice() + dice2())
#i want to print this last function but by adding only this first and second nothing else

Use a variable or set a global variable for it使用变量或为其设置全局变量

import random
def dice():
    rollball = int(random.uniform(1, 6))
    return (rollball)

def dice2():
    rollball = int(random.uniform(1, 6))
    return (rollball)

roll1 = dice()
print(roll1)
input("you have 10 chances left")
roll2 = dice2()
print(roll2)
input("you have 9 chances left")
print(roll1 + roll2)
#i want to print this last function

Or或者

use

import random
roll1 = 0
roll2 = 0
def dice():
    global roll1
    rollball = int(random.uniform(1, 6))
    roll1 = rollball
    return (rollball)

def dice2():
    global roll2
    rollball = int(random.uniform(1, 6))
    roll2 = rollball
    return (rollball)

print(dice())
input("you have 10 chances left")
print(dice2())
input("you have 9 chances left")
print(roll1 + roll2)
#i want to print this last function

This should provide you with a basic example.这应该为您提供一个基本示例。 I would spend some time searching out some resources to help you get started with the basics of python programming;我会花一些时间搜索一些资源来帮助您开始学习 Python 编程的基础知识; There are an enumerable amount.有无数的数量。 You'll find it much easier to progress if you understand how to do the little things first.如果您了解如何先做小事,您会发现进步会容易得多。

#!/usr/bin/env python3.9
"""
Basic Function Usage
"""
from random import uniform
from typing import NoReturn


def dice() -> int:
    """A dice function
    
    Returns:
        (int)
    """
    return int(uniform(1, 6))


def dice2() -> int:
    """Another dice function

    Returns:
        (int)
    """
    return int(uniform(1, 6))


def main() -> NoReturn:
    """ Main

    Returns:
        (NoReturn)"""
    d = dice()
    d2 = dice2()

    print('Dice: ', d)
    print('Dice2:', d2)
    print(f'Dice Total: {d + d2}')


if __name__ == '__main__':
    try:
        main()
    except Exception as excp:
        from sys import exc_info
        from traceback import print_exception

        print_exception(*exc_info())

Output:输出:

Dice:  1
Dice2: 3
Dice Total: 4

Some possibly helpful resources:一些可能有用的资源:

Real Python - Defining Your Own Python Function真正的 Python - 定义你自己的 Python 函数

Real Python - f-Strings真正的 Python - f-Strings

Official Python Documentation官方 Python 文档

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

相关问题 我如何将输出保存为python中的float - how can I save output as a float in python 如何从Java保存Open NLP解析器输出,以便我可以在Python中使用它? - How can I save the Open NLP parser output from Java, so that I can use it in Python? 我如何使用带有变量的随机函数? - How can i use the random function with variables? 如何在python的另一个应用程序中使用函数返回的值? - How can I use a value returned by a function in another application in python? 如何保存使用Tkinter按钮运行的python函数的输出? - How can I save the output of a python function that I run using a Tkinter button? 如何将一个函数中生成的numpy.ndarray输出用作另一个函数的输入? - How can I use the numpy.ndarray output generated in one function as the input into another function? 如何获取函数的输出并将其用作另一个函数python的输入? - How to take output of function and use as input of another function python? 如何使用一个 function 的 output 作为 Z23EEEB4347BDD26BDDFC6B7EE 中另一个 function 的输入 - how to use output of one function as input of another function in python 如何在 Python 中选择随机对话 output? - How can I choose a random conversation output in Python? 如何在 Python 3 中获取一个函数以返回我可以在另一个函数中使用的值? - How can I get a function in Python 3 to return a value that I can use in another function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM