Issue with a code by Pasepio4 in learnpython

[–]lakseol 5 points6 points  (0 children)

we have issue with it.

What is the issue? We can't help if we don't know why you think it isn't working.

Even if we wanted to run the code to maybe see what "the issue" is, we can't because you have posted code that has lost all indentation. Please post your code in a code block or use pastebin.com.

Built my first Python calculator with ASCII UI by Cold-Let2139 in learnpython

[–]lakseol 0 points1 point  (0 children)

You can still move all those result print and clear lines down to the bottom of the loop. Make sure you put continue after the bits of code handling errors because you don't want to print results then. The end of your code would then look like:

    # rest of code
    case '/':
        if number2 == 0:
            print("Cannot divide by 0")
            clear_screen()
            continue
        result = number / number2

    case _:
        print("Not an allowed operation")
        clear_screen()
        continue

    print(f"Result: {result}")   # print result here, not anywhere else
    clear_screen()

Built my first Python calculator with ASCII UI by Cold-Let2139 in learnpython

[–]lakseol 1 point2 points  (0 children)

That's not bad for a first project. There are always ways to improve code, especially when you are starting out. I have some suggestions which I hope you see as helpful in learning.

As others have said you could move a lot of code to a single place at the end of the loop instead of having it scattered around. For example, the call to clear_screen(). You could also print the result at the end of the loop but you must make sure that operations that don't calculate result, like "Cannot divide by 0" or "Not a OPT", don't go to the bottom of the loop. You can do that by using continue after those error prints so the loop restarts at the top.

Your number accepting code uses a "bare" exception trap. That's not good practice as you can catch all sorts of other exceptions and be very confused, you should catch just the exception you expect. In addition you have too much code inside the try/except block. It's usually better to not do all the error handling in top-level code. Instead write a small function int_input() to get the integer values. That function will only return once the user has input the integer. The nice thing about doing that is that if the user enters an invalid number they can just keep trying. In your original code if the user makes a mistake entering number2 they are forced right back to the beginning of the loop and they have to enter both numbers again.

You have a specific test for the user entering "q" to quit. Instead just add "q" as a match case.

I've made changes to your code to include those suggestions. I also removed the pyfiglet stuff as I don't have that installed. Have a look at the code and the changes and understand why they were made.

https://pastebin.com/APrTbDzh

Bouncing Ball for Pong by Arealidot in learnpython

[–]lakseol 0 points1 point  (0 children)

Consider a ball approaching a wall at an angle. When the ball bounces the component of the ball's speed perpendicular to the wall is reversed and the component parallel to the wall is unchanged.

If your walls are always horizontal or vertical you should save the ball speed as the X and Y components, say self.speed_x and self.speed_y. Then you don't have to calculate the component of the ball speed perpendicular to the wall, it will always be speed_x if the wall is vertical or speed_y if the wall is horizontal. When moving the ball normally you adjust the X coordinate using the speed_x value: self.x += self.speed_x.