Python Forum
please help me improve this minigame - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: Code sharing (https://python-forum.io/forum-5.html)
+--- Thread: please help me improve this minigame (/thread-15541.html)



please help me improve this minigame - Naito - Jan-21-2019

Hi, i made this minigame for learning purposes it works fine, but it's too long and i guess there are much more improvements i can make , i am a total beginner
THANK YOU!
in this gamemode (duogame) each player needs to guess the random number between 0 and 10, the player who, guesses the number gains 2 points, the player who guessed a number near the random number gets 1 point, expl: the random number is 7 but you guessed 6 or 8 you have a point, the player who didn't guess the number gain no points
import random


def duogame():
    round = 0
    scores = {}
    n = int(input("plz type the number of rounds you want to play: "))
    roundsleft = [n]
    player1name = input("first player, please enter your name: ")
    player2name = input("second player, please enter your name: ")
    scores[player1name] = 0
    scores[player2name] = 0

    while True:
        Dice = random.randint(0, 10)
        print(player1name, 'turn: ')
        player1 = int(input())
        print(player2name, "turn: ")
        player2 = int(input())
        round += 1
        roundsleft[0] -= 1
        if (player1 and player2) == 911:
            print("Thank you for playing :)")
            print("scores list: ", scores)
            break
        if roundsleft[0] == 0:
            print("game over!, Thanks for playing!")
            print("scores list: ", scores)
            break

        print("round 0")
        if (player1 or player2) > 10:
            print('please enter a number between 0 and 10', player1name or player2name)
        elif player1 == Dice:
            print("you are so lucky", player1name)
            scores[player1name] += 2
        elif player1 == Dice + 1 or player1 == Dice - 1:
            print("too close :)", player1name)
            scores[player1name] += 1
        elif player1 > Dice + 1 or Dice - 1 < player1 < Dice + 1 or player1 < Dice - 1:
            print("better luck next time", player1name)
        if player2 == Dice:
            print("you are so lucky", player2name)
            scores[player2name] += 2
        elif player2 == Dice + 1 or player2 == Dice - 1:
            print("too close :)", player2name)
            scores[player2name] += 1
        elif player2 > Dice + 1 or Dice - 1 < player2 < Dice + 1 or player2 < Dice - 1:
            print("better luck next time", player2name)
        print("the random number is :", Dice)
        print("round", round, "!", roundsleft, "rounds left!")


def normalgame():
    print(" see if you  are lucky or not")
    print(" system will generated a random number between 0 and 10 try to guess the generated number")
    print(" if you want to quit type 911")
    round = 1
    while True:
        Dice = random.randint(0, 10)
        playernumchoise = int(input("guess the number: "))
        round += 1
        if playernumchoise == 911:
            print("Thank you for playing :)")
            break
        if playernumchoise > 10:
            print('please enter a number between 0 and 10')
        elif playernumchoise == Dice:
            print("you are so lucky")
            print("the random number is :", Dice)
        elif playernumchoise == Dice + 1 or playernumchoise == Dice - 1:
            print("too close :)")
            print("the random number is :", Dice)
        elif playernumchoise > Dice + 1 or Dice - 1 < playernumchoise < Dice + 1 or playernumchoise < Dice - 1:
            print("better luck next time")
            print("the random number is :", Dice)

        print("round", round, "!")


try:
    print(" see if you  are lucky or not")
    print(" system will generated a random number between 0 and 10 try to guess the generated number")
    print(" if you want to quit type 911 (for 2 players mode please type both 911)")
    yesno = input("are you going to play alone or with someone else? answer by yes or no: ")
    if yesno == "yes".lower():
        print("in this gamemode each player needs to guess the random number between 0 and 10, ")
        print("the player who guesses the number gains 2 points")
        print("the player who guessed a number near the random number gets 1 point ")
        print("expl: the random number is 7 but you guessed 6 or 8 you have a point")
        print("the player who didn't guess the number gain no points")
        duogame()
    if yesno == "no".lower():
        normalgame()

except ValueError:
    print("you must put a integer nor a character or floats")
except Exception:
    print("something went wrong")



RE: please help me improve this minigame - j.crater - Jan-21-2019

Hello, thanks for sharing your code!
I just moved the thread to "Completed Scripts/Snippets" subforum, since it is the appropriate place for this kind of thread.
I see a potential for bugs in lines, such as.
if (player1 and player2) == 911:
if (player1 or player2) > 10:
I don't think those conditions do what you want - see if this thread can be useful.
Also as you may notice the Python code tags highlighted the "round" variable. That's because "round" is already used in Python (a function for rounding numbers). It is a good idea to not use reserved and already used keywords in Python, so you'd be better off by changing the variable name (e.g. game_round or whatever).


RE: please help me improve this minigame - Naito - Jan-21-2019

(Jan-21-2019, 11:11 AM)j.crater Wrote: Hello, thanks for sharing your code!
I just moved the thread to "Completed Scripts/Snippets" subforum, since it is the appropriate place for this kind of thread.
I see a potential for bugs in lines, such as.
if (player1 and player2) == 911:
if (player1 or player2) > 10:
I don't think those conditions do what you want - see if this thread can be useful.
Also as you may notice the Python code tags highlighted the "round" variable. That's because "round" is already used in Python (a function for rounding numbers). It is a good idea to not use reserved and already used keywords in Python, so you'd be better off by changing the variable name (e.g. game_round or whatever).
hello! thank you for your suggestions and sorry if i missplaced my thread, i didn't notice that
i don't know why you see the 2 if statements as "bug-making", they are working perfectly (can you please further explain to me ? thank you) i changed these statements from:
if (player1 and player2) == 911:
if (player1 or player2) > 10:
to:
if player1 == 911 and player2 == 911:
if player1 > 10 or player2 > 10:
about the variable "round" i forgot to change the variable name

Thank you!


RE: please help me improve this minigame - j.crater - Jan-21-2019

No problem about misplacing thread :)
I might have misunderstood your intention and the code is indeed doing exactly what you wanted.
You can try this:
player1 = 3
player2 = 911
if (player1 and player2) == 911:
   print("it's true!")
or this.
player1 = 3
player2 = 30
if (player1 or player2) > 10:
   print("it's true!")
And if it sparks interest I can explain further :D


RE: please help me improve this minigame - Naito - Jan-21-2019

(Jan-21-2019, 11:40 AM)j.crater Wrote: No problem about misplacing thread :) I might have misunderstood your intention and the code is indeed doing exactly what you wanted. You can try this:
 player1 = 3 player2 = 911 if (player1 and player2) == 911: print("it's true!") 
or this.
 player1 = 3 player2 = 30 if (player1 or player2) > 10: print("it's true!") 
And if it sparks interest I can explain further :D
OOOPS , well i didn't give much attention to if i want to make one player type 911 and break or both to break the code but for the sake of learning i am interested(if you don't mind)
Thank you!


RE: please help me improve this minigame - j.crater - Jan-21-2019

If you compare values in Python using "and" and "or" like this:
if (player1 and player2) == 911:
if (player1 or player2) > 10:
a Boolean comparison (True/False) is made. In this case, any nonzero number and non-empty string is True. When "and" comparison is used, values are checked sequentially and if they are all True, the last value is returned (in this case value of player2). In case of "or", values are checked sequentially, and when first value which is True is encountered, comparison stops and that value is returned (it may be player1 or player2). It works the same way even if you chain 100 values to be compared instead of just 2. There is no real science behind this, it's just how Python works.

There is maybe a bit of an optimization trick here. If you compare by "and", you can put values that are most likely to be False first, so the checking is over quicker in case they are False. Likewise with "or", you can put values that are most likely to be True first, so checking can be over quicker.
I don't think this is very relevant for your particular code, but just an extra info in case you find it useful to know :)


RE: please help me improve this minigame - Naito - Jan-21-2019

(Jan-21-2019, 12:54 PM)j.crater Wrote: If you compare values in Python using "and" and "or" like this:
 if (player1 and player2) == 911: if (player1 or player2) > 10: 
a Boolean comparison (True/False) is made. In this case, any nonzero number and non-empty string is True. When "and" comparison is used, values are checked sequentially and if they are all True, the last value is returned (in this case value of player2). In case of "or", values are checked sequentially, and when first value which is True is encountered, comparison stops and that value is returned (it may be player1 or player2). It works the same way even if you chain 100 values to be compared instead of just 2. There is no real science behind this, it's just how Python works. There is maybe a bit of an optimization trick here. If you compare by "and", you can put values that are most likely to be False first, so the checking is over quicker in case they are False. Likewise with "or", you can put values that are most likely to be True first, so checking can be over quicker. I don't think this is very relevant for your particular code, but just an extra info in case you find it useful to know :)
THANK YOU SO MUCH !!! your post will save me from many future problems :)


RE: please help me improve this minigame - PythonSnake - Jan-22-2019

i love this game you made!!!
Heart Heart Heart Heart Heart Heart Heart Heart Heart Heart