Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

How to call a random function within another function without it being executed separately each time in Python?

The purpose of the code is to build a rock, paper, scissor game against the computer.

import random

def computer():
    comp = random.choice([0,1,2])
    if comp ==0:
        print "Computer throws rock",
    elif comp==1:
        print "Computer throws paper"
    elif comp==2:
        print "Computer throws scissors"
    return comp
print computer()

def throwing(a):
    if a == 0:
        print "Player throws rock"
    elif a == 1:
        print "Player throws paper"
    elif a  ==2:
        print "Player throws scissors"
    if (a-computer())%3 > (computer()-a)%3:
        print "Computer wins"
    elif a == computer():
        print "Draw"
    else:
        print "Player wins"

throwing(0)

I would expect it to return something like this:

Computer throws rock
0
Player throws paper
Player wins

but instead it returns

Computer throws rock
0
Player throws rock
Computer throws rock Computer throws scissors
Computer throws paper
Player wins

It seems that each time I call the function computer() within the function throwing(a) it doesn't take the aforegenerated random number but instead generates a new number each time, causing both the message to appear two extra times and totally messing up the correct rock-paper-scissor results.

How can I call the random-number-generating function computer() within the function throwing() for it just to keep its original value consistently?

question from:https://stackoverflow.com/questions/65862851/how-to-call-a-random-function-within-another-function-without-it-being-executed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can't. That's not how computers work.

You have a problem in your logic: you tell the computer to run some code, when you don't actually want it to run that code. And that is not going to succeed, no matter what programming language you are using.

If you want the computer to "throw" only once, then you must call computer() only once!

But, since you need the result later, you have to save that result:

import random

def computer():
    comp = random.choice([0,1,2])
    if comp == 0:
        print "Computer throws rock",
    elif comp == 1:
        print "Computer throws paper"
    elif comp == 2:
        print "Computer throws scissors"
    return comp
comp = computer()
print comp

def throwing(player, comp):
    if player == 0:
        print "Player throws rock"
    elif player == 1:
        print "Player throws paper"
    elif player == 2:
        print "Player throws scissors"
    if (player - comp) % 3 > (comp - player) % 3:
        print "Computer wins"
    elif player == comp:
        print "Draw"
    else:
        print "Player wins"

throwing(0, comp)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...