+ 1

My First Project

Hi Guys, Iā€™m new to coding, i done the java course first and then the python. Python took my interest, i done the tutorial along with help online and from the great comments section. I got through it with a fairly decent understanding in about 2 days. I looked online for good projects for (python)beginners. Number 1 was a rolling dice gen. here is my attempt but iā€™m having some teething snags. Any comments, good or bad will be appreciated but i want it to work. Thanks >>> """ Dice Rolling Sim""" import random print ("Welcome to the Dice Rolling sim!") class RollTheDice(): def __init__(self): self.min = 1 self.max = 6 super(RollTheDice, self). __init__() def ask_something(self): while True: userinput = input("Roll Again? Yes or No?" + "\n") if (userinput == 'yes'): self.rollDice() else: print("You Didn't type 'Yes'. No roll, Goodbye!.") exit() def rollDice(self): print("rolling the dice!") print("The values are...") print (random.randint(self.min, self.max)) return RollTheDice = rollDice() RollTheDice.ask_something() >>>

15th Jun 2018, 11:11 AM
Jordan Pullinger
3 Answers
+ 2
Hey! Welcome to the magical world of coding! So I see what you were trying to do there and its pretty good! The first little tip id give you is in oop (Object Oriented Programing) you want your object to be a noun. So while RollTheDice is descriptive, Dice makes more sense to me as a classname. The while loop is where you might want to consider rewriting some things. Typically a while loop has a condition that will run the loop as long as the condition evaluates to true and when it evaluates to false it jumps out of the loop. Right now it appears you have it constantly evaluating to True and then you just exit out of the loop when the user types no. I would consider doing something like this. def ask_something(): userinput = None while userinput != "no": userinput = input("Roll Again? Yes or No?") if (userinput == 'yes'): rollDice() else: print("You Didn't type 'Yes'. No roll, Goodbye!.") You may also want to add a way to ensure you will get the right results if your user types adds capitalization to the input. I believe you can call userinput.lower() to convert to lowercase before it is used in comparisons.
15th Jun 2018, 12:19 PM
Haon1919
Haon1919 - avatar
0
Noah, thanks for the time for your reply. and yeah the convert idea at the end if great. iā€™m still having problems. my ā€˜rolldiceā€™ at the isnā€™t definfed. https://code.sololearn.com/cxlTT4kQZ9le/?ref=app
15th Jun 2018, 2:55 PM
Jordan Pullinger
0
you wouldnt call rollDice anymore to create that dice opject. it would be Dice()
15th Jun 2018, 3:06 PM
Haon1919
Haon1919 - avatar