简体   繁体   中英

the program runs but when i type in my value it doesnt do anything . this is a game of rock paper scissors

This is a game of rock paper scissors.

The program runs, but when I type in my value it doesn't do anything.

Here's my code:

import random

def play():
    user = input("whats your choice? 'r' for rock, 'p' for paper, 's' for scissors\n")
    computer = random.choice(["r", "p", "s"])

    if user == computer:
        return "its a tie"

    if is_win(user, computer):
        return "you won!"

    return "you lost!"

def is_win(player, opponent) :
    if (player == "r" and opponent == "s") or (player == "s" and opponent == "p")  or (player== "p" and opponent == "r"):
        return True

play()

It does run. But you are not printing anything.

You must print what the play() function returns. Then only it prints one of the return statements.

print(play())

You are returning the answers above, you can either print the result, like print("You won!") or print the call to the play function like print(play()) . This should work.

It works perfectly. It is just that you aren't making use of the returned value. When you use return in the function, you need to either define a variable where you call the function (if you want to use it later) or you can just use print(your_function()) to print the returned value.

So you need to do:

print(play())

Or if you want to use it later:

val1=play()
print(val1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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