issue with if else statement - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: issue with if else statement (/thread-28031.html) |
issue with if else statement - spalisetty06 - Jul-02-2020 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 ") RE: issue with if else statement - menator01 - Jul-02-2020 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 RE: issue with if else statement - spalisetty06 - Jul-02-2020 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 RE: issue with if else statement - ndc85430 - Jul-02-2020 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. RE: issue with if else statement - spalisetty06 - Jul-02-2020 @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 RE: issue with if else statement - pyzyx3qwerty - Jul-02-2020 Remember to use proper code tags in the future while posting, @spalisetty06 - see BBCode RE: issue with if else statement - spalisetty06 - Jul-02-2020 Sorry Sir, I didn't know that there is a format for output too, will not repeat down the line. |