Python Forum
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Poker Dice while loop
#1
I am not sure why my while loop is broken, any help would be greatly appreciated.


#This program will simulate a poker dice game.
import random
import time
def isEven(number):
    if (number % 2 == 0):
    	return True
    else:
        return False
def isOdd(number):
    if (number % 2 == 1):
    	return True
    else:
        return False
pts = 0
a = 0
print("---Poker Dice---\nWelcome User! This is Poker Dice, three dice will be thrown and score points:\nThree of a Kind = +50 pts, Straight = +30 pts, Three Odd Numbers = +15 pts,\nThree Even Numbers = +15 pts, One Pair = +10 pts.")

while a == 0:
    d1 = random.randint(1,6)
    d2 = random.randint(1,6)
    d3 = random.randint(1,6)
    print("The first die rolled", d1)
    print("The second die rolled",d2)
    print("The third die rolled",d3)
    if d1 == d2 and d1 == d3:
        print("All three dice match which means +50 pts!")
        pts += 50
    if d1==(d2+1) and d1==d3+2:
        pts += 30
        print("Wow, you just rolled a straight which means +30 pts!")
    elif d1==(d3+1) and d1==d2+2:	    
        pts += 30
        print("Wow, you just rolled a straight which means +30 pts!")
    elif d2==(d3+1) and d2==d1+2:	    
        pts += 30
        print("Wow, you just rolled a straight which means +30 pts!")
    elif d2==(d1+1) and d2==d3+2:	    
        pts += 30
        print("Wow, you just rolled a straight which means +30 pts!")
    elif d3==(d1+1) and d3==d2+2:	    
        pts += 30
        print("Wow, you just rolled a straight which means +30 pts!")
    elif d3==(d2+1) and d3==d1+2:	    
        pts += 30
        print("Wow, you just rolled a straight which means +30 pts!")
    if isEven(d1) and isEven(d2) and isEven(d3):
        pts += 15
        print("You just rolled three even numbers! Which means +15 pts!")
    if isOdd(d1) and isOdd(d2) and isOdd(d3):
        pts += 15
        print("You just rolled three odd numbers! Which means +15 pts!")
    if d1 == d2 or d1 == d3 or d2 == d1 or d2 == d3 or d3 == d1 or d3 == d2:
        pts += 10
        print("Two of the dice match, which means +10 pts!")

    print("Overall you scored", pts,"points!")
    fa = input("Would you like to re-roll your dice? Type 'yes' to re-roll or type 'no' to go home safe with your current points.\n").lower()
    if fa == 'yes' or 'y':
        print("Wow, you are brave!")
        pts = 0
    else:
        print("Fine then, we hope to see you again soon! Goodbye.")
        a += 1
        time.sleep(3)
        quit()
        
Reply
#2
This is your error:

if fa == 'yes' or 'y':
Python reads it like this:

if (fa == 'yes') or ('y'):
Since any non-empty string evaluates as True, the second part is True, and the whole expression is always True. You want:

if fa == 'yes' or fa == 'y':
or

if fa in ('yes', 'y'):
I would also use break instead of quit, and more descriptive variable names (a and fa are confusing). Also, if you put the three dice into a list, and then sort that list, then the comparisons are much easier (especially for straights).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(May-28-2017, 08:03 PM)ichabod801 Wrote: This is your error:
 if fa == 'yes' or 'y': 
Python reads it like this:
 if (fa == 'yes') or ('y'): 
Since any non-empty string evaluates as True, the second part is True, and the whole expression is always True. You want:
 if fa == 'yes' or fa == 'y': 
or
 if fa in ('yes', 'y'): 
I would also use break instead of quit, and more descriptive variable names (a and fa are confusing). Also, if you put the three dice into a list, and then sort that list, then the comparisons are much easier (especially for straights).
Thank You!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Hi! + Poker calculator SpookyKooks 2 1,097 Mar-12-2020, 05:32 AM
Last Post: SpookyKooks
  Similar to Poker bluekade5050 1 1,094 Nov-14-2018, 04:46 PM
Last Post: j.crater
  Issue with my 'roll the dice simulation'-exercise (cannot break out of the loop) Placebo 2 1,934 Sep-30-2018, 01:19 PM
Last Post: Placebo
  Making a percentile dice roller and dice roller Fixer243 2 1,544 Sep-30-2018, 12:18 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

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