Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,634 members, 7,809,370 topics. Date: Friday, 26 April 2024 at 08:25 AM

Pls Help With Python Problem - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Pls Help With Python Problem (6857 Views)

I Need HELP With This Python Problem / Build A Bare Bones Reddit Clone With Python And Flask (hands On Tutorial Thread) / Help With This Python Problem (2) (3) (4)

(1) (2) (3) (4) (Reply) (Go Down)

Pls Help With Python Problem by bright007(f): 5:28pm On Jul 26, 2013
i started learning python last month and it has not been really easy as many python programmers usually say.i use a book titled "python for the absolute beginner" and it has really helped to encourage me in learning python.
But there is one exercise which i have not been able to solve and that's why i want you guyz to help out.it goes thus:
write a program for numbers in the range 91 using the followin defined functions
(1)thrust#define thrust such that d thrust of a number in range is (25 minusthat number) if the number is greater than 25 or the thrust is(25 plus that number) if the number is less than 25.if the raw_input is 25,then thrust is (25+25)#
(2)also define turn such that the turn of any number in the range is thus:
numbers ending in zero, the turn is the number without the zero.e.g turn of 50 is 5 and turn of 40 is 4
any number ending in 9,the program returns same number e.g turn of 49 is 49 while turn of 19 is 19.

1 Like 1 Share

Re: Pls Help With Python Problem by bright007(f): 5:37pm On Jul 26, 2013
.
Re: Pls Help With Python Problem by Nobody: 8:53pm On Jul 26, 2013
The problem isnt clear.
Will the numbers be inputed one by one from the standard input like using x= raw_input()?
Or will they be already defined like x=range(91)? Either way, I will try to answer to d best of my understanding.
Re: Pls Help With Python Problem by bright007(f): 9:39pm On Jul 26, 2013
PhenomenonVFX: The problem isnt clear.
Will the numbers be inputed one by one from the standard input like using x= raw_input()?
Or will they be already defined like x=range(91)? Either way, I will try to answer to d best of my understanding.
....it will be in raw_input.for example after running the module,the user will type the function(thrust or turn) followed by any number within range (91) for the program to execute.thanks for the reply
Re: Pls Help With Python Problem by bright007(f): 9:39pm On Jul 26, 2013
PhenomenonVFX: The problem isnt clear.
Will the numbers be inputed one by one from the standard input like using x= raw_input()?
Or will they be already defined like x=range(91)? Either way, I will try to answer to d best of my understanding.
Re: Pls Help With Python Problem by Nobody: 9:49pm On Jul 26, 2013
"""Since u are a beginner, let me give u a lenghty code. And I am assuming predefined numbers. The trailing dots '......' are just there for indentation because I am writing this on a mobile phone and I doubt Nairaland supports tabs. Leave them out of ur code use tabs instead.
""""

y= range(91) #predefined
thrust_list= [] #holds the thrust
turn_list= [] #holds the turn

def thrust():
......for x in y:
..........if x <= 25:
...............thrust_list.append(x+25)
..........else thrust_list.append(25-x)

def turn():
......for x in y:
..........if x % 10 == 0 and x >= 10:
.................turn_list.append(x/10)
..........elif x % 10 == 9 and x >= 10:
..................turn_list.append(x)
..........elif x < 10:
.................turn_list.append(x*10)
..........else
..............turn_list.append(str(x).reverse())

if __name__ == '__main__':
.......thrust()
.......turn()
Re: Pls Help With Python Problem by Nobody: 10:11pm On Jul 26, 2013
bright007: ....it will be in raw_input.for example after running the module,the user will type the function(thrust or turn) followed by any number within range (91) for the program to execute.thanks for the reply

Ok. The foregoing code is still useful. All u have to do is remove the for-loop in the function declarations, declare the functions with one parameter such as saying "def thrust(x):" and use a raw_input instead. Like this:

if __name__ == '__main__':
.......print ("Please insert a number" )
.......y= raw_input()
.......x= int(y) #just to be safe
.......if x > 90:
.............print("Number out of range" )
........else
.............print("This is the thrust:" )
.............thrust(x)
.............print("This is the turn:" )
.............turn(x)
"""
depending on ur version of python, the raw-input function would return a string or an integer. If it is an integer then use it like that. If not, convert it to an integer using the int() function.
"""
Re: Pls Help With Python Problem by bright007(f): 10:19pm On Jul 26, 2013
thanks boss for your quickie reply.i will try it now.
Re: Pls Help With Python Problem by Nobody: 10:26pm On Jul 26, 2013
bright007: thanks boss for your quickie reply.i will try it now.

You are welcome. You can also try getting this book on the net: Think Python, How to think like a computer scientist. It is a very good beginner's book. Probably the best python beginner's book IMHO. And keep practicing
Re: Pls Help With Python Problem by bright007(f): 8:06am On Jul 27, 2013
@phenomenonVFX:i tried the code but its returning a name error,thus;
"name" is not defined.pls help me check for any correction.Thanks for the recommendation,i will get it.
Re: Pls Help With Python Problem by Nobody: 8:52am On Jul 27, 2013
Okay I have modified the whole code. It has been long I wrote a python code last so thanks for the exercise. The reason why u are having an error may be because u are not running the code from the command line. Hence there is no need for the line that says "if __name__ == '__main__' " . Comment off that line by adding a # in front of the line. The code should still work. I dont know the version of python u are using but this code should work fine or need only minor tweaks with ingenuity on ur part.

"""
This is the full code.
depending on ur version of python, the raw-input function would return a string or an integer. If it is an integer then use it like that. If not, convert it to an integer using the int() function.
"""

def thrust(x):
..........if x <= 25:
...............return(x+25)
..........else return(25-x)

def turn(x):
..........if x % 10 == 0 and x >= 10:
.................return(x/10)
..........elif x % 10 == 9 and x >= 10:
..................return(x)
..........elif x < 10:
................return(x*10)
..........else
..............return int((str(x).reverse()))

if __name__ == '__main__': #u can remove this line.
.......print ("Please insert a number" )
.......y= raw_input()
.......x= int(y) #just to be safe
.......if x > 90:
.............print("Number out of range" )
........else
.............print("This is the thrust: " )
.............print (thrust(x))
.............print("This is the turn: " )
.............print(turn(x))
Re: Pls Help With Python Problem by bright007(f): 11:12am On Jul 27, 2013
ok...i use python 3.3.2 but also have version 2.7.3.I enter the codes and save it as a python script before running with IDLE.
I will try once again.thanks

thanks phenomenonVFX.I have download the book.
Re: Pls Help With Python Problem by bright007(f): 11:13am On Jul 27, 2013
ok...i use python 3.3.2 but also have version 2.7.3.I enter the codes and save it as a python script before running with IDLE.
I will try once again.thanks phenomenonVFX.I have download the book.
Re: Pls Help With Python Problem by Nobody: 11:19am On Jul 27, 2013
Ok then. U will find that book very useful. It teaches a lot quite well. Let me know if there's anything that isnt clear or that doesnt work. I will be glad to be of help. I probably should download Python again. And try my hands on some advanced stuff.
Re: Pls Help With Python Problem by Nobody: 11:21am On Jul 27, 2013
Lest I forget, are u doing this as a hobby or is it some sort of academic chore?
Re: Pls Help With Python Problem by Ajibel(m): 12:09pm On Jul 27, 2013
Phenomenom, pls can u post ur solution online and paste d link here. I've tackled a similar bright007's problem before and its quite diff from ur solution!
Re: Pls Help With Python Problem by Nobody: 12:17pm On Jul 27, 2013
Ajibel: Phenomenom, pls can u post ur solution online and paste d link here. I've tackled a similar bright007's problem before and its quite diff from ur solution!

Of course u have to realise that there is more than one way to solve this problem and this isnt even the optimal solution. I only chose this for clarity considering the skill level of the OP. I will download python today and try to oblige ur request in more ways than one. Where will u like me to upload the code?

1 Like

Re: Pls Help With Python Problem by Ajibel(m): 12:24pm On Jul 27, 2013
Never mind. Thanks a lot. I was using mobile to view ur solution buh couldnt get ur codes clear. I've seen it with a pc nau and have understood so never mind. Thanks wink
Re: Pls Help With Python Problem by Nobody: 12:28pm On Jul 27, 2013
^^^Okeydokey
Re: Pls Help With Python Problem by bright007(f): 12:42pm On Jul 27, 2013
PhenomenonVFX: Lest I forget, are u doing this as a hobby or is it some sort of academic chore?
....though not a computer science student,i love programming have always wanted to be able write my own imaginatinations.
thanks phenomenonVFX.I have tried the code with success.
the "y=raw_input" was returning error so i deleted it.
the "return int((str(x),reverse()))" is returning error and not working.hence,if i type turn(35) into IDLE,it returns an error thus:
name "reverse" not defined.

but if i type turn(10) and turn(19),it returns my expected answer(1 and 19),hence its working.
Re: Pls Help With Python Problem by bright007(f): 12:50pm On Jul 27, 2013
@phenomenoVFX:Pls if you are not so busy,i would like you to give me a line by line explanation of how you defined the "turn" function.i cant seem to understand everything.i am marvelled that its working.
Re: Pls Help With Python Problem by bright007(f): 1:38pm On Jul 27, 2013
also,how do i use python to search an already existing data.the data contains a range of numbers that occurs as a set of 4 different numbers(within the range).the sets are arranged and numbered sequentially.here is an example:
set(1)['22','35','18','03']
set(2)['38','65','13','45']
set(3)['02','65','52','49']
set(4)['53','31','58','18']
set(5)['18','35','66','20']
from the above data,in set 1 35 in position 1 #since items in python starts counting from zero# "jumps to" or repeats again at set 5 or simply put it repeats again after counting 5 sets #that is from set 1 to 5#.
how do i tell python to print the number (in this example 35) and the sets where this property occurs.
##my head is seriously aching after pondering over this problem since morning without success.
Re: Pls Help With Python Problem by Nobody: 2:32pm On Jul 27, 2013
bright007: ....though not a computer science student,i love programming have always wanted to be able write my own imaginatinations.
thanks phenomenonVFX.I have tried the code with success.
the "y=raw_input" was returning error so i deleted it.
the "return int((str(x),reverse()))" is returning error and not working.hence,if i type turn(35) into IDLE,it returns an error thus:
name "reverse" not defined.

but if i type turn(10) and turn(19),it returns my expected answer(1 and 19),hence its working.


Hmmm. I am trying to make this code as simple as possible for u.
Let me see.......
U can replace the "raw-input()" with "y= input()" instead.
Also replace the "int(str(x).reverse())" with "int(str(x)[::-1])"
let me know if u still need the explanation.
Re: Pls Help With Python Problem by Nobody: 3:33pm On Jul 27, 2013
bright007: also,how do i use python to search an already existing data.the data contains a range of numbers that occurs as a set of 4 different numbers(within the range).the sets are arranged and numbered sequentially.here is an example:
set(1)['22','35','18','03']
set(2)['38','65','13','45']
set(3)['02','65','52','49']
set(4)['53','31','58','18']
set(5)['18','35','66','20']
from the above data,in set 1 35 in position 1 #since items in python starts counting from zero# "jumps to" or repeats again at set 5 or simply put it repeats again after counting 5 sets #that is from set 1 to 5#.
how do i tell python to print the number (in this example 35) and the sets where this property occurs.
##my head is seriously aching after pondering over this problem since morning without success.

There are many ways to solve this problem. Again I will try to use the longer method to make it clearer. And also, depending on ur version of python, there may be various revision to this code before it can work.

"""
One solution would be to put all of the sets in one list. And then create a container to hold the data as u are counting.
"""

set1=['22','35','18','03']
set2=['38','65','13','45']
set3=['02','65','52','49']
set4=['53','31','58','18']
set5=['18','35','66','20']

Big_Set= [set1, set2, set3, set4, set 5]

Cont=[] #container

for set in Big_Set:
........for item in set:
...........if item not in Cont:
...............Cont.append(item)
...........else print(item+ "Already in set" )
....................x = item
....................Occur_func(x)

def Occur_func(n):
.......for set in Big_Set:
............for item in set:
................if item is n:
....................print("it occurs in set "+Big_Set.index(set)+1)
Re: Pls Help With Python Problem by Ajibel(m): 3:35pm On Jul 27, 2013
9ce 1 pheno. I got sth similar to urs
Re: Pls Help With Python Problem by Nobody: 3:51pm On Jul 27, 2013
Ajibel: 9ce 1 pheno. I got sth similar to urs
Good to know. smiley
Re: Pls Help With Python Problem by bright007(f): 4:16pm On Jul 27, 2013
thanks once again pheno...i will try the correction for the "turn" function.
Re: Pls Help With Python Problem by bright007(f): 4:49pm On Jul 27, 2013
bright007: here is an example:
set(1)['22','35','18','03']
set(2)['38','65','13','45']
set(3)['02','65','52','49']
set(4)['53','31','58','18']
set(5)['18','35','66','20']
from the above data,in set 1 35 in position 1 #since items in python starts counting from zero# "jumps to" or repeats again at set 5 or simply put it repeats again after counting 5 sets #that is from set 1 to 5#.
how do i tell python to print the number (in this example 35) and the sets where this property occurs.
.
thanks for your prompt reply.
based on the data above,pls help me to define the occur_function such that the user just searches for a specific or any number in the data based on properties imagined by the user.for example the user may search the data for any number that is in a particular or any position in any set which subsequently re-occurs at a particular/ any position after counting an inputed number of sets.
Re: Pls Help With Python Problem by bright007(f): 5:23pm On Jul 27, 2013
.
Re: Pls Help With Python Problem by Nobody: 5:26pm On Jul 27, 2013
bright007: thanks for your prompt reply.
based on the data above,pls help me to define the occur_function such that the user just searches for a specific or any number in the data based on properties imagined by the user.for example the user may search the data for any number that is in a particular or any position in any set which subsequently re-occurs at a particular/ any position after counting an inputed number of sets.

I dont get the problem. Could u make it clearer?
Re: Pls Help With Python Problem by Ajibel(m): 5:28pm On Jul 27, 2013
Bright,, you started out with a tutorial that seems to be too technical or complicated for an absoulute beginner maybe that's why you are finding it difficult. But its fun to learn things the hard way. So when ur done with the current tutorial, I'd advice u go for 3 or more tutorials to go through. They are fun and you'd really enjoy the power held by the snake and maybe soonest find urself addicted... Never knew a friend from jokes section would be into programming too grin gudluck wink
Re: Pls Help With Python Problem by bright007(f): 5:38pm On Jul 27, 2013
PhenomenonVFX:

I dont get the problem. Could u make it clearer?
ok..i will make it clearer in my next post.i am happy that you are here to help me.thanks a million times.
@ajibel...i only have saturday and sunday to practice programming.so am trying to maximize my learning

(1) (2) (3) (4) (Reply)

Python Tutorial Group / My Journey To Programming / Php/mysql Help:how To Run Sql Query From A Link

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 61
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.