+ 2

Bigger Dice Game!

This is continuing my first Python project. Still having scoring trouble beyond one turn. This seems a promising approach (line 270) Thanks to all who've helped so far, really appreciated! I also think, overall, the code isn't very pythonic. It feels too clunky and repetitive. Tear it up y'all!! https://code.sololearn.com/cSiVx2RlCuFK/#py

15th Dec 2017, 6:54 PM
Paul
Paul - avatar
5 Respuestas
+ 1
Nice job! A way to work towards making the code less repetitive is to turn the variables that do similar things into arrays. So instead of first_choice, second_choice, etc., make an array called choices! Instead of first_list, second_list, etc., make an array called lists. Slowly the patterns should emerge :P (For the time being: you can even fit functions into an array. `throws = [first_throw, second_throw, ...]` and later `throws[0](dice)`, pretty cool.) If you are feeling adventurous, you could then try to rewrite the throw function, start off with a for loop, and try to make it work. def throw(): for i in range(0, 6): ... You are doing the same kinds of prompts and calculations six times, so this ought to be able to fit in a for loop like that!
15th Dec 2017, 8:10 PM
Schindlabua
Schindlabua - avatar
+ 1
Thank you! I'm having lots of fun and learning tons doing this. :) And, great suggestions! I am feeling adventurous indeed, and will endeavor to eliminate the repetition. I like the idea of the array. Should get rid of the need to declare variables global, too, right? I know that's not very pythonic at all. Thanks again!
15th Dec 2017, 8:24 PM
Paul
Paul - avatar
+ 1
Nice to hear! I like that you're just going out and making stuff too, it's the best way to learn :) Yeah globals are no good. But I think that when some time later you'll get to unify the nth_throw and nth_pick functions into one, they'll go away all by themselves in the process. I wouldn't worry about them too much!
15th Dec 2017, 8:42 PM
Schindlabua
Schindlabua - avatar
+ 1
Yeah, you can pass choices along to the functions that need it. So make it "def choosing(y, choices)" and in your toss function, pass it along, like "choosing(pick(throw(...)), choices)" - that way every function is working on the same array. Of yourse you'll need to remove the "choices = []" from your choosing function too. Hope that helps!
22nd Dec 2017, 2:44 AM
Schindlabua
Schindlabua - avatar
0
So I've got this mostly working with 1/3 the code. I'm still struggling to get the results of one roll to be available to the next. I have the list called choices to handle that. Should be a neat way to control the number of dice rolled in subsequent turns as well. Any ideas on saving the data in that list until the loop runs again? https://code.sololearn.com/cxQxFajDnEhH/#py
21st Dec 2017, 1:44 AM
Paul
Paul - avatar