EnCoder EnCoder - 1 year ago 36
Python Question

Issue with OR statement in Python while making simple dice rolling game

I am new to Python programming and i have a problem with the or statement in this program. When i run it in PyCharm and input 'roll' it works. But when i use 'yes' it seems not to do anything.
while input() == ("roll") or input() == ("yes"): This line is the problem. I would like to know what I'm doing wrong here. When i use only one command for example roll it works perfectly for as many times as i want to run it.

# this is a dice rolling game
import random

min = 1
max = 6
def roll_dice():
x = random.randint(min, max)

print("Would you like to roll the dice?")

while input() == ("roll") or input() == ("yes"):
print("Would you like to roll the dice again ?")

Answer Source

Do not call input() twice. Call it once and test for existence in a set using in:

while input() in {'roll', 'yes'}:

If you're on python2, remember you'll need raw_input, not input.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download