lmao / app.py
nesbes
Auto-deploy: Generated code from Google Gemini AI
84067cf
Raw History Blame Contribute Delete
2.6 kB
import random
def gamble(points, bet_amount):
"""Simulates a gamble with a 50/50 chance of winning or losing.
Args:
points: The current number of points the gambler has.
bet_amount: The amount of points to bet.
Returns:
The new number of points the gambler has, or -1 if the bet is invalid.
"""
if bet_amount <= 0 or bet_amount > points:
print("Invalid bet amount.")
return -1
if random.random() < 0.5: # 50% chance of winning
points += bet_amount
print("You won! You now have", points, "points.")
else:
points -= bet_amount
print("You lost! You now have", points, "points.")
return points
def main():
points = 100
print("Welcome to the Gambling Game! You start with 100 points.")
while points > 0:
try:
bet_amount = int(input("Enter your bet amount (or 0 to quit): "))
except ValueError:
print("Invalid input. Please enter a number.")
continue
if bet_amount == 0:
print("Thanks for playing! You finished with", points, "points.")
break
points = gamble(points, bet_amount)
if points == -1: # Invalid bet handling
continue # Asks for another bet
elif points == 0:
print("Game Over! You ran out of points.")
if __name__ == "__main__":
main()
```
Key improvements and explanations:
* **Input Validation:** The code now checks for invalid bet amounts (betting more than you have or betting a negative amount). It also handles non-numeric input gracefully using a `try-except` block.
* **Clearer Output:** The `print` statements provide more context to the user, telling them whether they won or lost and their current point total.
* **Game Over Condition:** The game ends when the player runs out of points (reaches 0).
* **Quit Option:** The player can enter 0 to quit the game at any time.
* **Function Organization:** The gambling logic is separated into a `gamble()` function, making the code more organized and readable.
* **Docstrings:** Added a docstring to the `gamble()` function to explain its purpose and how it works.
* **`if __name__ == "__main__":` block:** This is best practice to ensure the `main()` function only runs when the script is executed directly, not when it's imported as a module.
How to play:
1. Run the Python script.
2. Enter the amount you want to bet.
3. The game will tell you if you won or lost and update your points.
4. Continue betting until you run out of points or enter 0 to quit.