i have challenges implementing OOP in python - python

I am facing challenges implementing OOP in python to enable me to call the functions whenever i want , so far i have no syntax errors which makes quite challenging for me . The first part of the code runs ehich is to accept data but the function part does not run.
I have tried different ways of calling the function by creating an instance of it.
print (list)
def tempcheck(self,newList):
temp=newList[0]
if temp==27:
print ("Bedroom has ideal temperature ")
elif temp>=28 or temp<=26:
print ("Bedroom Temperature is not ideal ,either too low or too cold. ")
print ("Please to adjust the temperature to the optimum temperature which is 27 degree Celsuis")
# now to initialize args
def __init__(self,temp,puri1,bedwashroom,newList):
self.temp=temp
self.puri1=puri1
self.bedwashroom=bedwashroom
tempcheck(newList)
# now calling the functions
newvalue=tempcheck(list)
# where list contains the values from the input function.
I expected the function to to check the specific value at the location in the list provided which is called list and also for the function to return a string based on the if statements.

i got it right ,i figured out an alternative to my bug thanks for the critique however any further addition is welcome,
the main goal was to create a function that takes input and passes it to list to be used later i guess this code is less cumbersome
the link to the full code is pasted below

Related

Python - Function not updating

I've been facing strange results in a numerical problem I've been working on and since I started to program in python recently, I would like to see if you can help me.
Basically, the program has a function that is minimized for different values in a nested loop. I'll skip the details of the function for simplicity but I checked it several times and it is working correctly. Basically, the code looks like that:
def function(mins,args):
#I'll skip those details for simplicity
return #Value
ranges = ((0+np.pi/100),(np.pi/2-np.pi/100),np.pi/100)
while Ri[0] < R:
Ri[1] = 0; Ri[n-1] = R-(sum(Ri)-Ri[n-1])
while Ri[1] < (R-Ri[0]):
res = opt.brute(function, ranges, args=[args], finish=None)
F = function(res,args)
print(f'F = {F}')
Ri[1] += dR
Ri[2] = R-(sum(Ri)-Ri[n-1])
Ri[0] += dR
So, ignoring the Ri[] meaning in the loop (which is a variable of the function), for every increment in Ri[] the program makes a minimization of mins by the scipy.optimize.brute obtaining res as the answer, then it should run the function with this answer and print the result F. The problem is that it always get the same answer, no matter what the parameters I get in the minimization (which is working fine, I checked). It's strange because if I get the values from the minimization (which is an n-sized array, being n an input) and create a new program to run the same function and just get the result of the function, it returns me the right answer.
Anyone can give me a hint why I'm getting this? If I didn't make myself clear please tell and I could provide more details about the function and variables. Thanks in advance!

call function inside argument of a function in python

I am starting with python, and I am trying to understand the sample code that Phidget website give to me (I bought a Phidget Bridge 4 input where I have plug on 4 gauge cell). For my project I need to use python but not much use to it.
To read only one channel, sol only one gauge cell with my Phidget bridge, the website give me this code.
from Phidget22.Phidget import *
from Phidget22.Devices.VoltageRatioInput import *
import time
def onVoltageRatioChange(self, voltageRatio):
print("VoltageRatio: " + str(voltageRatio))
def main():
voltageRatioInput0 = VoltageRatioInput()
voltageRatioInput0.setOnVoltageRatioChangeHandler(onVoltageRatioChange)
voltageRatioInput0.openWaitForAttachment(5000)
time.sleep(5)
voltageRatioInput0.close()
main()
There is the function def onVoltageRatioChange(self, voltageRatio): which takes 2 arguments, self and voltageRatio, and this function is used inside setOnVoltageRatioChangeHandler(onVoltageRatioChange) with no argument. What I mean, it's that I do not get why we give as an argument onVoltageChange (which is normally a function) to the function setOnVoltageRatioChangeHandler...
It someone could explain it to me, it could help me to build my own code... at least try..
thank you
Phidget22 knows how to detect a voltage ratio change, but it doesn't know what you want to do with it: maybe you want to sound an alarm. maybe you want to send an email with the new ratio. maybe you just want to print it to the screen.
So it uses setOnVoltageRatioChangeHandler to ask you what you would like to do once it detects this voltage change, saying "hey, if you'll give me a function that takes a voltage ratio, I'll call it when I detect a change" then you can do:
setOnVoltageRatioChangeHandler(soundTheAlarm)
or
setOnVoltageRatioChangeHandler(sendAnEmail)
or:
setOnVoltageRatioChangeHandler(printToScreen)
The supplied onVoltageRatioChange will just print the new value to the screen.
Generally, passing a function as an argument is a great way to give flexibility in the way events are handled. It's basically saying "I know how to detect a certain situation or get a certain value. You let me know what you want me to do once I do"

How do I write this function in Python?

*I'm a Beginner...
My friend tried to help me a bit with this but I can't seem to solve it. I'm not really sure what to do so any help would be greatly appreciated.I get the following error in averageMPG,"name stats city is parameter and global".I also wasn't really sure how to write both functions in my readData function as you can see. The problem is in the picture. I haven't succeeded in part b so I haven't moved on,
def readData(carmodelData_city):
global stats_city,stats_hwy
infile=open("carModelData_city", 'r')
stats_city=[]
for s in infile.read.split():
stats.append(float(s))
return stats_city
def read_Data(carmodelData_hwy):
global stats_city,stats_hwy
infile=open("carModelData_hwy", 'r')
stats_hwy=[]
for s in infile.read.split():
stats.append(float(s))
return stats_hwy
def averageMPG(stats_city, stats_hwy):
global stats_city,stats_hwy
totals=sum(stats_city)
length=len(stats_city)
avg1=totals/length
print("The averge mpg city is", avg1)
totals1=sum(stats_hwy)
length1=len(stats_hwy)
avg2=totals/length
print("The average mpg highway is", avg2)
average=(avg1+avg2)/2
print("The combined averge mpg is", average)
def main():
global stats_city,stats_hwy
stats_city=readData("carModelData_city", "r")
stats_hwy=read_Data("carModelData_hwy", "r")
[enter image description here][1]main()
You named a function parameter stats_city, and also declared it a global value. Those two things are incompatible.
AFAICT, none of your code actually requires anything to be global in the first place, so stop declaring everything global, and you should be fine.
Well, fine on that specific error anyway. The massive overuse of global here feels an awful lot like cargo cult programming, and you have many other problems (e.g. infile.read.split() is going to try to split the read method of the file; you forgot parens, so it's not actually calling read to get data back). You're also returning at the end of the first iteration of each loop, when I suspect you want to finish the loops and return the accumulated values. You need to learn a lot more of the basics here; please talk to a professor or a tutor.
Also, your "stats.append()" calls should probably be "stats_city.append" in the first function and "stats_hwy.append" in the second. You will return after one iteration in each function unless you adjust your idents on the return call.

Why can't I get the output in Python?

def travel():
travel.s=0
travel.frate=[]
travel.tr=[]
def accomodation():
print"""specialises
1.place 1
a.hotel 1
b.hotel 2
Hotel1:ac/non ac rooms
Ac.for ac...
Noac.for non ac...."""
hd=[5000,6000]
hg=[4000,7000]
TAc=[1000]
Nac=[400]
ch5=input("Enter your choice")
fav=raw_input("ENter hotel choice")
mode=raw_input("Enter ac/no ac")
if(ch5==1):
for i in hd:
frate=hd[i]+TAc
else:
frate=hd[i]+Nac
if(ch5==2):
for i in range(0,2,1):
frate=hg[i]+TAc
else:
frate=hg[i]+Nac
accomodation()
travel()
When i run the program , i get the error as List Index out of range.but in hd and hg list,there are only two elements, so index number will be from 0 right?? Is there anything i should import?? I even gave statements like this:
travel.frate=travel.hg[i]+TAc
but it still doesn't come.Thank you for your effort.
the indentation is proper now,but the output is still not coming.
The specific issue you are encountering is caused by these lines here:
if(ch5==1):
for i in hd:
frate=hd[i]+TAc
hd[i] looks at hd[5000] and hd[6000] which will throw an IndexError.
But if you fix that, you're going to run into another error, because
frate=hd[i]+TAc
tries to add a list TAc to an integer hd[i], which is an unsupported operation. You probably need frate=hd[i]+TAc[0], or even better, make TAc a number rather than a list. This issue occurs elsewhere in your code as well.
Finally, while not causing explicit issues in your code right now, there are two other problems:
ch5=input("Enter your choice") is dangerous since input tells Python to run whatever code snippet the user enters. In this case, much safer to do ch5=int(raw_input("Enter your choice"))
for i in range(0,2,1): is really for i in range(2): - only use the starting index and jump parameters if you need to change them from their default, namely 0 and 1.
There are other issues like variable scope (you're expecting travel.frate to be modified from within accommodation, but that's not happening) and defining variables in functions like travel.frate (not invalid syntax, but definitely strange) but those are probably better addressed outside of this question.

Python Global Variable Not Working

I have an extremely long program for a Canasta game that I am writing, in it there is a variable that ends the turn of the player if it equals a certain value. However, the turn repeats itself.
I have read many different articles and posts on forums like this one, but none of these seem to work. I even have a debug system to print the value of the variable before the if statement, and the variable is the right value, but the if statement doesn't see the same value.
I can't give the entire code, as it is extremely long, but I can give the sections that use this variable. I am using Python 2.7.2 and cannot make files separate from the main file to import.
The first line of the program is:
endTurn=1
The following function is called when conditions to win the game are met, but I have not been able to test that this works because of the bug.
def winGame(player):
global endTurn
gameWinner=player
gameWinner["score"]+=100
endTurn=0
The following function is called when a move is made. The variable decide is a raw_input() variable.
def move(player,decide):
global endTurn
theMove=decide.lower()
#if player says to end turn
if theMove=="end":
#until player discards something
discarded=0
while not discarded:
displayHand(player)
#ask player for discard
discard=int(raw_input(" Enter the list number of the card you wish to discard:"))-1
#if discard ID is in player's hand length
if not discard<0 and not discard>len(player["hand"])-1:
#add card to discard pile and remove from hand
discardPile.append(player["hand"][discard])
del(player["hand"][discard])
discarded=1
debug("Before changing, endTurn is %s"%str(endTurn))
endTurn = 0
debug("After changing, endTurn is %s"%str(endTurn))
if theMove=="new book":
newBook(player)
if theMove=="add to book":
addBook(player)
Here is where the turn should be ended. turn(thePlayer) repeats the function. The debug statement shows the correct value, 0, but the if still reads a 1. The function this is in also has the global endTurn at the top.
debug("If ending turn, endTurn of %s should be 0."%str(endTurn))
if endTurn==1:
turn(thePlayer)
Any help is greatly appreciated!
EDIT: The code is available at http://labs.codecademy.com/CV9z#:workspace. I ask that anyone viewing the code does not modify it, so that other people can see the true code.
I fixed the bug. I removed the endTurn variable and instead made the function that ends the turn just do the turn of the next player. It also simplified my code a bit.
I am wondering though, is using this method of running a function inside of itself over and over and over again (without leaving) rather messy or maybe slowing down my program?
EDIT: I now realize that this answer does not exactly help with anyone else who is stuck with global variables...
The previous answer I posted was just wrong, as it was based on a misinterpretation of the namespace docs as I had read them (Thanks to TheifMaster for pointing out my error), so this is a SEVERELY EDITED ANSWER:
The problem is in the while loop that I was able to read when the link to the entire code was posted after the erroneous answer I gave earlier.
while gameWinner==0:
endTurn=1
turn(player1)
if not gameWinner==0:
endTurn=1
turn(player2)
The game can never get to player two in this loop until gameWinner!=0.
I added the entire code to my sandbox and changed it to:
while gameWinner==0:
endTurn=1
turn(player1)
if gameWinner==0: #if player1 did not win yet
endTurn=1
turn(player2)
However, the game is still buggy... No one can win! gameWinner is not changed globally by the winGame() function until you add it to the global statement as I have shown here.
def winGame(player):
global endTurn, gameWinner
gameWinner=player
gameWinner["score"]+=100
debug("gameWinner == " + str(gameWinner))
endTurn=0

Categories

Resources