[Tutor] python strings?
below is my program. what i would like to do is to get the last line to print the number of years that the user enters and also the principal # A program to compute the value of an investment # years into the future def main(): print "This program calculates the future value of an investment over years" principal = input("Enter the initial principal: ") apr = input("Enter the annual interest rate: ") years = input("Enter the number of years: ") for i in range(10): principal = principal * (1 + apr) print "The value in years is", principal main() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] how to insert a string?
Hello I would like to insert a string into the following program to simplify the program. Any suggestions on how I might do so? # dateconvert2.py #Converts day month and year numbers into two date formats import string def main(): # get the day month and year day, month, year = input("Please enter the day, month and year numbers: ") date1 = str(month)+"/"+str(day)+"/"+str(year) months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] monthStr = months[month-1] date2 = monthStr+" " + str(day) + ", " + str(year) print "The date is", date1, "or", date2 main() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] how to insert a string?
Hello I would like to insert a string into the following program to simplify the program. Any suggestions on how I might do so? # dateconvert2.py #Converts day month and year numbers into two date formats import string def main(): # get the day month and year day, month, year = input("Please enter the day, month and year numbers: ") date1 = str(month)+"/"+str(day)+"/"+str(year) months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] monthStr = months[month-1] date2 = monthStr+" " + str(day) + ", " + str(year) print "The date is", date1, "or", date2 main() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] printing an acronym
Hello How could I get the following to print out an acronym for each phrase entered such as if I entered random access memory it word print out RAM? import string def main(): phrase = (raw_input("Please enter a phrase:")) acr1 = string.split(phrase) acr2 = string.capwords(phrase) acr3 = acr2[0] print"",acr3 main() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] drawing a face
Hello I am trying to draw a basic face with eyes, nose, and a mouth. I keep receiving the following error message: >>> Traceback (most recent call last): File "C:\Documents and Settings\Michele\Desktop\face", line 26, in ? main() File "C:\Documents and Settings\Michele\Desktop\face", line 10, in main leftEye = Cirlce(Point(285,210), 50) NameError: global name 'Cirlce' is not defined also I know i want an oval for the month but I only need half of one how would i just get half of the oval? from graphics import* def main(): win = GraphWin() shape = Circle(Point(300,200), 40) shape.setOutline("black") shape.setFill("orange") shape.draw(win) leftEye = Cirlce(Point(285,210), 50) leftEye.setFill("yellow") leftEye.setOutline("orange") rightEye = leftEye.clone() leftEye.draw(win) rightEye.draw(win) sidenose = Line(245, 265) botnose = Line(245,0) sidenose.draw(win) botnose.draw(win) win.getMouse() win.close() main() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] lonely snowman
Hello I have a snowman who would like a nose. I have tried the following to make a triangle and all I get is a little green line. would anyone have any suggestions as to why? from graphics import * def main(): win = GraphWin("Triangle") triangle = Polygon(Point(4,8), Point(2,10), Point(6,3)) triangle.setFill("purple") triangle.setOutline("green") triangle.draw(win) win.getMouse() win.close() main() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] help with elif statements
hello below is my code and everytime I input a value of 16 or more it keeps returning sophomore. could anyone help me figure out what to change so that it won't return sophmore for things greater than or equal to 16? def getcredits(num): if num < 7: return 'Freshman' elif num >= 7: return 'Sophomore' elif num <16: return 'Sophomore' elif num >= 16: return 'Junior' elif num < 26: return 'Junior' else: return 'Senior' def main(): g = input('Enter number of credits:') print 'Your standing is %s' % (getcredits(int(g))) main() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] if-elif-else statements
Hello I am having trouble getting my program to calculate the correct total if the speed limit is over 90mph. For example when i enter 100 for the clocked speed and 50 for the actual speed it returns 300 when I should receive 500. It also seems to be skipping over the else statement of if the person is not speeding they would be doing the legal speed limit. Any help would be greatly appreciated. def main(): actual = input("Please enter the legal speed limit: ") clocked = input("Please enter the clocked speed limit: ") speedlimit = clocked - actual fine = 50 + 5*speedlimit total = 200 + fine amount = fine if speedlimit < 90: print "The total amount due is", amount elif speedlimit >= 90: print "The total amount due is", total else: print "The speed was legal." main() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] if-elif-else statements
amount is supposed to equal fine because there are a couple of different fines. there is a fine for going over 90mph that includes a penalty and then there is a fine for just going over the speed limit as long as it is under 90. > Is amount suppose to equal total instead of fine? > > > > On Thursday 13 October 2005 04:58 pm, [EMAIL PROTECTED] wrote: >> def main(): >> actual = input("Please enter the legal speed limit: ") >> clocked = input("Please enter the clocked speed limit: ") >> speedlimit = clocked - actual >> >> fine = 50 + 5*speedlimit >> total = 200 + fine >> amount = fine >> >> if speedlimit < 90: >> print "The total amount due is", amount >> >> elif speedlimit >= 90: >> print "The total amount due is", total >> >> else: >> print "The speed was legal." >> >> >> main() > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] if-else statements
Hello I'm having some trouble with my if, else statements. For some reason, the months that have 31 days work fine, but the months that have 28/30 do not work. Am I doing something wrong? it is supposed to take a date as an input like 9/31/1991 and then say that the date is not valid because september only has 30 days. import string def main(): # get the day month and year month, day, year = input("Please enter the mm, dd, : ") date1 = "%d/%d/%d" % (month,day,year) months = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if day <= months[1]: d = "valid" else: n = "not valid" if day <= months[2]: d = "valid" else: d = "not valid" if day <= months[3]: d = "valid" else: d = "not valid" if day <= months[4]: d = "valid" else: n = "not valid" if day <= months[5]: d = "valid" else: d = "not valid" if day <= months[6]: d = "valid" else: d = "not valid" if day <= months[7]: d = "valid" else: d = "not valid" if day <= months[8]: d = "valid" else: d = "not valid" if day <= months[9]: d = "valid" else: d = "not valid" if day <= months[10]: d = "valid" else: d = "not valid" if day <= months[11]: d = "valid" else: d = "not valid" if day <= months[12]: d = "valid" else: d = "not valid" print "The date you entered", date1, "is", d +"." main() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] syracuse sequence (collatz or hailstone)
Hello I am trying to create a program that will calculate the syracuse sequence which is also known as collatz or hailstone. the number that is input by the user may be either even or odd. the number goes through a series of functions which are x/2 if the number is even and 3x+1 if the number is odd. it keeps doing so until the number reaches 1. An example would be if the user inputed 5 they should recieve: 5, 16, 8, 4, 2, 1 as the sequence for the value that they started with. My code currently just prints a 1 and none of the numbers that would have preceded it. any ideas on how I could get the program to not do this would be greatly appreciated. def main(): try: x = input("Please enter a starting value: ") while x != 1: if x%2 == 0: x = x/2 else: x = x*3+1 except ValueError, excObj: msg = str(excobj) if msg == "math domain error": print "No negatives or decimals." else: print "Something went wrong." print "The Syracuse sequence of your starting value is:", x main() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] syracuse sequence (collatz or hailstone)
hello, Could I gather all of the values from print x into a string or a range? Since, I am not familiar with lists yet. def main(): x = input("Please enter a positive starting value: ") while x != 1: if x%2 == 0: x = x/2 else: x = x*3+1 print x print "The Syracuse sequence of your starting value is:", x main() - Original Message - From: "Frank Bloeink" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Friday, October 28, 2005 5:06 AM Subject: Re: [Tutor] syracuse sequence (collatz or hailstone) > Hey, > > your code seems almost alright to me, except that in your case it's only > printing the last number of your sequence, which obviously is not what > you want. Quick fix would be to insert a line "print x" just below else > statement: > ---snip-- > else: >x=x*3+1 > print x > ---snip > This should make clear where the error is: You are just calculating, but > not printing the sequence! > If you want to leave the output to the end of the program you could as > well gather all the calculated values in a list or similar structure and > then print the contents of the list.. > > hth Frank > > On Fri, 2005-10-28 at 01:22 -0400, [EMAIL PROTECTED] wrote: > > Hello > > > > I am trying to create a program that will calculate the syracuse sequence > > which is also known as collatz or hailstone. the number that is input by > > the user may be either even or odd. the number goes through a series of > > functions which are x/2 if the number is even and 3x+1 if the number is > > odd. it keeps doing so until the number reaches 1. An example would be if > > the user inputed 5 they should recieve: 5, 16, 8, 4, 2, 1 as the sequence > > for the value that they started with. My code currently just prints a 1 > > and none of the numbers that would have preceded it. any ideas on how I > > could get the program to not do this would be greatly appreciated. > > > > > > def main(): > > try: > > x = input("Please enter a starting value: ") > > while x != 1: > > > > if x%2 == 0: > > x = x/2 > > else: > > x = x*3+1 > > > > except ValueError, excObj: > > msg = str(excobj) > > if msg == "math domain error": > > print "No negatives or decimals." > > else: > > print "Something went wrong." > > > > > > > > print "The Syracuse sequence of your starting value is:", x > > > > main() > > > > > > ___ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] syracuse sequence (collatz or hailstone)
would this be a possible use of a list and appending even though I recieve an error from it: def main(): x = [1] x[0] = input('enter an int to start your syracuse sequence\n') while not isinstance(x[0], int): x[0] = input('no, please enter an int to start your syracuse sequence\n') while x[-1] != 1: if ((x[-1] % 2) == 0): x.append(x[-1] / 2) else: x.append((3 * x) + 1) print len(x), x print "The Syracuse sequence of your starting value is:", x main() line 10, in main x.append((3 * x) + 1) TypeError: can only concatenate list (not "int") to list >>> > [EMAIL PROTECTED] wrote: >> hello, >> >> Could I gather all of the values from print x into a string or a range? >> Since, I am not familiar with lists yet. > > Here is a simple example of gathering values into a list and making a > string: > >>> r=[] # Start with an empty list > >>> for x in range(3): # x will be 0, 1, 2 in sequence > ... r.append(str(x*x)) # Put x*x (as a string) onto r > ... > >>> r > ['0', '1', '4'] > >>> ', '.join(r) # make a single string by joining the elements of r with > ', ' > '0, 1, 4' > > Kent > >> >> >> def main(): >> x = input("Please enter a positive starting value: ") >> while x != 1: >> if x%2 == 0: >> x = x/2 >> else: >> x = x*3+1 >> print x >> print "The Syracuse sequence of your starting value is:", x >> >> main() >> >> >> >> >> - Original Message - >> From: "Frank Bloeink" <[EMAIL PROTECTED]> >> To: <[EMAIL PROTECTED]> >> Sent: Friday, October 28, 2005 5:06 AM >> Subject: Re: [Tutor] syracuse sequence (collatz or hailstone) >> >> >> >>>Hey, >>> >>>your code seems almost alright to me, except that in your case it's only >>>printing the last number of your sequence, which obviously is not what >>>you want. Quick fix would be to insert a line "print x" just below else >>>statement: >>>---snip-- >>> else: >>> x=x*3+1 >>> print x >>>---snip >>>This should make clear where the error is: You are just calculating, but >>>not printing the sequence! >>>If you want to leave the output to the end of the program you could as >>>well gather all the calculated values in a list or similar structure and >>>then print the contents of the list.. >>> >>>hth Frank >>> >>>On Fri, 2005-10-28 at 01:22 -0400, [EMAIL PROTECTED] wrote: >>> Hello I am trying to create a program that will calculate the syracuse sequence which is also known as collatz or hailstone. the number that is input by the user may be either even or odd. the number goes through a series of functions which are x/2 if the number is even and 3x+1 if the number is odd. it keeps doing so until the number reaches 1. An example would be if the user inputed 5 they should recieve: 5, 16, 8, 4, 2, 1 as the sequence for the value that they started with. My code currently just prints a 1 and none of the numbers that would have preceded it. any ideas on how I could get the program to not do this would be greatly appreciated. def main(): try: x = input("Please enter a starting value: ") while x != 1: if x%2 == 0: x = x/2 else: x = x*3+1 except ValueError, excObj: msg = str(excobj) if msg == "math domain error": print "No negatives or decimals." else: print "Something went wrong." print "The Syracuse sequence of your starting value is:", x main() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor >>> >>> >>> >> >> ___ >> Tutor maillist - Tutor@python.org >> http://mail.python.org/mailman/listinfo/tutor >> >> > > -- > http://www.kentsjohnson.com > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] help with prime number program
Hello I am trying to write a program that will figure out if a number is prime or not. Currently this is the code that I have: import math def main(): number=input("Please enter a positive whole number greater than 2: ") for value in range(2, number-math.sqrt): if number % value == 0: print number, "is not prime" exit print number, "is prime" main() my problem is that the output prints both statements whether the number is prime or not. How could I get it to only print the one statement saying that 49 is not a prime number? enter a number greater than 2: 49 49 is not prime 49 is prime ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help with prime number program
> > >> I am trying to write a program that will figure out if a number is prime >> or not. Currently this is the code that I have: > > [code cut] > > Hi Andrade1, > > As in your other previous homework programs, you seem to have some trouble > with control flow. Have you considered talking with your professor or > teaching assistant about this? > > >> import math >> >> def main(): >> number=input("Please enter a positive whole number greater than 2: >> ") >> for value in range(2, number-math.sqrt): >> if number % value == 0: >> print number, "is not prime" >> exit >> print number, "is prime" >> >> main() >> >> my problem is that the output prints both statements whether the number >> is prime or not. > > > The block of code: > > ## > for value in range(2, number-math.sqrt): > if number % value == 0: > print number, "is not prime" > exit > ## > > has two issues. > > 1. What is 'exit'? instead of exit I can use break so that the loop will stop executing > 2. What is 'number - math.sqrt'? for math.sqrt I could use math.sqrt(n) so that it would take the sqrt of n which is entered in by the user However for the output I am still receiving enter a number greater than 2: 49 49 is prime 49 is prime 49 is prime 49 is prime 49 is prime 49 is not prime > Instead of 'exit', you may be looking for the 'return' statement instead. > > > Good luck to you. > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor