0

A teacher gives 100-point exams that are graded on the scale
90-100:A, 80-89:B, 70-79:C, 60-69:D, 50-59:E, <50:F

Write a string where the score applied to the index gives the grade letter.

Hint print('F'*50)

Edited by vegaseat

2
          *
         ***
        *****
       *******
      *********
     ***********
          |

Write Python code to print a little tree like this.

1

Make a program that can open up text files and count the number of characters that are in it.
Using that as a starting point have it count the number of words.
Then make it create an average for letters per word.

0

I have a quite simple idea that should entertain any novice programmer for a little while. My idea is for the the user to output timestables. I've created this programmer as an example for code.

import random
import time

def welcome():
    global questionAmount
    print "-" * 60
    print "                   Times Table Generator"
    print "-" * 60

def timestables(n):
    global startTime
    global questions
    questions = 1
    correct = 0
    startTime = time.time()
    while questions < int(questionAmount) + 1:
        operandOne = random.randint(1,12)
        operandTwo = random.randint(1,12)
        answer = operandOne * operandTwo
        question = raw_input(str(operandOne) + " x " + str(operandTwo) + " = ")
        if(int(question) == int(answer)):
            print "Correct\n"
            #sleep(1)
            questions = questions + 1
            correct = correct + 1
        else:
            print "Incorrect\n"
            #sleep(1)
            questions = questions + 1
    print "You got " + str(correct) + "/" + str(questions -1) + " right!"

welcome()    
contin = "y"
while contin == "y":
    questionAmount = raw_input("How many questions would you like?: ")
    timestables(questionAmount)
    finishTime = time.time()
    length = finishTime - startTime
    length = "{0:.2f}".format(round(length,2))
    print "It took you: " + str(length) + " second(s) to complete."
    average = float(length) / questions
    contin = raw_input("Would you like to continue?(Y/N)")
    contin = contin.lower()

Now if you wanted to clean this up a bit you can simply import the module 'os' and use os.system('cls') or os.system('clear') depending on whether or not you use windows or mac. If you want to make the program automatically detect which Operating System a user is running then import the platform module and use this:

import platform
import os

def clear():
    operSys = platform.system()
    if operSys.lower() == "windows":
        os.system('cls')
    else:
        os.system('clear')

This is justa simple program but it should allow for novice learners of the python programming language to learn how to create something like this.

Hope I was able to help!

Happy programming.

-1

I've made another program since I posted the last. It simply calculates the areas of different shapes with the given values. For example if you have a circle you can give it the radius and it will calculate the area for you. It accepts Triangle, Rectangle, Circle, and Square.

Here is the code should you need guidance.

import time
import random
import math
#import os
#import platform

#def clear():
#   operSys = platform.system()
#   if operSys.lower() == "windows":
#       os.system('cls')
#   else:
#       os.system('clear')

def welcome():
    #clear()
    print "-" * 60
    print "                       Calculate Area"
    print "-" * 60

def calculateArea(length, width, unit, shape, base, height, radius):
    #clear()
    if shape.lower() == "square":
        area = int(length) * int(length)
        print "The area of a square with sides of " + str(length) + str(unit) + " long, is " + str(area) + str(unit) + "."

    elif shape.lower() == "rectangle":
        area = int(length) * int(width)
        print "The area of a rectangle with a length of " + str(length) + str(unit) + " and a width of " + str(width) + str(unit) + " is " + str(area) + str(unit)

    elif shape.lower() == "triangle":
        if base == None or height == None:
            print "Error: Base and/or Height not provided. Exiting"
        else:
            area = 0.5 * (int(base) * int(height))
            print "The area of a triangle with a base of " + str(base) + str(unit) + " and a height of " + str(height) + str(unit) + " is " + str(area) + str(unit) + "."

    elif shape.lower() == "circle":
        if radius == None:
            print "Error: Radius not given. Exiting"
        else:
            area = math.pi * (int(radius) * int(radius))
            print "The area of a circle with a radius of " + str(radius) + " is " + str(area) + str(unit) + "."



welcome()
shape = raw_input("Please enter the Shape of the object we're trying to calculate: ")
if shape.lower() == "circle":
    radius = raw_input("Please enter the radius: ")
elif shape.lower() == "triangle":
    base = raw_input("Please enter the base: ")
    height = raw_input("Please enter the height: ")
elif shape.lower() == "rectangle":
    length = raw_input("Please enter the length: ")
    width = raw_input("Please enter the width: ")
elif shape.lower() == "square":
    length = raw_input("Please enter the length: ")

unit = raw_input("Please enter the unit of the object: ")

if shape.lower() == "square":
    calculateArea(length, None, unit, shape, None, None, None)
elif shape.lower() == "rectangle":
    calculateArea(length, width, unit, shape, None, None, None)
elif shape.lower() == "triangle":
    calculateArea(None, None, unit, shape, base, height, None)
elif shape.lower() == "circle":
    calculateArea(None, None, unit, shape, None, None, radius)

Enjoy~

Happy programming.

Votes + Comments
Use code snippet section not this sticky for code samples!
0

Create the game called pica centro. The premise of the game is to use deductive reasoning to figure out what the secret number is(the secret number can be randomly generated or manually enter by a different user, thus insinuating that there will be two modes,that is, single and multiplayer.). A pica means the user guess the right digit it is just in the incorrect location. A centro means that the user guessed the right digit, and the digit is in the right location. Usually, the game uses three digit numbers as the secret number; however to make it more challenging or to give the game difficulty level allow the user,if on single player mode, to select the size of the secret number.
example output:
secretNumber = 327<---note you should look at each specific digit
userGuess = 123
pica: 1 centro: 1<---note the 2 is in the correct location but the 3 is not
//note that each time the pica and the centro counters should be clear so the user knows what the results were of the previous guess. Lastly, watch for special cases like:
secretNumber = 223
userGuess = 322

0

US weather forecasters measure rainfall in inches of water on the ground, in many other countries rain is measured in liters per square-meter. Write a Python program that converts these different measurements.

1

A good approximation of the cosine of a given angle can be obtained with the following series calculation:

''' cos_approximate102.py
based on series ...
cos(x) = 1 - x2/2! + x4/4! - x6/6! + ..
'''

import math

theta = 45
x = math.radians(theta)
cosx = 1
alt = 1
for n in range(2, 15, 2):
    # alternate between 1 and -1
    alt *= -1
    cosx += alt*math.pow(x, n)/math.factorial(n)
    #print(cosx)  # test

print("Approximate cos({}) = {}".format(theta, cosx))
print("Module math cos({}) = {}".format(theta, math.cos(x)))

''' result ...
Approximate cos(45) = 0.707106781187
Module math cos(45) = 0.707106781187
'''

Write a Python program to do the calculation for this series:
sin(x) = x - x3/3! + x5/5! - x7/7! + ..

0

It's always fun to make a battle game, such as one similar to the popular game Pokémon. Start of by asking the basics. Then do random numbers for different enemies. I will later post an example.

0

I have a project on my mind, to create a menu list. Where from that menu, I choose a category, and from that category, once I choose it, some commands will run to do certain types of tasks (like restart an app or service). However, I am new in python, which means that it will take some time. If someone has some ideas I would be happy to hear them.

0

You have a large multiline string. In what GUI toolkit widget would you display this string so that it can be scrolled but not edited?

0

Write a Python program that copies files from one directory to another, but excludes files with a specified extension.

1

Make a text based adventure game much like zork. All you have to have is a basic knowlege of functions and printing. You just make a function with each location/event and then the event is based on the input. There are many tutorials for this online but it is fun and extremely satisfying

2

The Moon's mass is 0.0123 times the Earth's mass.
Its Density is 0.607 times the Earth's density.
The Moon's radius is 1737.5 km (1079.6 miles).
What is the Earth's radius?

Solve it with a Python program.

Edited by vegaseat

0

Explore the behaviour of a function with multiple decorators.

Edited by vegaseat

0

Use a GUI toolkit like Pyside to create a telephone pad that emits the proper tone for each key pressed.

0

Use the result (list of lists) of the following program ...

''' data_2013_US_population_by_age_and_gender.py
copied the raw data string from:
http://www.census.gov/popclock/
convert to a list of lists for processing
'''

# 2013 US population by age and gender
data = '''\
Age Male % of Population    Female % of Population
0   0.64%   0.61%
1   0.64%   0.61%
2   0.65%   0.62%
3   0.64%   0.62%
4   0.64%   0.62%
5   0.67%   0.64%
6   0.67%   0.64%
7   0.66%   0.64%
8   0.66%   0.63%
9   0.66%   0.64%
10  0.66%   0.63%
11  0.65%   0.63%
12  0.67%   0.64%
13  0.68%   0.65%
14  0.67%   0.64%
15  0.67%   0.64%
16  0.67%   0.64%
17  0.68%   0.65%
18  0.7%    0.66%
19  0.71%   0.67%
20  0.72%   0.68%
21  0.74%   0.7%
22  0.75%   0.72%
23  0.75%   0.72%
24  0.72%   0.69%
25  0.71%   0.68%
26  0.69%   0.67%
27  0.7%    0.67%
28  0.7%    0.68%
29  0.67%   0.66%
30  0.68%   0.67%
31  0.68%   0.68%
32  0.67%   0.67%
33  0.69%   0.68%
34  0.65%   0.64%
35  0.63%   0.63%
36  0.62%   0.63%
37  0.61%   0.61%
38  0.63%   0.63%
39  0.6%    0.61%
40  0.61%   0.62%
41  0.64%   0.65%
42  0.68%   0.69%
43  0.69%   0.7%
44  0.65%   0.66%
45  0.64%   0.65%
46  0.64%   0.65%
47  0.65%   0.66%
48  0.69%   0.7%
49  0.7%    0.72%
50  0.7%    0.72%
51  0.7%    0.72%
52  0.71%   0.73%
53  0.71%   0.74%
54  0.69%   0.72%
55  0.68%   0.71%
56  0.67%   0.71%
57  0.65%   0.69%
58  0.64%   0.68%
59  0.61%   0.66%
60  0.59%   0.63%
61  0.56%   0.61%
62  0.54%   0.6%
63  0.53%   0.58%
64  0.52%   0.57%
65  0.51%   0.56%
66  0.53%   0.59%
67  0.39%   0.43%
68  0.38%   0.43%
69  0.37%   0.42%
70  0.38%   0.43%
71  0.33%   0.38%
72  0.3%    0.35%
73  0.28%   0.33%
74  0.26%   0.31%
75  0.25%   0.3%
76  0.23%   0.28%
77  0.21%   0.27%
78  0.2%    0.26%
79  0.18%   0.24%
80  0.17%   0.23%
81  0.16%   0.22%
82  0.15%   0.22%
83  0.14%   0.21%
84  0.12%   0.19%
85  0.11%   0.18%
86  0.1%    0.17%
87  0.09%   0.15%
88  0.07%   0.14%
89  0.06%   0.12%
90  0.05%   0.11%
91  0.04%   0.09%
92  0.03%   0.08%
93  0.02%   0.06%
94  0.02%   0.04%
95  0.01%   0.04%
96  0.01%   0.03%
97  0.01%   0.02%
98  0%  0.01%
99  0%  0.01%
100+    0%  0.02%'''

# create a list of [age, male%, female%] lists
data_list = []
for line in data.split('\n'):
    data_list.append(line.split('\t'))

# test
import pprint
pprint.pprint(data_list)

''' 2013 US population by age and gender:
[['Age', 'Male % of Population', 'Female % of Population'],
 ['0', '0.64%', '0.61%'],
 ['1', '0.64%', '0.61%'],
 ['2', '0.65%', '0.62%'],
 ['3', '0.64%', '0.62%'],
 ['4', '0.64%', '0.62%'],
 ['5', '0.67%', '0.64%'],
 ['6', '0.67%', '0.64%'],
 ['7', '0.66%', '0.64%'],
 ['8', '0.66%', '0.63%'],
 ['9', '0.66%', '0.64%'],
 ['10', '0.66%', '0.63%'],
 ['11', '0.65%', '0.63%'],
 ['12', '0.67%', '0.64%'],
 ['13', '0.68%', '0.65%'],
 ['14', '0.67%', '0.64%'],
 ['15', '0.67%', '0.64%'],
 ['16', '0.67%', '0.64%'],
 ['17', '0.68%', '0.65%'],
 ['18', '0.7%', '0.66%'],
 ['19', '0.71%', '0.67%'],
 ['20', '0.72%', '0.68%'],
 ['21', '0.74%', '0.7%'],
 ['22', '0.75%', '0.72%'],
 ['23', '0.75%', '0.72%'],
 ['24', '0.72%', '0.69%'],
 ['25', '0.71%', '0.68%'],
 ['26', '0.69%', '0.67%'],
 ['27', '0.7%', '0.67%'],
 ['28', '0.7%', '0.68%'],
 ['29', '0.67%', '0.66%'],
 ['30', '0.68%', '0.67%'],
 ['31', '0.68%', '0.68%'],
 ['32', '0.67%', '0.67%'],
 ['33', '0.69%', '0.68%'],
 ['34', '0.65%', '0.64%'],
 ['35', '0.63%', '0.63%'],
 ['36', '0.62%', '0.63%'],
 ['37', '0.61%', '0.61%'],
 ['38', '0.63%', '0.63%'],
 ['39', '0.6%', '0.61%'],
 ['40', '0.61%', '0.62%'],
 ['41', '0.64%', '0.65%'],
 ['42', '0.68%', '0.69%'],
 ['43', '0.69%', '0.7%'],
 ['44', '0.65%', '0.66%'],
 ['45', '0.64%', '0.65%'],
 ['46', '0.64%', '0.65%'],
 ['47', '0.65%', '0.66%'],
 ['48', '0.69%', '0.7%'],
 ['49', '0.7%', '0.72%'],
 ['50', '0.7%', '0.72%'],
 ['51', '0.7%', '0.72%'],
 ['52', '0.71%', '0.73%'],
 ['53', '0.71%', '0.74%'],
 ['54', '0.69%', '0.72%'],
 ['55', '0.68%', '0.71%'],
 ['56', '0.67%', '0.71%'],
 ['57', '0.65%', '0.69%'],
 ['58', '0.64%', '0.68%'],
 ['59', '0.61%', '0.66%'],
 ['60', '0.59%', '0.63%'],
 ['61', '0.56%', '0.61%'],
 ['62', '0.54%', '0.6%'],
 ['63', '0.53%', '0.58%'],
 ['64', '0.52%', '0.57%'],
 ['65', '0.51%', '0.56%'],
 ['66', '0.53%', '0.59%'],
 ['67', '0.39%', '0.43%'],
 ['68', '0.38%', '0.43%'],
 ['69', '0.37%', '0.42%'],
 ['70', '0.38%', '0.43%'],
 ['71', '0.33%', '0.38%'],
 ['72', '0.3%', '0.35%'],
 ['73', '0.28%', '0.33%'],
 ['74', '0.26%', '0.31%'],
 ['75', '0.25%', '0.3%'],
 ['76', '0.23%', '0.28%'],
 ['77', '0.21%', '0.27%'],
 ['78', '0.2%', '0.26%'],
 ['79', '0.18%', '0.24%'],
 ['80', '0.17%', '0.23%'],
 ['81', '0.16%', '0.22%'],
 ['82', '0.15%', '0.22%'],
 ['83', '0.14%', '0.21%'],
 ['84', '0.12%', '0.19%'],
 ['85', '0.11%', '0.18%'],
 ['86', '0.1%', '0.17%'],
 ['87', '0.09%', '0.15%'],
 ['88', '0.07%', '0.14%'],
 ['89', '0.06%', '0.12%'],
 ['90', '0.05%', '0.11%'],
 ['91', '0.04%', '0.09%'],
 ['92', '0.03%', '0.08%'],
 ['93', '0.02%', '0.06%'],
 ['94', '0.02%', '0.04%'],
 ['95', '0.01%', '0.04%'],
 ['96', '0.01%', '0.03%'],
 ['97', '0.01%', '0.02%'],
 ['98', '0%', '0.01%'],
 ['99', '0%', '0.01%'],
 ['100+', '0%', '0.02%']]
'''

... to show the five ages with the most male + female percentages.

Edited by vegaseat

0

Lets say you have the following data set (weight is in pounds) ...

# data is a list of (fname, lname, age, weight) tuples
data_list = [
('Heidi', 'Kalumpa', 36, 127),
('Frank', 'Maruco', 27, 234),
('Larry', 'Pestraus', 19, 315),
('Serge', 'Romanowski', 59, 147),
('Carolus', 'Arm', 94, 102),
('Michel', 'Sargnagel', 21, 175),
('Hans', 'Solo', 45, 199),
('Mother', 'Theresa', 81, 113)
]

... do the following:
1) show the row that 'Hans' is in
2) show the row with the oldest person
3) show the average weight of all the people in the data
4) change all the weights in the data list from pounds to kilograms

Edited by vegaseat: new exp

0
# data is a list of (fname, lname, age, weight) tuples
data_list = [
('Heidi', 'Kalumpa', 36, 127),
('Frank', 'Maruco', 27, 234),
('Larry', 'Pestraus', 19, 315),
('Serge', 'Romanowski', 59, 147),
('Carolus', 'Arm', 94, 102),
('Michel', 'Sargnagel', 21, 175),
('Hans', 'Solo', 45, 199),
('Mother', 'Theresa', 81, 113)
]

You just found out the Heidi Kalumpa in the above data_list has gained 30 pounds. How would you change it?

0

Use Python to prove that the sentence
"the quick brown fox jumps over a lazy dog"
uses every letter of the alphabet.

0

In the mid-18th century, Leonhard Euler wrote what he admitted to be a paradoxical equation, consisting of a (infinite) series whose terms are the successive integers, given alternating signs:

1-2+3-4+5-6+7-8+ ... = 1/4

Write a Python program to prove it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.