Useless if statement in yum source [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
I was curious about how something worked in yum so I was looking at some of its score code and I found this line in the erasePkgs function in cli.py.
if False: pass
elif basecmd in ('erase-n', 'remove-n'):
rms = self.remove(name=arg)
.
.
.
The if False: pass does nothing correct? It never gets into that branch it always just skips to the next one doesn't it?
Here is the link to the source code: https://github.com/rpm-software-management/yum/blob/master/cli.py. It's on line 1268.

This appears to be the developer's idiom for a generalized switch statement.
if False: pass
elif basecmd in ('erase-n', 'remove-n'):
rms = self.remove(name=arg)
elif basecmd in ('erase-na', 'remove-na'):
...
elif basecmd in ('erase-nevra', 'remove-nevra'):
...
else:
...
which is ever so slightly more readable than
if basecmd in ('erase-n', 'remove-n'):
rms = self.remove(name=arg)
elif basecmd in ('erase-na', 'remove-na'):
...
elif basecmd in ('erase-nevra', 'remove-nevra'):
...
else:
...

Related

Python Temperature converter output is none [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Im trying to create a function which will convert temperatures based on the temperature that is being converted from to the temperature which we want. However the output of my code is none when I test it.
def convertTemperature(T, unitFrom, unitTo):
if unitFrom=="Fahrenheit" and unitTo=="Celsius":
T=(T-32)/1.8
return T
elif unitFrom=="Kelvin" and unitTo=="Celcius":
T=T-273.15
return T
elif unitFrom=="Celcius" and unitTo=="Fahrenheit":
T=1.8*T+32
return T
elif unitFrom=="Kelvin" and unitTo=="Fahrenheit":
T=1.8*T-459.67
return T
elif unitFrom=="Celcius" and unitTo=="Kelvin":
T=T+273.15
return T
elif unitFrom=="Fahrenheit" and unitTo=="Kelvin":
T=(T*459.67)/1.8
return T
the unitFrom parameter is the current temperature, T is the temperature and unitTo is the temperature to which I want to convert.
I tried testing it with print(convertTemperature(50.0, "Fahrenheit", "Celcius")) but the output was none.
It is just a spelling mistake Celsius not Celcius
This will work
def convertTemperature(T, unitFrom, unitTo):
if unitFrom=="Fahrenheit" and unitTo=="Celsius":
T=(T-32)/1.8
return T
elif unitFrom=="Kelvin" and unitTo=="Celsius":
T=T-273.15
return T
elif unitFrom=="Celsius" and unitTo=="Fahrenheit":
T=1.8*T+32
return T
elif unitFrom=="Kelvin" and unitTo=="Fahrenheit":
T=1.8*T-459.67
return T
elif unitFrom=="Celsius" and unitTo=="Kelvin":
T=T+273.15
return T
elif unitFrom=="Fahrenheit" and unitTo=="Kelvin":
T=(T*459.67)/1.8
return T
print(convertTemperature(50.0, "Fahrenheit", "Celsius"))
Hi you have use wrong word Celsius
It would be Celcius
Your program is right except spelling mistake

Need a code to allow the user to restart my python quiz at any time [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I would like to know how I could allow my code to let the user restart the quiz at any time throughout the program however, I have no idea how to do this from scratch. It preferably needs to be fairly simple. Would it be easiest doing it as an if statement and if so what would I put in it? I tried this:
while True:
# main program
while True:
answer = raw_input('Run again? (y/n): ')
if answer in ('y', 'n'):
break
print 'Invalid input.'
if answer == 'y':
continue
else:
print 'Goodbye'
break
breaking your problem down into parts is the first step
# 1. ask a question and validate the response
def ask_question(prompt,possible_choices):
while True:
result = raw_input(prompt)
if result in possible_choices:
return result
print "Invalid Response Please Enter One Of:",possible_choices
# 2. run a whole quiz (ask all our questions)
def do_quiz():
answers = []
for question_text in questions_list:
answer = ask_question(question_text,['a','b','c','d','quit','restart')
if answer == "restart":
return False
# instead of returning false we could also simply call recursively
# return do_quiz()
elif answer == "quit":
print "Adios muchacho"
sys.exit(0)
else:
answers.append(answer)
return answers
# 3. play forever (or atleast until the user wants to quit...)
while True:
results = do_quiz()
if not results:
print "Lets Start Over!!"
continue
else:
check_answers(results)
if raw_input("Play again?")[0].lower() == "n":
print "Goodbye!"
sys.exit(1)

Why does my while loop not stop? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
RANDOM_COR=random.randrange(5,6)
def check_xy_data():
global COUNT
COUNT=0
input_xy=input("input(x,y) : ")
think_xy=list(map(int,input_xy.split(",")))
if(random_array[think_xy[0]][think_xy[1]] == "C"):
screen_array[think_xy[0]][think_xy[1]] = "O"
COUNT=COUNT+1
else:
screen_array[think_xy[0]][think_xy[1]] = "X"
def main():
make_intro()
init_screen_array ()
init_random_array ()
make_random_num(RANDOM_COR)
while(True):
check_xy_data()
draw_outline_start(TOTAL_COL_NUM//2)
draw_out_rowline(TOTAL_COL_NUM//2, "Input : ")
draw_out_rowline(TOTAL_COL_NUM//2, "Correct : ")
draw_out_rowline(TOTAL_COL_NUM//2, "Error : ")
draw_out_rowline(TOTAL_COL_NUM//2, "Total : ")
draw_outline_mid(TOTAL_COL_NUM//2)
if(COUNT==RANDOM_COR-1):
break
The if at the bottom of my code is supposed to get me out of the while loop, but I'm stuck in an infinite loop. Help?
(assignment, 2016) 예고편 The Assignment | 어싸인먼트 감독: 월터 힐 각본: 월터 힐, 데니스 해밀 출연: 김성훈 출연 현빈, 유해진, 김주혁 개봉 2016 한국 상세보기 그간...
Try this change:
RANDOM_COR=random.randrange(5,6)
COUNT = 0
def check_xy_data():
global COUNT
With COUNT inside check_xy_data, you set it back to 0 on every call. It can never reach more than 1. Your check is whether it's in the range 5-6. This is never true, so you can never leave the loop.
Note that trivial debugging skills would have found this: just stick a print statement before you test your loop condition, to see what the values are. Use that next time ... :-)

recursion and object recreation [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am new to python and I am curious to why this error is occurring. This error only occurs when I input a number (selection) greater than 3. When the input is 1,2, or 3 it works as if it is supposed to
error:
File "/Users/username/Development/foodcalc/interface.py", line 12, in display_options
display_options()
NameError: global name 'display_options' is not defined
code
from interface import Interface
interface = Interface();
print "welcome"
print "what would you like to do?"
interface.display_options()
my interface.py
class Interface:
#options = ["1 - create a meal plan","2 - add food","3 - look up food"]
def display_options(self):
options = ["1 - create a meal plan","2 - add food","3 - look up food"]
for choice in options:
print choice
selection = int(raw_input())
if selection > 3:
print "Incorrect selection. Please try again."
display_options()
else:
if selection == 1:
print "meal"
elif selection == 2:
print "add"
else:
print "search"
When you attempt to call a member function or method you need to preface it with self.. Otherwise the interpreter looks for a global function with the name you've requested.
It is because you didn't use an instance to call the function. add self. in-front of the function.
As a side note; it would be better to implement this in a while loop seeing as a continuous entry of incorrect values will cause your program to crash due to reaching the recursion limit.
To prevent this bug, consider writing the function like this:
def display_options(self):
options = ["1 - create a meal plan","2 - add food","3 - look up food"]
selection = 0
while selection > len(options) or selection <= 0:
for choice in options:
print choice
try:
selection = int(raw_input())
if selection > len(options) or selection <= 0:
raise Exception()
except:
print "Incorrect selection. Please try again."
if selection == 1:
print "meal"
elif selection == 2:
print "add"
else:
print "search"

Return outside function error - help needed [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
def getPressAve(odbname):
odb=openOdb(odbname)
lastFrame=odb.steps['Step-1'].frames[-1]
pressure=lastFrame.fieldOutputs['CPRESS']
press=[[0,0]] # sets the first element to [0,0]
for n in pressure.values:
gridPt=part1.nodes.getFromLabel(n.nodeLabel)
coord=assemb.getCoordinates(gridPt)
press.append([n.nodeLabel,n.data,coord])
press=avePress=press[1:] # removes the first element
press.sort(Comp_X)
print ('pressure extracted')
index=0
while index<len(press):
sum=0
tally=0
if index!=0:
sum=sum+press[index-1][1]
tally=tally+1
if index!=1:
sum=sum+press[index-2][1]
tally=tally+1
if index!=2:
sum=sum+press[index][1]
tally=tally+1
if index<len(press)-1:
sum=sum+press[index+1][1]
tally=tally+1
if index<len(press)-2:
sum=sum+press[index+2][1]
tally=tally+1
average=sum/tally
avePress[index][1]=average
index=index+1
odb.close()
print ('pressure averaged')
return avePress
In Python, indentation matters. As is, you're defining a function called getPressAve which does only this:
odb=openOdb(odbname)
After you've defined your function, you go on to do
lastFrame=odb.steps['Step-1'].frames[-1]
and such outside of the function. That's not what you want. The solution is to indent everything after that odb=openOdb(odbname) line to that level, so those lines are interpreted as being part of the body of the function.
You forgot to properly indent your code:
def getPressAve(odbname):
odb=openOdb(odbname)
...
print ('pressure averaged')
return avePress
As yours has not been, the return keyword is featured outside of a function, and hence the error: SyntaxError: 'return' outside function.

Categories