Python Help

I need help with non-makecode python. I’m trying to put the number of tries it took to get the number. Help?

3 Likes

@UnsignedArduino?

3 Likes

Well, first this:

  • A MakeCode Arcade forum, so not really relevant but ok sure :joy:
  • And it would be easier to post the text, not an image of the text so we don’t have to type it all out and we can copy and paste

First, I’ve formatted a bit to my liking :sweat_smile: sorry I’m picky

from random import randint

correctNumber = randint(0, 100)

while True:
    guessedNumber = int(input("Guess the number: (0 - 100) "))
    if guessedNumber == correctNumber:
        print("Correct!")
        print("Good job!")
        break
    elif guessedNumber < correctNumber:
        print("Too low!")
    elif guessedNumber > correctNumber:
        print("Too high!")

print("You win!!!")

If you want to count how many guesses it took the player, you need a variable that starts at 0 and increments every time the player makes a guess. Then display it at the end.

Try to figure it out yourself but if you are stuck here is the answer behind the spoiler >:)

from random import randint

correctNumber = randint(0, 100)
guesses = 0  # Define a variable to count the number of guesses

while True:
    guessedNumber = int(input("Guess the number: (0 - 100) "))
    guesses += 1  # Increment every time the player guesses
    if guessedNumber == correctNumber:
        print("Correct!")
        print("Good job!")
        break
    elif guessedNumber < correctNumber:
        print("Too low!")
    elif guessedNumber > correctNumber:
        print("Too high!")

# The f-string formatting was introduced in Python 3.6 - if you are on a older version you can do something like this:
# (Although I highly recommend upgrade to 3.9 if you aren't already - many new fun features are being added all the time!)
# print("You win!!! It took you {0} guess(es)".format(guesses))
# Otherwise stick to f-strings as it's more "Pythonic"
print(f"You win!!! It took you {guesses} guess(es)")
# Or if the above is too confusing, you can do something like this:
# print("You win!!! It took you " + str(guesses) + " guess(es)")
# Note that since guesses is a number, you can not add it to a string without first turning the number into a string
3 Likes

Thank you soooo much! I am new to python and It’s hard. thank you!

Edit: Variables are tricky like that! why do you have to have the f in front of the Quotations?

2 Likes

Ah it takes time but you will learn - if you ever need help just post here!

1 Like

OK!
why do you have to have the f in front of the Quotations?

2 Likes

Search up f-strings, they are really cool. Basically if you put f in from of a string then anything within brackets ({}) will be considered a statement which will be converted automatically to a string before being inserted there.

So something like this:

>>> x = 0
>>> print(f"The value of x is {x}")

will print:

The value of x is 0
1 Like

Ohhh, thanks so much!!

2 Likes

I know this is more like minecraft, but would it be possible to make a minecraft mod using python @UnsignedArduino ?

1 Like

You can use MakeCode for Minecraft:

But it only works if you use Minecraft Education Edition. But it can accept chat commands and do lots of fun stuff like spawn mobs, set blocks.

For Minecraft Java Edition, you can use the Raspberry Pi Juice plugin:

But it wouldn’t be like a mod - it’s pretty limited and it can’t accept chat commands.

1 Like

I mean like actual python for like [forge] minecraft?

1 Like

Then I don’t know, sorry :frowning:

1 Like

No. Mods are strictly written with either java language or c++. Also, as I had said before, you’re better off using a mod creator like mCreator

1 Like

Well…if someone wrote an bridge between the Java APIs and Python you could write a mod in Python XD

1 Like

ah, thanks for the help

1 Like

Well, there already probably are, but still, it would be written in java (for java) or c++ (for bedrock) at its deepest. At least mCreator has a block workspace environment along with text based.

1 Like

Now I’m working with Java. It is Much harder! T_T(jk);

1 Like

Good luck! (I’ll be learning Java next year too! We’ll suffer together)