//Post 4: Getting Back to Python//
Back ../22.06.29
Python, Scripting, Dice
Ahoy!
So I'm back looking for ways to improve my skills and increase my employability. The thought ocurrd to me that it's been a while since I did anything with Python. Scripting and automation are not just desirable, but often essential for the more interesting roles I could be applying for.
To get myself back into the swing of writing scripts, I started out with a simple thinger to roll some dice. The user can pick how many sides and how many of said die. I was pleased at how easily it came back to me, so building up from here shouldn't be an issue.
From what I recall, we don't really want Python scripts to involve user interaction. Makes it harder to automate if it's sat waiting for user input. The next steps are to move towards increased complexity, automation, and running it entirely from the command line with no user input save the initiation of the script. What the next script will do, however, will have to be seen... Maybe I'll rebuild my packet analyer from last year's university module.
Script included below.
- Adam
oh lawdi it messed up my white spacing :^(
let's put that on my to-do list...
import random def roller(sides, dice): die = random.randint(1, sides) total = die * dice return total def dice_picker(): sides = 0 dice = 0 while True: try: sides = int(input("How many sides:")) break except Exception as err: print("Invalid input, enter an integer only.") while True: try: dice = int(input(f'How many D{sides}:')) break except Exception as err: print("Invalid input, enter an integer only.") print(f'\nResult:{roller(sides, dice)}') def print_menu(): print("=======MENU=======") print("* 1) Roll dice") print("* 2) Quit") print("==================\n") def main(): while True: print_menu() while True: try: menu_input = int(input("Input:")) if menu_input == 1: menu_choice = 1 break elif menu_input == 2: menu_choice = 2 break else: None except Exception: print("Invalid, enter 1 or 2") if menu_choice == 1: while True: dice_picker() while True: again = input("\nRoll again? Y/N:") if again == "Y": break elif again == "N": break else: print("Invalid entry. Roll again Y/N:") if again == "Y": None elif again == "N": print("\n") break else: break if __name__ == '__main__': main()