Python Forum
issue with if else statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
issue with if else statement
#1
Hello,
I have tried to program something simple as I am a beginner. But it always executes else part, I don't know why. Please correct my code.

import random
Player1 = ["Rock", "Paper", "Scissor"]
Player2 = ["Rock", "Paper", "Scissor"]
Player1 = random.choice(Player1)
Player2 = random.choice(Player2)
print("Player1 and Player2 are ",Player1, Player2)
choice = input("Please select your choice by entering Y or N ")
while choice!='N':
    if Player1 == "Rock" and Player2 == "Paper":
        print("Player2 is the winner")
        choice = input("Please select your choice by entering Y or N ")
    elif Player1 == "Rock" and Player2 == "Scissor":
        print("Player1 is the winner")
        choice = input("Please select your choice by entering Y or N ")
    else:
        print("No loser")
        choice = input("Please select your choice by entering Y or N ")
Reply
#2
Your random choice never changes. It's out side the loop.

Your code modified a little. Please be aware that you have not accounted for all win scenarios. Hence it will show looser a good bit. It does change if the conditions are met.
import random
CHOICES = ["Rock", "Paper", "Scissor"]
run = True
while run:
    choice = input("Please select your choice by entering Y or N ")
    Player1 = random.choice(CHOICES)
    Player2 = random.choice(CHOICES)
    print("Player1 and Player2 are ",Player1, Player2)
    if Player1 == "Rock" and Player2 == "Paper":
        print("Player2 is the winner")
        choice = input("Please select your choice by entering Y or N ")
    elif Player1 == "Rock" and Player2 == "Scissor":
        print("Player1 is the winner")
        choice = input("Please select your choice by entering Y or N ")
    else:
        print("No loser")
        choice = input("Please select your choice by entering Y or N ")
    if choice == 'N':
        run = False
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
That didn't work, sorry

@menator01, as you have said, I have added all options and I am able to see where I am going wrong, I will write the output as well, but, I don't understand why is that output.

import random
Player1 = ["Rock", "Paper", "Scissor"]
Player2 = ["Rock", "Paper", "Scissor"]
Player1 = random.choice(Player1)
Player2 = random.choice(Player2)
print("Player1 and Player2 are ",Player1, Player2)
choice = input("Please select your choice by entering Y or N ")
while choice!='N':
    if Player1 == "Rock" and Player2 == "Paper":
        print("Player2 is the winner")
        Player1 = random.choice(Player1)
        Player2 = random.choice(Player2)
        print("Player1 and Player2 are ", Player1, Player2)
        choice = input("Please select your choice by entering Y or N ")
    elif Player1 == "Rock" and Player2 == "Scissor":
        print("Player1 is the winner")
        Player1 = random.choice(Player1)
        Player2 = random.choice(Player2)
        print("Player1 and Player2 are ", Player1, Player2)
        choice = input("Please select your choice by entering Y or N ")
    elif Player1 == "Paper" and Player2 == "Rock":
        print("Player1 is the winner")
        Player1 = random.choice(Player1)
        Player2 = random.choice(Player2)
        print("Player1 and Player2 are ", Player1, Player2)
        choice = input("Please select your choice by entering Y or N ")
    elif Player1 == "Scissor" and Player2 == "Rock":
        print("Player2 is the winner")
        Player1 = random.choice(Player1)
        Player2 = random.choice(Player2)
        print("Player1 and Player2 are ", Player1, Player2)
        choice = input("Please select your choice by entering Y or N ")
    elif Player1 == "Paper" and Player2 == "Scissor":
        print("Player2 is the winner")
        Player1 = random.choice(Player1)
        Player2 = random.choice(Player2)
        print("Player1 and Player2 are ", Player1, Player2)
        choice = input("Please select your choice by entering Y or N ")
    elif Player1 == "Scissor" and Player2 == "Paper":
        print("Player2 is the winner")
        Player1 = random.choice(Player1)
        Player2 = random.choice(Player2)
        print("Player1 and Player2 are ", Player1, Player2)
        choice = input("Please select your choice by entering Y or N ")
    else:
        print("No loser")
        Player1 = random.choice(Player1)
        Player2 = random.choice(Player2)
        print("Player1 and Player2 are ", Player1, Player2)
        choice = input("Please select your choice by entering Y or N ")
Output
Player1 and Player2 are Scissor Paper
Please select your choice by entering Y or N Y
Player2 is the winner
Player1 and Player2 are S a
Please select your choice by entering Y or N Y
No loser
Player1 and Player2 are S a
Please select your choice by entering Y or N Y
No loser
Player1 and Player2 are S a
Please select your choice by entering Y or N
Reply
#4
What have you done to debug your program then? Print out your variables to see what's going on.

Also, you don't want to be overwriting your lists as you're doing on lines 4 and 5, as that will likely lead to more problems. By the way, you only need one list that you can choose from.
Reply
#5
@ndc85430
I have added the output. Not sure why I am getting single characters instead of the elements in the list
output:
Player1 and Player2 are Scissor Rock
Please select your choice by entering Y or N Y
Player2 is the winner
Player1 and Player2 are Rock Scissor
Please select your choice by entering Y or N y
Player1 is the winner
Player1 and Player2 are Paper Paper
Please select your choice by entering Y or N Y
No loser
Player1 and Player2 are Paper Paper
Please select your choice by entering Y or N Y
No loser
Player1 and Player2 are Rock Scissor
Please select your choice by entering Y or N Y
Player1 is the winner
Player1 and Player2 are Rock Paper
Please select your choice by entering Y or N Y
Player2 is the winner
Player1 and Player2 are Scissor Scissor
Please select your choice by entering Y or N Y
No loser
Player1 and Player2 are Scissor Scissor
Please select your choice by entering Y or N Y
No loser
Player1 and Player2 are Paper Rock
Please select your choice by entering Y or N Y
Player1 is the winner
Player1 and Player2 are Paper Paper
Please select your choice by entering Y or N

I resolved the issue with all of your's support and idea.

import random
Random1 = ["Rock", "Paper", "Scissor"]
Random2 = ["Rock", "Paper", "Scissor"]
Player1 = random.choice(Random1)
Player2 = random.choice(Random2)
print("Player1 and Player2 are ",Player1, Player2)
choice = input("Please select your choice by entering Y or N ")
while choice!='N':
    if Player1 == "Rock" and Player2 == "Paper":
        print("Player2 is the winner")
        Player1 = random.choice(Random1)
        Player2 = random.choice(Random2)
        print("Player1 and Player2 are ", Player1, Player2)
        choice = input("Please select your choice by entering Y or N ")
    elif Player1 == "Rock" and Player2 == "Scissor":
        print("Player1 is the winner")
        Player1 = random.choice(Random1)
        Player2 = random.choice(Random2)
        print("Player1 and Player2 are ", Player1, Player2)
        choice = input("Please select your choice by entering Y or N ")
    elif Player1 == "Paper" and Player2 == "Rock":
        print("Player1 is the winner")
        Player1 = random.choice(Random1)
        Player2 = random.choice(Random2)
        print("Player1 and Player2 are ", Player1, Player2)
        choice = input("Please select your choice by entering Y or N ")
    elif Player1 == "Scissor" and Player2 == "Rock":
        print("Player2 is the winner")
        Player1 = random.choice(Random1)
        Player2 = random.choice(Random2)
        print("Player1 and Player2 are ", Player1, Player2)
        choice = input("Please select your choice by entering Y or N ")
    elif Player1 == "Paper" and Player2 == "Scissor":
        print("Player2 is the winner")
        Player1 = random.choice(Random1)
        Player2 = random.choice(Random2)
        print("Player1 and Player2 are ", Player1, Player2)
        choice = input("Please select your choice by entering Y or N ")
    elif Player1 == "Scissor" and Player2 == "Paper":
        print("Player2 is the winner")
        Player1 = random.choice(Random1)
        Player2 = random.choice(Random2)
        print("Player1 and Player2 are ", Player1, Player2)
        choice = input("Please select your choice by entering Y or N ")
    else:
        print("No loser")
        Player1 = random.choice(Random1)
        Player2 = random.choice(Random2)
        print("Player1 and Player2 are ", Player1, Player2)
        choice = input("Please select your choice by entering Y or N ")

output:
Player1 and Player2 are Paper Scissor
Please select your choice by entering Y or N Y
Player2 is the winner
Player1 and Player2 are Scissor Scissor
Please select your choice by entering Y or N Y
No loser
Player1 and Player2 are Paper Paper
Please select your choice by entering Y or N Y
No loser
Player1 and Player2 are Rock Rock
Please select your choice by entering Y or N Y
No loser
Player1 and Player2 are Rock Paper
Please select your choice by entering Y or N Y
No loser
Player1 and Player2 are Paper Scissor
Please select your choice by entering Y or N Y
Player2 is the winner
Reply
#6
Remember to use proper code tags in the future while posting, @spalisetty06 - see BBCode
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#7
Sorry Sir, I didn't know that there is a format for output too, will not repeat down the line.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Issue using print statement in a thread bweiss1258 9 5,299 Jan-16-2018, 02:07 AM
Last Post: bweiss1258

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020