user16198775
user16198775

Reputation:

First part of this code is not being executed

I'm trying to make a flip the coin game but part of it is not being executed

import time
import random

useractions = input("Enter(flip the coin): ")

possible_actions =["flip the coin", "Flip the coin"]
computeractions=random.choice(possible_actions)

if useractions == "tails":
    if computeractions == "heads":
        print("It was "+computeractions)
elif useractions == "heads":
    if computeractions == "tails":
        print(" It was "+computeractions)
elif useractions == "tails":
    if computeractions == "tails":
        print("It was "+computeractions)
elif useractions == "heads":
    if computeractions == "heads":
        print("It was "+computeractions)

that's the full code ^

 elif useractions == "tails":
        if computeractions == "tails":
            print("It was "+computeractions)
    elif useractions == "heads":
        if computeractions == "heads":
            print("It was "+computeractions)

That's the part that is not working ^

When I type flip the coin it either executes the first part but if it doesn't it displays nothing

Upvotes: 1

Views: 63

Answers (2)

Richard K Yu
Richard K Yu

Reputation: 2202

I think there are several issues that have already been pointed out in the comments. But to design something that is clearer and that I think achieves what you want with the advice from the comments, I think you should change the possible actions and then use a ternary operator to display the result.

This will avoid the problematic comparisons.

For instance,

import random

useractions = input("Heads or tails? ").lower()

possible_actions =["heads", "tails"]
computeractions=random.choice(possible_actions)

print("You got it!" if computeractions == useractions else "It was "+computeractions)

More on ternary operators

Sample output from code: enter image description here

Upvotes: 0

Jovan Topolac
Jovan Topolac

Reputation: 64

In your outer if/elif blocks, the first two conditions are the same as the third and fourth one, so your program always executes one of the first two branches. Looks to me like you wanted to do something like this:

if useractions == "tails" and computeractions == "heads":    
    print("It was "+computeractions)
elif useractions == "heads" and computeractions == "tails":
    print(" It was "+computeractions)
elif useractions == "tails" and computeractions == "tails":    
    print("It was "+computeractions)
elif useractions == "heads" and computeractions == "heads":
    print("It was "+computeractions)

Upvotes: 1

Related Questions