mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
Python: Make code testable
Avoid executing code on module level as this prevents importing the module for testing. Especially infinite loops are evil.
This commit is contained in:
@@ -18,64 +18,69 @@ def throw_dice():
|
||||
return randint(1, 6) + randint(1, 6)
|
||||
|
||||
|
||||
print(" " * 33 + "Craps")
|
||||
print(" " * 15 + "Creative Computing Morristown, New Jersey")
|
||||
print()
|
||||
print()
|
||||
print()
|
||||
def main():
|
||||
print(" " * 33 + "Craps")
|
||||
print(" " * 15 + "Creative Computing Morristown, New Jersey")
|
||||
print()
|
||||
print()
|
||||
print()
|
||||
|
||||
winnings = 0
|
||||
print("2,3,12 are losers; 4,5,6,8,9,10 are points; 7,11 are natural winners.")
|
||||
winnings = 0
|
||||
print("2,3,12 are losers; 4,5,6,8,9,10 are points; 7,11 are natural winners.")
|
||||
|
||||
play_again = True
|
||||
while play_again:
|
||||
wager = int(input("Input the amount of your wager: "))
|
||||
play_again = True
|
||||
while play_again:
|
||||
wager = int(input("Input the amount of your wager: "))
|
||||
|
||||
print("I will now throw the dice")
|
||||
roll_1 = throw_dice()
|
||||
print("I will now throw the dice")
|
||||
roll_1 = throw_dice()
|
||||
|
||||
if roll_1 in [7, 11]:
|
||||
print(f"{roll_1} - natural.... a winner!!!!")
|
||||
print(f"{roll_1} pays even money, you win {wager} dollars")
|
||||
winnings += wager
|
||||
elif roll_1 == 2:
|
||||
print(f"{roll_1} - snake eyes.... you lose.")
|
||||
print(f"You lose {wager} dollars")
|
||||
winnings -= wager
|
||||
elif roll_1 in [3, 12]:
|
||||
print(f"{roll_1} - craps.... you lose.")
|
||||
print(f"You lose {wager} dollars")
|
||||
winnings -= wager
|
||||
else:
|
||||
print(f"{roll_1} is the point. I will roll again")
|
||||
roll_2 = 0
|
||||
while roll_2 not in [roll_1, 7]:
|
||||
roll_2 = throw_dice()
|
||||
if roll_2 == 7:
|
||||
print(f"{roll_2} - craps. You lose.")
|
||||
print(f"You lose $ {wager}")
|
||||
winnings -= wager
|
||||
elif roll_2 == roll_1:
|
||||
print(f"{roll_1} - a winner.........congrats!!!!!!!!")
|
||||
print(
|
||||
f"{roll_1} at 2 to 1 odds pays you...let me see... {2 * wager} dollars"
|
||||
)
|
||||
winnings += 2 * wager
|
||||
else:
|
||||
print(f"{roll_2} - no point. I will roll again")
|
||||
if roll_1 in [7, 11]:
|
||||
print(f"{roll_1} - natural.... a winner!!!!")
|
||||
print(f"{roll_1} pays even money, you win {wager} dollars")
|
||||
winnings += wager
|
||||
elif roll_1 == 2:
|
||||
print(f"{roll_1} - snake eyes.... you lose.")
|
||||
print(f"You lose {wager} dollars")
|
||||
winnings -= wager
|
||||
elif roll_1 in [3, 12]:
|
||||
print(f"{roll_1} - craps.... you lose.")
|
||||
print(f"You lose {wager} dollars")
|
||||
winnings -= wager
|
||||
else:
|
||||
print(f"{roll_1} is the point. I will roll again")
|
||||
roll_2 = 0
|
||||
while roll_2 not in [roll_1, 7]:
|
||||
roll_2 = throw_dice()
|
||||
if roll_2 == 7:
|
||||
print(f"{roll_2} - craps. You lose.")
|
||||
print(f"You lose $ {wager}")
|
||||
winnings -= wager
|
||||
elif roll_2 == roll_1:
|
||||
print(f"{roll_1} - a winner.........congrats!!!!!!!!")
|
||||
print(
|
||||
f"{roll_1} at 2 to 1 odds pays you...let me see... {2 * wager} dollars"
|
||||
)
|
||||
winnings += 2 * wager
|
||||
else:
|
||||
print(f"{roll_2} - no point. I will roll again")
|
||||
|
||||
m = input(" If you want to play again print 5 if not print 2: ")
|
||||
if winnings < 0:
|
||||
print(f"You are now under ${-winnings}")
|
||||
elif winnings > 0:
|
||||
print(f"You are now ahead ${winnings}")
|
||||
else:
|
||||
print("You are now even at 0")
|
||||
play_again = m == "5"
|
||||
|
||||
m = input(" If you want to play again print 5 if not print 2: ")
|
||||
if winnings < 0:
|
||||
print(f"You are now under ${-winnings}")
|
||||
print("Too bad, you are in the hole. Come again.")
|
||||
elif winnings > 0:
|
||||
print(f"You are now ahead ${winnings}")
|
||||
print("Congratulations---you came out a winner. Come again.")
|
||||
else:
|
||||
print("You are now even at 0")
|
||||
play_again = m == "5"
|
||||
print("Congratulations---you came out even, not bad for an amateur")
|
||||
|
||||
if winnings < 0:
|
||||
print("Too bad, you are in the hole. Come again.")
|
||||
elif winnings > 0:
|
||||
print("Congratulations---you came out a winner. Come again.")
|
||||
else:
|
||||
print("Congratulations---you came out even, not bad for an amateur")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user