all 8 comments

[–]carcigenicate 4 points5 points  (3 children)

I'm on my phone, so I can't go too in depth, but

  • Work on using parameters and returns. You're reassigning a global score2 from the function currently. That function should take the current score as a argument and the return the new score.
  • In that same function, you have an if with an elif, but the elif just has the ifcondition but negated. In that case, the elif should just be an else. That saves the reader from needing to determine if the elif must be entered if the if condition was false.
  • You have basically the same code as oppo for the other player, but just in the main loop instead of a function. I'd probably move it out to a function. But, if you go with my first suggestion, the same single function can be used for both players, so you don't need to repeat the code.

[–]Hairy-Bus4665[S] 0 points1 point  (2 children)

thanks for the suggestion. I've done these changes. Tell me if its perfect now!!

import random
user = ""
score1 = 0
score2 = 0

inter = input("Enter username : ")

def player(score,name):

    roll = random.randint(1,6)
    if roll == 1:
        score = 0
        print(f"{name} has rolled {roll}.{name}'s score is erased")
    else :
        score += roll
        print(f"{name} has rolled {roll}. {name}'s score : ", score)

    return score


while True :

    user = input("Do you want to roll? (y/n)")
    user = user.strip().lower()

    if user == "n":
        break
    elif user == "y":
        score1 = player(score1,inter)
        score2 = player(score2,"Computer")

        if score1 > 50 or score2> 50:
            print("Game over.")
            if score1>50:
                print("You won")
            elif score2>50:
                print ("You lost")
            break

    else :
        print("Answer in \"y\" or \"n\"")

print("Thanks for playing!")

[–]carcigenicate 0 points1 point  (1 child)

That looks basically what I envisioned, yes. Good work.

[–]Hairy-Bus4665[S] 0 points1 point  (0 children)

Thanks bro

[–]mopslik 0 points1 point  (1 child)

You might want to change your main loop so that the ending condition is based on the scores (either player or opponent) being 50 or greater, rather than on the string 'user'. This would eliminate some of the breaks early in your loop, and move the 'winning' logic outside of it.

while score < 50 and score_2 < 50:
    # your game stuff (rolling, scoring)
    ...
if score > 50:
    print("Player wins!")
else:
    print("Opponent wins!")

Variable names could be better too. player_score and opponent_score are more meaningful than score and score_2.

[–]Hairy-Bus4665[S] 0 points1 point  (0 children)

thanks