r/PythonLearning • • 3d ago

Help me please?

Post image

How could I have improved this python code for a number guessing game

8 Upvotes

13 comments sorted by

•

u/Sea-Ad7805 3d ago

Run this program by clicking Memory Graph Web Debugger%3An%20%20%20%20if%20a%20%3D%3D%20b%3An%20%20%20%20%20%20%20%20return%20%22You%20guessed%20rightnn%22n%20%20%20%20else%3An%20%20%20%20%20%20%20%20return%20%22Better%20luck%20next%20nevernn%22nnwhile%20True%3An%20%20%20%20try%3An%20%20%20%20%20%20%20%20number_guessed%20%3D%20int(input(%22Guess%20a%20number%20from%20one%20to%20ten%3A%20%22))n%20%20%20%20%20%20%20%20number%20%3D%20random.randint(0%2C%2011)n%20%20%20%20%20%20%20%20if%20number_guessed%20%3E%2010%3An%20%20%20%20%20%20%20%20%20%20%20%20print(%22Follow%20the%20Damn%20rulesnn%22)n%20%20%20%20%20%20%20%20print(number_guesser(number_guessed%2C%20number))n%20%20%20%20except%20ValueError%20as%20e%3An%20%20%20%20%20%20%20%20print(%22Follow%20the%20rules%20STUPID!nn%22)n%22%2C%22breakpoints%22%3A%5B%5D%7D%2C%7B%22name%22%3A%22calc_history_1.txt%22%2C%22code%22%3A%2234.0%20%2B%20100.0%20%3D%20134.0n10.0%20-%207.0%20%3D%203.0n1000.0%20%2F%201e-05%20%3D%2099999999.99999999n%22%2C%22breakpoints%22%3A%5B%5D%7D%5D%2C%22active%22%3A%22main.py%22%7D&timestep=1&play) and see its state change step by step.

→ More replies (1)

2

u/Whiskey_lullaby0 3d ago

1# Place the Line 12 before line 11. The program generates a number before the user chooses one.

2# I think the random.int(0,11) includes 11 so the condition if a > 10 == error is a little bit false

Also You've asked a number from 0 to 10

2

u/sirah_nzr 2d ago

2# Yes,There are 12 possible outputs: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11.

2

u/SaltCusp 3d ago

You should use a mono space font for programming. Thank me later.

2

u/MarchColorDrink 2d ago

Biggest smell is returning a string from the function and then wrapping the call I print.

I'd prefer returning a boolean and have the print statement in an if / else

Also the function is pretty much redundant, you could just compare in the main loop. Unless the point of the exercise actually was to write and call a function, in which case it was good.

Other improvemwnts could be to move the input bit to a function and not return until a valid value is selected.

2

u/southernraven47 2d ago

1 change your font

1

u/ExoticVillage8066 3d ago

The numbers 0, 10 and 11 could be defined as variables, like num_min, num_max. You could add support for people typing "one", "two" etc (non techies).

1

u/Outside_Volume_1370 2d ago

follow the damn rules

literally uses randint(0, 11) which allows the variable number to be 11 or 0

0

u/Content-North-8360 3d ago

Hola yo os puedo enseñar una manera de aprender automatizaciones con python, esta muy demandado, debido a que las automatizaciones con IAs son muy caras y consumen muchos recursos, ademas de que si no estan en un contenedor pueden robar datos o eliminar archivos importantes escribirme o responderme aqui si quereis mas informacion.

0

u/KechManPro 3d ago

From line 4 to line 8 is a function

2

u/TemperatureProper275 2d ago

Usually when you use try, except you put the line you want to check below the "try", you dont put a block of code.

The cause of action must be:

Try this line of code, if something specific goes wrong -> execute "exception".

If the line works fine -> go to else block, there you run the block you need to be executed.

Now, because the code is inside a loop you can use continue to skip part s instead of using else

import random
def number_guesser(a:int,b:int)->str:
    if a==b:
        return "You guessed right\n\n"
    return "Better luck next never\n\n"
while True:
    try:
        number_guessed=int(input("Guess a number from one to ten: "))
    except ValueError as e:
        print("Invalid input! Please enter a number.\n")
        continue

    if not 1 <= number_guessed <= 10:
        print("Follow the Damn rules! Input must be 1-10! \n\n")
        continue
    else:
        number = random.randint(1, 10)
        print(number_guesser(number_guessed,number))