r/learnpython • • 7h ago

My first complete comand in Python, from my first day on:

Look:

import random
import time

def drawn(a, b):
    return random.randint(a, b)
answer = 'yes'

while answer == 'yes':
    a = int(input('Min number: '))
    b = int(input('Max number: '))
    print(f'Drawn: {drawn(a, b)}')

    time.sleep(2)

    answer = input('Try again? ').lower()

This creates a random number generator where you can choose the minimum and maximum numbers!

18 Upvotes

10 comments sorted by

28

u/fazzah PyQt | SQLAlchemy | ReportLab 7h ago

good. now find a way to protect the script if someone inputs "foo"

•

u/ActuatorGlittering94 21m ago

do you mean just catching the ValueError on int() or also handling cases like b being smaller than a?

2

u/kilkil 3h ago

hell yeah! :D

what are you going to do next?

3

u/Available_Safe1818 5h ago

Very nice. Put one empty line after your function definition.

1

u/EnvironmentalDot9131 5h ago

This is good but need improvements

-4

u/zanfar 6h ago
  1. PEP8
  2. What is the point of drawn() if it accepts the exact same arguments as random.randint()? Why is it named as a passive verb when it does something?
  3. Validate your inputs. Always.
  4. answer is badly named. There is zero context until the script is done.
  5. Why are you deliberately making your user wait? sleep() has almost no place in U/I.

10

u/DeafeningSlience 5h ago

It is OP's first script, so take it easy on 'em lol

Nevertheless, good feedback for more intermediate/advanced learners.

2

u/AmanBabuHemant 3h ago

I think the sleep is a good touch for UX

-1

u/Fast-Station1106 3h ago

jetzt hört mal auf, von 0 auf das oben? am ersten Tag?

so eine Quatsch zu posten versaut allen Anfängern doch komplett die Lust neu einzusteigen