Python Forum
5 mini programming projects for the python beginner
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
5 mini programming projects for the python beginner
#1
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')
Reply
#2
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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
Thanks!
Reply
#4
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
You can create a list with colors and number and use randrange function. Here is the sample program which I tried myself.
import random #import random function
input('Press any key to roll your Dice:')
color=['Red','Blue','Green','yellow','Pink','Black'] #declaring list of 6 colors of 6 sides.
op=random.randrange(1,7) #selecting random number from 1 to 6
op1=random.choice(color) #selecting random color from color list

print("Your Dice is",op1,"colour with number",op) #printing the output.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Visual Python Programming flows Joost 0 2,648 Jun-06-2021, 06:51 AM
Last Post: Joost
Photo All 31 of my Python projects with source code steve_shambles 2 4,091 Mar-23-2020, 12:08 AM
Last Post: steve_shambles
  Guess the dice roll mini-game tawnnx 6 7,245 May-22-2018, 02:12 PM
Last Post: malonn
  My first python projects (help with reviews and opinions) wilfredinni 0 7,954 Mar-06-2018, 02:33 PM
Last Post: wilfredinni
  Use this integer and string encrypter and decrypter module in your projects Unlimiter 4 4,220 Jan-05-2018, 11:32 AM
Last Post: Unlimiter
  Mini-Web Framework nilamo 12 9,590 Jun-15-2017, 05:32 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020