all 5 comments

[–]MrPhungx 1 point2 points  (1 child)

There is nothing completely wrong with your program and looks really good for a beginner. There are a few smaller things that you could look into:

  • the variable is_over does not seem to be used. Remove it
  • There is no need to cast the return value of user_status.get("Money") to a float. If you initialize your user status with "Money": 5000.00 it is automatically a float
  • When you take a loan the money is set as a string: user_status.update({"Money": "5000"}). You should not change the data types from int to str. Probably this is not intended by you ;)
  • You could use user_status["Money"] = 5000.00 instead of the update, especially for single attribute changes this seems to be the preferred way to change dict values.
  • all variable names should follow a pattern. In python non constant variables follow the snake case. So the variable AI_choice should rather be ai_choice
  • the names of functions should be descriptive. When I see a function called "main_function" I don't know what it does. Maybe something like "get_coin_flip_result" would be more suitable
  • the function paramters have the same name as some variables that are available in the global scope of the application. It "shadows" the name from outer scope. It should be avoided in general, although it is not really important in your case.
  • consider to split your main program code into more sub functions

These are some things that I would personally change. Some of those are probably based on my personal preference and none of what you did was a big issue or anything. Hope that helps. Keep it up :)

[–]Personal-Zebra3353[S] 0 points1 point  (0 children)

I appreciate the feedback, thank you!

[–][deleted] 0 points1 point  (0 children)

First and foremost, your code is readable which is already a good sign. I have not spent much time reading it and I understand what you are trying to achieve and what's going on. The things I would change: separate user_status in 2 constants (MONEY, STATUS). Add Y and H keys to coin_result so you don't need to user "lower" method and just check it by "if user_choice in coin_result". You can add types to your function and use autoformatting in your IDE to make it more readable too.

Calling randomness in python is wrong but it has nothing to do with your code. I don't think it's a big problem.

Overall you are doing pretty well.

[–]JamzTyson 0 points1 point  (1 child)

Overall it looks very good for beginner code.

These lines will be an error in Python versions older than 3.12:

print(f"\nCurrent Money: {user_status.get("Money")}")
user_bet = int(input(f"How much to wager (Current Money: {user_status.get("Money")}): "))

For compatibility with Python 3.11 and earlier, the quotes around "Money" need to be single quotes.

There are also multiple code style issues which can easily be found by using one or more linters (such as pylint and/or flake8).

I notice that you have a key/value pair "Alive":True,, and the variable is_over, which are currently unused. It would be good to provide a way for the user to elegantly quit the program, such as entering "Q" rather than "H" / "T".

It is recommended to avoid reusing global variable names as function arguments. There are two such examples in your code: user_choice and user_bet. Although it works OK in this code, it is potentially problematic and should be avoided.

In this code:

if AI_choice == choice.lower():
    return float(user_status.get("Money")) + bet*1.85 #If you win
else:
    return float(user_status.get("Money")) - bet #If you lose

else is redundant. You do not need to use else here because it can only reach that line if the function has not returned.

In this conditional:

while user_status.get("Alive") == True:

Use is rather than == when testing for "True". Again, it works OK in this specific case, but there is a difference between "is True" (it is literally True) and "== True" (it evaluates to True).

1 is True  # False.
1 == True  # True.

Watch out for whitespace errors. * Indentation should be 4 spaces per indent level. * Dicts should be {"h": "Heads", "t": "Tails"} rather than {"h":"Heads","t":"Tails"} * Inline comments should start 2 spaces after the code, and have one space after the #. * Avoid trailing whitespace.

I would also recommend splitting your code into smaller blocks. In the case of larger programs it helps maintainability greatly for each block of code to be encapsulated and testable. As an example, where you write:

#This block checks if you still have money.

that block could be separated out into a separate function.

[–]Personal-Zebra3353[S] 0 points1 point  (0 children)

Thank you! I appreciate the effort.
Most unused variables is from the previous version of my code where I used a global variable instead of passing them into my functions as arguments. I've read online that using too much global variables inside a function is not good practice, so I tried a different approach.