[Tutor] Python
Hello, Can you build these 4 programms for me? 1: Input: 5 7 (rows and colloms) Output: *** *** *** *** *** 2: Move all of the letters 23 places to the right. All capital letters. A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (boodschap) X Y Z A B C D E F G H I J K L M N O P Q R S T U V W (code) N=23 Example Input: COMPUTER Output: ZLJMRQBO 3: Bitcoin: Make as much as possible money. -You start with no bitcoin. -You stop with no bitcoinJ -Never have more then 1 bitcoin at a time. -You can sell and buy a bitcoin whenever you want to. First input = N, the amount of days. Then the input is the course of the bitcoin of every day. Output is 1 line with maximum of profit. It can be 0. Example: Input: 10 5 11 4 2 8 10 7 4 3 6 Output: 17 Kind Regards Jeroen van de Ven (beginner) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Counting iterations of a function
Hello, I'm trying to code a magic 8-ball program and it needs to include a counter for the number of questions asked. I'm having a hard time implementing a count, and need a little help. I had tried to implement it in the main() function, but that (naturally) led it to only counting '1' for each question, for each question asked. Is there a way to count how many times a give line of code is executed? For instance, every time the oracle() function is run, a counter goes up. Thanks for any help. Program: #Michael Dowell #11-26-27 #Capstone: Magic 8-ball #import modules import random import turtle #Give welcome message def print1(): print("Hello!") print("Thank you for using this Magic 8-Ball Simulator.") print("We understand that you have a choice in magic 8-ball experiences,\n and that you have no choice in viewing mine.") print("Thank you") #Define main function def main(): #Set local variables count=0 #Tried to intergrate count into main() function query='' #Get question query=str(input("Please type in your question(press 'Enter' to exit):")) while query!='': oracle() main() break #Determine fate (the error "Variable 'fate' is not defined" was quite existential) def oracle(): fateList=["It is certain","It is decidedly so","Without a doubt","Yes – definitely","You may rely on it","As I see it, yes","Most likely","Outlook is good","Yes","Signs point to yes","Reply hazy, try again","Ask again later","Better not tell you now","Cannot predict now","Concentrate and ask again","Don’t count on it","My reply is no","My sources say no","Outlook not so good","Very doubtful"] fate=fateList[random.randint(0,len(fateList))-1] #Draw 8 ball/Refresh fate turtle.pensize(10) turtle.speed(7) turtle.hideturtle() turtle.goto(0,-200) turtle.fillcolor('black') turtle.begin_fill() turtle.circle(250) turtle.end_fill() turtle.goto(0,-50) turtle.fillcolor('blue') turtle.begin_fill() turtle.circle(150) turtle.end_fill() turtle.fillcolor('white') turtle.begin_fill() turtle.pendown() turtle.goto(130,150) turtle.goto(-130,150) turtle.goto(0,-50) turtle.end_fill() turtle.penup() turtle.goto(-75,125) x=turtle.pos() #Helvetica is the one true font turtle.write(fate,font=("Helvetica",12,"normal")) #call functions print1() main() #exit message for count in range(20): print("Thanks for playing!") ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python
On Thu, Nov 30, 2017 at 2:20 AM, Jeroen van de Ven wrote: > Hello, > Can you build these 4 programms for me? No, we will not do your homework for you. However, you can show us the code you've written, describe the problem you're having with it (including the full text of all error messages), and we can help you figure out where you've gone wrong. Regards, -- Zach ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python
On 30/11/17 08:20, Jeroen van de Ven wrote: Hello, Can you build these 4 programms for me? This list doesn't work like that. We will not write code for you. You make an attempt at the problems, we will then assist. 1: Input: 5 7 (rows and colloms) Output: *** *** *** *** *** 2: Move all of the letters 23 places to the right. All capital letters. A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (boodschap) X Y Z A B C D E F G H I J K L M N O P Q R S T U V W (code) N=23 Example Input: COMPUTER Output: ZLJMRQBO 3: Bitcoin: Make as much as possible money. -You start with no bitcoin. -You stop with no bitcoinJ -Never have more then 1 bitcoin at a time. -You can sell and buy a bitcoin whenever you want to. First input = N, the amount of days. Then the input is the course of the bitcoin of every day. Output is 1 line with maximum of profit. It can be 0. Example: Input: 10 5 11 4 2 8 10 7 4 3 6 Output: 17 Kind Regards Jeroen van de Ven (beginner) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python
On 30/11/17 08:20, Jeroen van de Ven wrote: > Hello, > Can you build these 4 programms for me? Nice try. The answer is yes we can, but no we won't. You wouldn't learn anything if we did and this list is here to teach. That means you need to write the code and we will offer help and suggestions. > 1: > Input: 5 7 (rows and colloms) > Output: > *** > *** > *** > *** > *** Can you read a line of input and extract 2 numbers? Can you write a loop that repeats as often as one of the numbers you read? Can you write a print statement that outputs as many '*'s as the other number? If so, put it together and jobs done. If not do what you can and show us the result. > 2: > Move all of the letters 23 places to the right. All capital letters. > A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (boodschap) > X Y Z A B C D E F G H I J K L M N O P Q R S T U V W (code) > > N=23 > > Example > Input: COMPUTER > Output: ZLJMRQBO Read a number. Shift the letters - this is probably easiest with slicing and a wee bit of slightly tricky arithmetic Read the input word Now use the two lists of letters to map each letter (hint Python has some string functions that do precisely this.) > 3: Bitcoin: > Make as much as possible money. > -You start with no bitcoin. > -You stop with no bitcoinJ > -Never have more then 1 bitcoin at a time. > -You can sell and buy a bitcoin whenever you want to. > First input = N, the amount of days. Then the input is the course of the > bitcoin of every day. > Output is 1 line with maximum of profit. It can be 0. > Example: > Input: 10 5 11 4 2 8 10 7 4 3 6 > Output: 17 Sorry, I know zilch about bitcoin, so don't even understand the question. > -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Counting iterations of a function
On 2017-11-30, Michael Dowell wrote: > Hello, I'm trying to code a magic 8-ball program and it needs > to include a counter for the number of questions asked. I'm > having a hard time implementing a count, and need a little > help. I had tried to implement it in the main() function, but > that (naturally) led it to only counting '1' for each question, > for each question asked. Is there a way to count how many times > a give line of code is executed? For instance, every time the > oracle() function is run, a counter goes up. Thanks for any > help. > > Program: > #import modules > import random > import turtle > > > #Give welcome message > def print1(): > > print("Hello!") > print("Thank you for using this Magic 8-Ball Simulator.") > print("We understand that you have a choice in magic 8-ball > experiences,\n and that you have no choice in viewing mine.") > print("Thank you") > > #Define main function > > def main(): > #Set local variables > count=0 #Tried to intergrate count into main() function > query='' > > #Get question > query=str(input("Please type in your question(press 'Enter' to exit):")) > > while query!='': > oracle() > main() > break You are combining a while loop and a recursive call to main in a redundant way. A while loop without the complication of the function calling itself makes things easier. By the way, input returns a string, so you don't need to call "str" on it. query = input("Please type in your question(press 'Enter' to exit):") while query!='': oracle() count += 1 query = input("Please type in your question(press 'Enter' to exit):")) When the while loop is finally complete, count will be the number of times oracle has run. In current Python, pushing the query into a separate function is a good way to eliminate the verbatim repetition of code. def get_query(): return input("Please type in your question(press 'Enter' to exit):") In main: query = get_query() while query!='': oracle() count += 1 query = get_query() You can generalize function-call counting by writing a function decorator that counts the number of times its function has run, and then decorate oracle with it, but that pie is higher in the sky than needed here. -- Neil Cerutti ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Counting iterations of a function
On Nov 30, 2017 12:56 PM, "Michael Dowell" wrote: > > Hello, I'm trying to code a magic 8-ball program and it needs to include a > counter for the number of questions asked. I'm having a hard time > implementing a count, and need a little help. I had tried to implement it > in the main() function, but that (naturally) led it to only counting '1' > for each question, for each question asked. Is there a way to count how > many times a give line of code is executed? For instance, every time the > oracle() function is run, a counter goes up. Thanks for any help. > > Program: > > > #Michael Dowell > #11-26-27 > #Capstone: Magic 8-ball > > #import modules > import random > import turtle > > > #Give welcome message > def print1(): > > print("Hello!") > print("Thank you for using this Magic 8-Ball Simulator.") > print("We understand that you have a choice in magic 8-ball > experiences,\n and that you have no choice in viewing mine.") > print("Thank you") > > #Define main function > > def main(): > #Set local variables > count=0 #Tried to intergrate count into main() function > query='' > > #Get question > query=str(input("Please type in your question(press 'Enter' to exit):")) > > while query!='': > oracle() > main() Your problem is here. Instead of calling main recursively you should move the while to the top of the main function preceded by assigning something to query. Also get rid of the break statement > break > > #Determine fate (the error "Variable 'fate' is not defined" was quite > existential) > def oracle(): > > fateList=["It is certain","It is decidedly so","Without a doubt","Yes – > definitely","You may rely on it","As I see it, yes","Most likely","Outlook > is good","Yes","Signs point to yes","Reply hazy, try again","Ask again > later","Better not tell you now","Cannot predict now","Concentrate and ask > again","Don’t count on it","My reply is no","My sources say no","Outlook > not so good","Very doubtful"] > fate=fateList[random.randint(0,len(fateList))-1] > > #Draw 8 ball/Refresh fate > turtle.pensize(10) > turtle.speed(7) > turtle.hideturtle() > turtle.goto(0,-200) > turtle.fillcolor('black') > turtle.begin_fill() > turtle.circle(250) > turtle.end_fill() > turtle.goto(0,-50) > turtle.fillcolor('blue') > turtle.begin_fill() > turtle.circle(150) > turtle.end_fill() > turtle.fillcolor('white') > turtle.begin_fill() > turtle.pendown() > turtle.goto(130,150) > turtle.goto(-130,150) > turtle.goto(0,-50) > turtle.end_fill() > turtle.penup() > turtle.goto(-75,125) > x=turtle.pos() > > #Helvetica is the one true font > turtle.write(fate,font=("Helvetica",12,"normal")) > > #call functions > print1() > main() > > #exit message > > for count in range(20): > print("Thanks for playing!") > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] problem with a sub-class
I am almost a beginner. I have an error message which I cannot understand. My problem is with constructing a sub-class. I use; MAC OS V10.13.1 Anaconda Python 3.5 My base Class works properly and all 136 tests of the Base Class are correct. My sub-class is constructed as follows: import sys import random import numpy as np import pylab import copy import Population_InitV8 as POCI import Population_ProductivityV24 as POCWP line 27 : class SimulateCycleZero(POCWP): line 28 : def __init__(self, dirname_p): The program follows on from this. The error message is as follows: File "/Users/sydney/AnacondaProjects/Capital/Capital_with_productivity/Current_Versions/Simulate_Cycle_Zero_V3.py", line 27, in class SimulateCycleZero(POCWP): TypeError: module.__init__() takes at most 2 arguments (3 given) My problem is that I do not understand to which file the word 'module' in the error message applies. It seems to me that somewhere I am providing 3 arguments when only a maximum of two are required. When I do the following, I get the same error message. line 27 : class SimulateCycleZero(POCWP): line 28 : def __init__(self): I would appreciate some guidance , please. Sydney _ Professor Sydney Shall Department of Haematology/Oncology Phone: +(0)2078489200 E-Mail: sydney.shall [Correspondents outside the College should add @kcl.ac.uk] ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] problem with sub-classing
I am almost a beginner. I have an error message which I cannot understand. My problem is with constructing a sub-class. I use; MAC OS V10.13.1 Anaconda Python 3.5 My base Class works properly and all 136 tests of the Base Class are correct. My sub-class is constructed as follows: import sys import random import numpy as np import pylab import copy import Population_InitV8 as POCI import Population_ProductivityV24 as POCWP My program starts thus: line 27 : class SimulateCycleZero(POCWP): line 28 : def __init__(self, dirname_p): The program follows on from this. The error message is as follows: File "/Users/sydney/AnacondaProjects/Capital/Capital_with_productivity/Current_Versions/Simulate_Cycle_Zero_V3.py", line 27, in class SimulateCycleZero(POCWP): TypeError: module.__init__() takes at most 2 arguments (3 given) My problem is that I do not understand to which file the word 'module' in the error message applies. It seems to me that somewhere I am providing 3 arguments when only a maximum of two are required. When I do the following, I get the same error message. line 27 : class SimulateCycleZero(POCWP): line 28 : def __init__(self): I would appreciate some guidance , please. Sydney -- Sydney282003 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problem with a sub-class
On 30/11/17 15:37, Shall, Sydney wrote: > My problem is with constructing a sub-class. > > My sub-class is constructed as follows: > > import Population_ProductivityV24 as POCWP Note that POCWP is an alias for the *module* Population_ProductivityV24. It is not a class. > line 27 : class SimulateCycleZero(POCWP): Here you define a class that subclasses your imported module. Frankly I'm surprised that you don't get an error there but hey... > line 28 : def __init__(self, dirname_p): But this is now an init for a subclass of module. > The error message is as follows: > >File > "/Users/sydney/AnacondaProjects/Capital/Capital_with_productivity/Current_Versions/Simulate_Cycle_Zero_V3.py", > > line 27, in > class SimulateCycleZero(POCWP): > > TypeError: module.__init__() takes at most 2 arguments (3 given) So I'm guessing the use of a module to subclass has confused things and I confess I'm not clear on exactly what is going on and why you get this message. But I'm pretty sure you don;t want to subclass your imported module and thats the mistake. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor