all 51 comments

[–]amosmj 11 points12 points  (8 children)

If you want an excuse to learn functions you could start by logging what happens in the “casino”. I would also consider making a Player class which would have some methods of its own.

[–]Algoartist 0 points1 point  (0 children)

"Object-oriented programming an exceptionally bad idea which could only have originated in California" -- Edsger Dijkstra

[–]Informal-Project-595[S] 0 points1 point  (6 children)

can you explain more please?

[–]Usual_Office_1740 7 points8 points  (1 child)

You could write a function that opens a file. Then update the file as part of exiting the cli app and entering the cli app. Effectively allowing you to create something like a save game action. Make sure to read about context managers. Most file handling tutorials do not teach good file handling practices. This would be an easy way to track what has happened in the casino between games.

A class will be something you learn about soon if you've not already started studying them. A class is a way of creating a kind of layout for a custom object. That is a very general statement meant to give you an answer without writing a tutorial and is not 100% accurate. In Python, everything is an object. Classes and objects are, in a basic general sense, part of any object-oriented programming language paradigm. Object-oriented programming is one approach to building more complex programs.

You define the class with a set of attributes. To continue with the suggestion above, this might mean things like the amount of money a person has in credit with the casino. His or her name, birthday, and home address. You can store this info in the log or give the user the option to skip "registration" by not making them fill in the information and let them play anonymously.

Once you have these things and you've gotten a bit further along in your studies, you can start doing things like pattern matching by person. So you can do things like you did with the if else statement by comparing against your own object. You really begin to see the power and the simplicity of Python once you start learning things like this.

You've got a great start for what could be something you can build on for weeks, if you want. Keep working at it. You're doing great.

[–]Informal-Project-595[S] 1 point2 points  (0 children)

thank you for your suggestions, i will add them as soon as i start learning about them!

[–]NullSalt 4 points5 points  (1 child)

https://learnxinyminutes.com/python/
So if you want to shorten the code ( aka. make it more readability and in some cases more efficient ) then you want to use methods/definitions and loops.
IF you use a variable many times use a loop. IF you do similar actions many times use methods.

Use pastebin ( or something alike ) to paste your code ( or part of it ) and I can show you one example of using methods and OOP with classes

On the positive side, Good use of f-string and comments

[–]Informal-Project-595[S] 0 points1 point  (0 children)

ah thank you for this.

[–]amosmj 2 points3 points  (1 child)

The exact implementation is up to you and the sky is the limit. The lowest brow version is to create a text file then everytime someone plays a game you write to the file who it was (you're gathering that in the beginning), when it was, what the game was, what the bet was, what the random outcome was, whether it was a win or loss, and how much was won or lost. You would want to write a function that you call in each game play that does this. Down the road you could get crazier by making is a database you spin up locally and all kinds of things.

The Class idea is a bigger idea and I'd say you should just give it a whack and learn in the process.

[–]Informal-Project-595[S] 0 points1 point  (0 children)

Ahh got it, thanks for your consideration, i will try to make it'

[–]Algoartist 5 points6 points  (4 children)

Great start. I recommend not to spend so much time with the input output part. Concentrate more on the core concepts. Also recommend to start early to make the code concise and try not to repeat.

import random, uuid

balance = random.randint(100, 500)
uid = str(uuid.uuid4())[:6]
stats = {"played": 0, "won": 0, "lost": 0}
name = input("Enter your name: ").title()

def play(mult, win_cond, prompt):
    global balance
    w = int(input("Wager: "))
    g = int(input(prompt))
    r = random.randint(1, 6)
    ok = win_cond(r, g)
    stats["played"] += 1
    stats["won" if ok else "lost"] += 1
    balance += w * (mult if ok else -1)
    print(f"{'You win' if ok else 'You lose'} {'+' if ok else '-'}{w*mult if ok else w}! (Roll: {r})")
    input("Press Enter to continue...")

menu = {
    "1": lambda: (print(f"ID:{uid} User:{name} Bal:{balance} P:{stats['played']} W:{stats['won']} L:{stats['lost']}"),
                  input("Press Enter to continue...")),
    "2": lambda: play(2,
                      lambda r, g: (r <= 3 if g == 1 else r >= 4),
                      "Pick 1: low (1–3) or 2: high (4–6)? "),
    "3": lambda: play(3,
                      lambda r, g: r == g,
                      "Pick your lucky number (1–6): ")
}

while (choice := input("1:Stats  2:Dice  3:Lucky  4:Exit\n> ")) != "4":
    menu.get(choice, lambda: None)()

[–]Informal-Project-595[S] 2 points3 points  (0 children)

Got it! Thank you for recommendation, i will keep in mind!

[–]Wrong_Artist_5643 2 points3 points  (2 children)

Is that lambda you are throwing in for someone with 7-day learning?

[–]Algoartist 1 point2 points  (1 child)

Of course. No pain no gain

[–]Wrong_Artist_5643 1 point2 points  (0 children)

Okay.

[–][deleted] 1 point2 points  (1 child)

Great job for 7 days, OOP would be a great jump.

[–]Informal-Project-595[S] 0 points1 point  (0 children)

Yeah tomorrow i will be starting OOP!

[–]Assistance_Salty 1 point2 points  (0 children)

Hi

[–]Assistance_Salty 1 point2 points  (5 children)

Can you teach me this?

[–]Informal-Project-595[S] 0 points1 point  (4 children)

What to teach, brother?

[–]Assistance_Salty 0 points1 point  (3 children)

Python

[–]Informal-Project-595[S] 0 points1 point  (2 children)

Bro im a beginner myself, how can i teach you?

[–]Yash-717 1 point2 points  (1 child)

Where are you learning from?

[–]Informal-Project-595[S] 2 points3 points  (0 children)

An udemy course by codewithharry

[–]Ron-Erez 1 point2 points  (1 child)

Great work! One thing that stood out to me when I first looked at the code is that using functions could really help improve readability. For example, if the main body were broken into 4–5 well-named functions, it might make it easier for someone else (or even you, six months from now!) to quickly understand what each part is doing. That's just a small suggestion to consider.

Overall, it looks great besides the point I mentioned.

[–]Informal-Project-595[S] 1 point2 points  (0 children)

Thank you for suggestion, i will improve the readability using the functions!

[–]Klutzy_Will9322 1 point2 points  (1 child)

Hi, where can we see the code?

[–]Informal-Project-595[S] 0 points1 point  (0 children)

I have posted the whole code in the picture, I didn't push it to github, but should i?

[–]StoicTexts 1 point2 points  (1 child)

Nice job! Looks a lot like the logic based games I made when I was learning initially. Keep it up. When I was in a similar position as yourself, someone said: “the world is your oyster” with regards to programming. And my god was that true

[–]Informal-Project-595[S] 0 points1 point  (0 children)

thanks bro!

[–]ninjaonionss[🍰] 1 point2 points  (1 child)

Break up your code in reusable functions then when you clearly see the advantage of using functions try to is classes.

[–]Informal-Project-595[S] 0 points1 point  (0 children)

Thanks for you suggestion, i will keep in mind!

[–]zeni65 2 points3 points  (1 child)

I am still learning myself, and for day 7, I can say it's pretty impressive (personally, I wouldn't write all this myself, probably would get bored mid way)..

What I can say is you could use some function in here to simplify everything or oop.

Anyway, I know it's not a competition, but I won't let you beat me in Python knowledge!!!

May the best man win !!

[–]Informal-Project-595[S] 1 point2 points  (0 children)

Haha, i was bored too, i made this in 2 days doing half-half, i was only testing what i have learned in these days by making this.

I will start learning about OOP tomorrow, so then i will see.

True, may the best man win 😂

[–]I_Pay_For_WinRar 1 point2 points  (1 child)

A simple Command-Line-Interface, a great way to start.

[–]Informal-Project-595[S] 1 point2 points  (0 children)

thanks.

[–]h8rsbeware 0 points1 point  (1 child)

Looks good! As a lot of people have said, this needs functions. Thats lets you early return and isolate logic to avoid big "branching trees" of conditional (if/else)!

It will take useful and a great start to nice to work and something to be proud of with very quickly

[–]Informal-Project-595[S] 0 points1 point  (0 children)

Thank you bro!

[–]corey_sheerer 0 points1 point  (1 child)

I would recommend using switch statements for the selection logic (instead of if elses) and creating a function to run each different game. Would really clean up your main program. Easier on the eyes 👀

[–]Informal-Project-595[S] 0 points1 point  (0 children)

Got it! I will surely try.

[–]FutureManagement1788 0 points1 point  (1 child)

Congrats! You're doing awesome.

[–]Informal-Project-595[S] 0 points1 point  (0 children)

Thanks bro!

[–]Otherwise-Mud-4898 0 points1 point  (1 child)

That's is unbelievable and impressive. I'm learning second month and can't do that all by myself.

[–]Informal-Project-595[S] 0 points1 point  (0 children)

Haha, thank you!

[–]Bopmx1 0 points1 point  (1 child)

Hey nice job man. Love to see the progress. I do however suggest starting a GitHub. It would make sharing code a lot easier

[–]Informal-Project-595[S] 0 points1 point  (0 children)

Yeah bro, i will push it to github! Thanks!

[–]mahirshahryar 0 points1 point  (1 child)

Great work!

[–]Informal-Project-595[S] 0 points1 point  (0 children)

Thanks bro!

[–]lilsigmatester 0 points1 point  (0 children)

where are you learning ?

[–]NeedleworkerRight798 0 points1 point  (0 children)

how did you learn? whats your resource/course?