[Tutor] First program

2010-03-12 Thread yd
Hi,
I am new to programming, altough i have read a few books about OOP and
O'Reily's Learning Python.
I would like some critique on my first program, is it normal for it to be
this long to do something simple?
I know i could have turned some of these things into classes and functions
but i don't know how to do that yet.
Some critique of the algorithm and writing style or anything in general
would help and any pointers would be appreciated.
Thanks.

#title Area calculator
#author Yudhishthir Singh


#welcome screen
msg = 'Welcome to the area calculator program '
print(msg)
print('-'*len(msg))
loop = 'y'
print()
while loop == 'y':
  #Choices menu
  print('Please select a shape\n')
  print('1. Rectangle')
  print('2. Square')
  print('3. Parallelogram ')
  print('4. Trapezoid ')
  print('5. Circle ')
  print('6. Ellipse')
  print('7. Traingle\n')
  print('-'*len(msg))
  choice = input('\nPlease enter your choice: ')
  if choice.isdigit() ==True:
choice = int(choice)
  if choice ==1:
#Rect
height = input('please enter the height: ')
width = input('please enter the width: ')
height = int(height)
width = int(width)
areaRectangle = height*width
print('\nThe area of a rectangle with {0} height and {1} width is
'.format(height,width),areaRectangle,'\n')
  elif choice ==2:
#Square
side = input('enter the height or width: ')
side = int(side)
areaSquare = side**2
print('\nThe area of a square with a height or width of {0} is
'.format(side), areaSquare,'\n')
  elif choice ==3:
#Parallelogram
height = input('enter the height: ')
base = input('enter the width aka base: ')
height = int(height)
base = int(base)
areaParallelogram = height*base
print('\nThe area of a parrallelogram with height {0} and width {1} is
'.format(height,base), areaParallelogram,'\n')
  elif choice ==4:
#Trapezoid
height = input('enter the height: ')
base1 = input('enter the width of shorter side: ')
base2 = input('enter the width of longer side: ')
height = int(height)
base1 = int(base1)
base2 = int(base2)
areaTrapezoid = (height/2)*(base1+base2)
print('\nThe area of a trapezoid with height {0} ,base {1} and {2} is
'.format(height,base1,base2), areaTrapezoid, '\n')
  elif choice ==5:
#Circle
radius = input('radius: ')
radius = int(radius)
areaCircle = 3.14*(radius**2)
print('\nThe area of a circle with radius {0} is '.format(radius),
areaCircle, '\n')
  elif choice ==6:
#Ellipse
radius1 = input('enter length of radius 1: ')
radius2 = input('enter length of radius 2: ')
radius1 = int(radius1)
radius2 = int(radius2)
areaEllipse = 3.14*radius1*radius2
print('\nThe area of an ellipse with radii of length {0} and {1} is
'.format(radius1,radius2), areaEllipse, '\n')
  elif choice ==7:
#Triangle
base = input('enter base: ')
height = input('enter height: ')
base = int(base)
height = int(height)
areaTriangle = (1/2 *base)*height
print('\nThe area of a triange with height {0} and base {1} is
'.format(height,base), areaTriangle, '\n')
  else:
raise Exception('{0}, is not a valid choice'.format(choice))
  loop = input('Do you want to calculate the area of another shape? Y/N: ')
  loop = loop.lower()

#todo:
  #Improve error checking in individual modules
  #Turn the calculators into classes or functions
  #Make it so all the print statments for the results use the same function
or whatever
  #Make a function that turns input into integer and stores it in the
original input values place
  #etc.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] First program

2010-03-13 Thread yd
Thanks everyone, I am trying to figure out functions and classes right now,
i will probably rewrite the program once i get that down and probably use
try: and except: for error catching.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Prime numbers

2010-03-27 Thread yd
Having a problem finding the first 1000 prime numbers, here is my code:-

print(2)
n =3
counter =1
while counter <=1000:
  for x in range(3,int((n**0.5)),2):
if n%x != 0:
  print(n)
  n+=1
  counter+=1
else:
  n+=1

The problem is, it prints 2 and then does nothing, yet if i try and close,
it says program is still running do you want to kill it, is there a way to
do this with lists, i know python has a prime function but i am not going to
use it because i want to solve this problem without 'cheating'.Thanks.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Odds and even exercise

2010-03-27 Thread yd
Hi, just started python at Uni and think i am in for a rough ride with zero
> prior experience in programming.
> Anyway my problem that i can't fix my self after googling.
>
> The exercise is to generate a list of odd numbers between 1-100 and the
> same
> for even numbers.
>
> So far this is what i have
> way to display numbers 1 - 100
> numbers = range(100)



> this gives me numbers= range(0,100) as an output in python 3.0
>
   I find it easy to do all this stuff with list comprehensions, but i am a
beginner so this might not be the most efficient way to do it
  numbers=[]
  for x in range(1,101):
numbers.append(x)

> #A way to display all odd numbers
> odd = numbers[::2]
>
  instead i do this:
  odd=[]
  for y in range(1,101,2):
odd.append(y)


> #A way to display all even numbers
>

  even=[]
  for z in range(2,101,2):
even.append(z)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Prime numbers

2010-03-28 Thread yd
>
>
> > What 'problem' are you trying to solve?
> In general, anytime you can use a premade solution, you are at an
> advantage,
> not cheating.
> That's one of the marks of a truly good programmer - being able to reuse as
> much code as possible.
> Unless it's a homework problem and he said "don't use the prime function"
> because in this case, your goal is to learn how to write a prime function,
> not to calculate primes.
> I.E. it's the doing, not the task.  And if that's the case, please let us
> know that it's homework.  We will still help you, we just follow certain
> guidelines
> when providing homework assistance so as not to "give it away" and still
> allow you to reason / come up with the solution on your own, as your
> teacher
> probably intended.
>


>
> -Luke
>
It's not homework i just want to be able to convert my algorithm into good
code, and the only way to do that is by actually writing it. I'm just
writing it to learn how it's done.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor