Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

• 1 Vote(s) - 5 Average
• 1
• 2
• 3
• 4
• 5
 5 mini programming projects for the python beginner kerzol81 Programmer named Tim Posts: 13 Threads: 5 Joined: Jan 2017 Reputation: 0 Likes received: 0 #1 Apr-17-2017, 07:26 AM (This post was last modified: Apr-18-2017, 05:07 AM by kerzol81. Edited 2 times in total. Edit Reason: Minor bug ) Hi, I've found a nice tutorial which I've already started. I'd like to now how could my solutions be better, let's say more pytonic. I'm really into python, I'd like to have strong foundation, while learning it. Please if you have time, and can provide clearer, easier, more pythonic code, please share it with me. 1. Dice Rolling Simulator version A: ```import random import time def castDie():    input('Press any key to cast the die!')    r = list(range(1, 7))    print('Result: ' + str(random.choice(r))) while True:    time.sleep(1)    castDie() ```1. Dice Rolling Simulator version B: ```import random def question():         input('Press enter to roll!') def roll():     n = [1, 2, 3, 4, 5, 6]     print(random.choice(n)) question() roll()```2. Guess The Number: ```import random n = random.randrange(1, 101) print('I\'ve though a number between 1 and 100!') while True:     try:         g = input('Guess!')         g = int(g)         if not 100 > g > 0:             print('It\'s in between 0 and 100!')     except ValueError:         print('Enter an integer')         continue     if g == n:         print('Congratulations!')         break     if g < n:         print('Larger')     if g > n:         print('Smaller')``` Quote Quote sparkz_alot Posts: 795 Threads: 30 Joined: Sep 2016 Reputation: 17 Likes received: 167 #2 Apr-17-2017, 01:14 PM If you use double quotes, you will not need to escape the single quote. ```print("I've thought of  a number between 1 and 100!")    # also corrected the grammar a bit :-) ```you could also change this: ```g = input('Guess!') g = int(g) ```to this: ```g = int(input('Guess')) ```Keep indentation uniform (preferred = 4 spaces), notice in your second example, the indentation is not the same.  Try to use 'descriptive' variables, function and class names, etc. rather than single letters.  You will thank yourself in the future. kerzol81 likes this post If it ain't broke, I just haven't gotten to it yet. OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Jessie" Python 3.6.2, IDE: PyCharm 2017 Community Edition Quote Quote kerzol81 Programmer named Tim Posts: 13 Threads: 5 Joined: Jan 2017 Reputation: 0 Likes received: 0 #3 Apr-18-2017, 05:05 PM Thanks! Quote Quote Skaperen Giant Foot Posts: 918 Threads: 231 Joined: Sep 2016 Reputation: 0 Likes received: 26 #4 Apr-20-2017, 02:39 AM (This post was last modified: Apr-20-2017, 02:49 AM by Skaperen. Edited 1 time in total.) and avoid using tabs for indentation ... for anything except tab-separated-value data files, but especially avoid tabs for indentation. i'd like to see a graphical version of dice rolling simulators that allows setting the color of the die and the eyes, and the number of dies.  it would be plus to generate a video of them being rolled. kerzol81 likes this post Skaaaaaperen........ Quote Quote « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Mini-Web Framework nilamo 12 802 Jun-15-2017, 05:32 AM Last Post: Skaperen Collection of programming ideas and challenges metulburr 6 3,553 Feb-06-2017, 07:49 PM Last Post: Larz60+

Forum Jump:

Users browsing this thread: 2 Guest(s)