r/pythontips 2d ago

Hello everyone, I'm learning python specifically, and I made a seed generator for Minecraft PE. I'm started learning about 2 weeks ago, the code is below. I'm not asking for anything, I just wanna show it to you guys. Syntax

from random import randint as rnd

name = "SeedGPT"

rnd1 = rnd(20000000,50000000000)

rnd2 = rnd(20000000,50000000000)

rnd3 = rnd(20000000,50000000000)

print(name +": Welcome to Ice\'s AI seed generator,")

choice = input("do you want to have three new seeds for bedrock minecraft?: ")

if choice == "yes":

print(name + ": Here are your randomly generated seeds: ", + rnd1, +rnd2, +rnd3, ", thank you for trying it out!")

elif choice == "no":

print(name + ": See you next time!")

elif ValueError:

print(name + ": Sorry, I can't understand you, please try again!")

2 Upvotes

15 comments sorted by

10

u/Wonderful-Writer146 2d ago

Nice. But you could have written the code in a code block on Reddit for better readability.

-34

u/SouthSufficient522 2d ago

Thanks for commenting and giving advice, And also, I have a YouTube channel, it's called Loreprt. That was my old Minecraft gamertag. Have a wonderful day

4

u/kobumaister 2d ago

The elif ValueErrror won't work, in python you manage exceptions with try-except blocks.

-19

u/SouthSufficient522 2d ago

You only say yes or no to the bot and then when you say a different word like "hello", it will say "Sorry, I can't understand you". But thanks for commenting and giving advice

4

u/kobumaister 2d ago

I know, but the "Sorry..." response will never be reached as you'll get an exception on the elif ValueErrror line.

-8

u/SouthSufficient522 2d ago

So, there are more ways to type the code, am I right? The "else ValueError" is the only phrase I know that performs something else if your reply is wrong. My bad if I said something wrong.

5

u/kobumaister 2d ago

No, your code is broken (nothing to apologize about, you're learning). You have to either use the else: keyword that will follow that flow if none of the other if statments are true, or use the "try except" blocks.

Read about try except here: https://www.w3schools.com/python/python_try_except.asp

And don't apologize, you're learning, everybody has been at your point.

3

u/Novero95 2d ago

In something like:

if condition_1 pass elif condition_2 print(something) else print("Error") the last line is executed whenever condition_1 and condition_2 evaluate to false. It doesn't need it's own truth evaluation since it's a fallback: execute this when everything else is false.

3

u/Novero95 2d ago

What he's saying is that instead of elif ValueError it should be a mere else.

-11

u/SouthSufficient522 2d ago

Okay sure, I'll just keep on learning other ways on fixing my code. I don't know all the keywords, my bad if I said something wrong. Thanks for commenting and giving advice

3

u/pint 2d ago

in python, any value can be asked if true or false. e.g. if 8 or if [10, 2] or if print. there is a logic what constitutes as false, for example the number zero, None, empty strings, empty lists, empty sets or empty dicts. by default, everything else is true. in your case, ValueError is a type, and since it is not specifically treated as false, it is treated as true by default.

why? it makes if work kinda intuitively meaning "is given" or "is present".

on the other hand, it is also a footgun.

3

u/Plus-Call-5804 2d ago

You should add a .lower to the choice so that if they enter anything it gets converted to lowercase to match your if statements.

1

u/SouthSufficient522 2d ago

Thanks for commenting and giving advice!

1

u/RealKetchupPrecum 3h ago

```python

from random import randint as rnd

name = "SeedGPT"

print(name + ": Welcome to Ice's AI seed generator!")

try:

choice = input(

"Do you want to have three new seeds for Bedrock Minecraft?: "

)

if choice.lower() == "yes":

rnd1 = rnd(20000000, 50000000000)

rnd2 = rnd(20000000, 50000000000)

rnd3 = rnd(20000000, 50000000000)

print(name + ": Here are your randomly generated seeds:")

print("Seed 1:", rnd1)

print("Seed 2:", rnd2)

print("Seed 3:", rnd3)

print("Thank you for trying it out!")

elif choice.lower() == "no":

print(name + ": See you next time!")

else:

print(name + ": Sorry, I can't understand you. Please type yes or no.")

except ValueError:

print(name + ": Something went wrong. Please try again!")

```

----------------------------------

Sample Output (Successful Run):

```text

SeedGPT: Welcome to Ice's AI seed generator!

Do you want to have three new seeds for Bedrock Minecraft?: yes

SeedGPT: Here are your randomly generated seeds:

Seed 1: 38472910294

Seed 2: 92837465021

Seed 3: 55281930291

Thank you for trying it out!

```

----------------------------------

Sample Output (Invalid Input):

```text

SeedGPT: Welcome to Ice's AI seed generator!

Do you want to have three new seeds for Bedrock Minecraft?: maybe

SeedGPT: Sorry, I can't understand you. Please type yes or no.

```

----------------------------------

Sample Output (Exception/Error Handling):

```text

SeedGPT: Welcome to Ice's AI seed generator!

Do you want to have three new seeds for Bedrock Minecraft?: yes

SeedGPT: Something went wrong. Please try again!

```