mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
Port NUMBER to Python
Simple port, changed some variable names.
This commit is contained in:
82
66 Number/python/number.py
Normal file
82
66 Number/python/number.py
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
"""
|
||||||
|
NUMBER
|
||||||
|
|
||||||
|
A number guessing (gambling) game.
|
||||||
|
|
||||||
|
Ported by Dave LeCompte
|
||||||
|
"""
|
||||||
|
|
||||||
|
import random
|
||||||
|
|
||||||
|
|
||||||
|
def print_with_tab(num_spaces, msg):
|
||||||
|
if num_spaces > 0:
|
||||||
|
spaces = " " * num_spaces
|
||||||
|
else:
|
||||||
|
spaces = ""
|
||||||
|
|
||||||
|
print(spaces + msg)
|
||||||
|
|
||||||
|
|
||||||
|
def print_instructions():
|
||||||
|
print("YOU HAVE 100 POINTS. BY GUESSING NUMBERS FROM 1 TO 5, YOU")
|
||||||
|
print("CAN GAIN OR LOSE POINTS DEPENDING UPON HOW CLOSE YOU GET TO")
|
||||||
|
print("A RANDOM NUMBER SELECTED BY THE COMPUTER.")
|
||||||
|
print()
|
||||||
|
print("YOU OCCASIONALLY WILL GET A JACKPOT WHICH WILL DOUBLE(!)")
|
||||||
|
print("YOUR POINT COUNT. YOU WIN WHEN YOU GET 500 POINTS.")
|
||||||
|
print()
|
||||||
|
|
||||||
|
|
||||||
|
def fnr():
|
||||||
|
return random.randint(1, 5)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print_with_tab(33, "NUMBER")
|
||||||
|
print_with_tab(15, "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY")
|
||||||
|
print()
|
||||||
|
print()
|
||||||
|
print()
|
||||||
|
|
||||||
|
print_instructions()
|
||||||
|
|
||||||
|
points = 100
|
||||||
|
|
||||||
|
while points <= 500:
|
||||||
|
print("GUESS A NUMBER FROM 1 TO 5")
|
||||||
|
guess = int(input())
|
||||||
|
|
||||||
|
if (guess < 1) or (guess > 5):
|
||||||
|
continue
|
||||||
|
|
||||||
|
r = fnr()
|
||||||
|
s = fnr()
|
||||||
|
t = fnr()
|
||||||
|
u = fnr()
|
||||||
|
v = fnr()
|
||||||
|
|
||||||
|
if guess == r:
|
||||||
|
# lose 5
|
||||||
|
points -= 5
|
||||||
|
elif guess == s:
|
||||||
|
# gain 5
|
||||||
|
points += 5
|
||||||
|
elif guess == t:
|
||||||
|
# double!
|
||||||
|
points += points
|
||||||
|
print("YOU HIT THE JACKPOT!!!")
|
||||||
|
elif guess == u:
|
||||||
|
# gain 1
|
||||||
|
points += 1
|
||||||
|
elif guess == v:
|
||||||
|
# lose half
|
||||||
|
points = points - (points * 0.5)
|
||||||
|
|
||||||
|
print(f"YOU HAVE {points} POINTS.")
|
||||||
|
print()
|
||||||
|
print(f"!!!!YOU WIN!!!! WITH {points} POINTS.")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user