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 6 years ago.
Improve this question
I am fairly new to the programming world and am wondering why the following code will not only refuse to run, but my python software won't even give me any error messages whatsoever.I'm using Pythonista, an IOS app for python. I can't get the app to run this code (and it won't give me any error messages) and was wondering if it's my code itself, or is it just the application. Any knowledge on this issue would be greatly appreciated.
def starBits():
badMatchups = [Zelda, Cloud, Ryu]
worstMatchups = [Jigglypuff, Villager, Bayonetta]
print(badMatchups)[1:2]
print(worstMatchups)[1:1]
def main():
starBits()
main()
I'm not sure what you expect from this, but it's really funky syntax.
print(badMatchups)[1:2]
print(worstMatchups)[1:1]
If those slices are subscripts for the lists, you need them inside the call to print:
print(badMatchups[1:2])
print(worstMatchups[1:1])
By the way, do you realize that [1:1] is an empty slice? The second number is the first position not included. You may need
print(badMatchups[1:3]) # two elements
print(worstMatchups[1:2]) # one element
Also, are those elements external variables, or are they supposed to be literal names? If the latter, then you have to put them in quotation marks.
badMatchups = ["Zelda", "Cloud", "Ryu"]
worstMatchups = ["Jigglypuff", "Villager", "Bayonetta"]
With this change, the code runs; I hope it's what you want.
Can't get it to run? Reality check time ...
Full code, changes made:
def starBits():
badMatchups = ["Zelda", "Cloud", "Ryu"]
worstMatchups = ["Jigglypuff", "Villager", "Bayonetta"]
print(badMatchups[1:3])
print(worstMatchups[1:2])
def main():
starBits()
main()
Output:
['Cloud', 'Ryu']
['Villager']
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 20 days ago.
Improve this question
Very general question: I am attempting to write a fairly complext Python script.
#Part A: 100 lines of python code
#Part B: 500 lines of python code
#Part C: 100 lines of python code
Assume that I want "Part B" taken out of the picture for readability and debugging purposes, because I know that it is running well and I want to focus on the other parts of the code.
I would define a function like this:
def part_b():
#500 lines of python code
#Part A: 100 lines of python code
part_b()
#Part C: 100 lines of python code
The problem with this approach in my case is that there are more than twenty variables that need to be sent to "Part C". The following looks like bad practice.
def part_b():
global var1
global var2
global var3...
I am aware that I could return an object with more than twenty attibutes, but that would increase complexity and decrease readability.
In other words, is there a pythonic way of saying "execute this block of code, but move it away from the code that I am currently focusing on". This is for a Selenium automation project.
Sounds like what you're looking for is modules. Move part B into a separate file, and then import it.
import part_B
# part A here
part_B.run()
# part C here
# to access things that part B does:
do_part_C_stuff(part_B.something)
I am creating a Rock Paper Scissors game. When I run the application, I get a tkinter messagebox titled 'Tk_GetPixmap: Error from DIBSection' and says 'The parameter is incorrect'. Here's the image:
But the thing is I haven't used DIBSection or Tk_GetPixmap anywhere in my code. Could someone please explain why this error may come without usage?
I didn't get a traceback which makes it hard for me myself to debug and I dont know anything about DIBSection. I tried checking for other questions on StackOverflow but all of them used DIBSection in the code. I think I cannot create a minimal reproducible example for this so I have not pasted the code. If there is a need to do so, please ask. Thanks in advance for anyone who helps!
EDIT: Here's a small slice of code likely causing the error:
def start_round(self):
def rd_animate():
#--::Animation::--#
self.remove_wdgs()
self.PF_ani_lbl["text"] = "ROUND {}".format(self.round_no)
self.after(1000, self.place_wdgs)
print("start_round")
self.round = {}
#--::Animation::--#
rd_animate()
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 5 years ago.
Improve this question
I'm trying to solve for an equation in Python without using any scipy features. c = 5 and the equation is c = 10 - 20(exp(-0.15*x) - exp(-0.5*x)).
How do I solve for x with a tolerance of .0001.
Pardon my intro level programming here guys. This is the first class I've ever taken.
from math import exp
c = 5
def x(c):
c = 10 - 20(exp*(-0.15*x) - exp*(-0.5*x))
return x(5)
You might want to have a look at SymPy. It's a dedicated algebraic symbol manipulation library for Python with a BSD license. If you're looking for a "stock"/standard library solution, then as others have mentioned you're going to have to do some homework and potentially implement your own solver.
As a closing thought, unless this is a class assignment or your boss has a pathological hatred of third-party open source libraries, there's really no good reason not to use one of the SciPy packages. IIRC, they're largely implemented as highly-optimized C binaries wrapped in Python modules, so you get blazingly fast performance and the ease-of-use of a Python API.
It seems like you want to implement this "from scratch." A few hints:
We can simplify this a bit with algebra. What you really want is to find x such that exp(-0.15*x) + exp(-0.5*x) - 0.2 = 0
For a given value of x, you know how much error you have. For example, if x = 1, then c(1) = 1.267, so your error is 1.267. You need to keep "guessing" values until your error is less than 0.0001.
Math tells us that this function is monotonically decreasing; so, there is no point checking answers to the left of 1.
Hopefully you can solve it from these hints. But this is supposed to be an answer, so here is the code:
def theFunction(x): return exp(-0.15*x) + exp(-0.5*x) - 0.2
error = 1.267
x = 1
littleBit = 1
while (abs(error) > 0.0001):
if error > 0: x += littleBit
else: x -= littleBit
oldError = error
error = theFunction(x)
if (error*oldError < 0): littleBit *= 0.5
print x
Note, the last three lines in the loop are a little bit 'clever' -- an easier solution would be to just set littleBit = 0.00001 and keep it constant throughout the program (this will be much slower, but will still do the job). As an exercise, I recommend trying to implement it this simpler way, then time how long it takes both ways, and see if you can figure out where the time savings comes in.
The Task: It is planning that Nao should ask humans some questions and give answer variants. The people should give one answer variant per question but after every five question, Nao should say: Ok, you answered on 5 questions and gave 3 correct and 2 incorrect answers. Please, try again.
The realization: The questions and answer variants are realized in the Choreography Dialog but I can't understand how can I call the variable from Python script in the Dialog.
I call the calculated variable in Dialog (QiChat) like this:
topic: ~addition()
language: enu
u:([c]) Yes, it's the correct answer.
%script
p=+1
%script
But how in such case to do that the value of the variable should be understood by QiChat and nao will say the value of variables?
This example nao can't say, it will be missing during dialog.
%script
print p
%script
Such approach to connect dialog and python script doesn't work:
$cnt = %script p %script
Here is a dialog-service template that has dialog linked to some python code; see in the .top file for how to call the Python or get info from it:
u:(set {the} counter [to at] _~numbers)
setting counter to $1
^call(ALMyService.set($1))
u:(["check counter" "what is the counter?"])
So, ^call(ALMyService.get())
c1:(_*) the counter is $1
(this assumes your Python is running in a service; an example of that is also included in that template)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 8 years ago.
Improve this question
for reason, now my code has some weird formatting issue.
if __name__ == "__main__":
import doctest
# ### UNCOMMENT THESE ONE AT A TIME TO GET THE TESTS TO PASS.
# ### WHEN YOU ARE FINISHED, LEAVE ONLY THE "allTests.txt" line uncommented.
# doctest.testfile("noConsecDupsTests.txt", verbose=True)
# doctest.testfile("isVowelTests.txt", verbose=True)
# doctest.testfile("countVowelsTests.txt", verbose=True)
# doctest.testfile("allVowelsATests.txt", verbose=True)
# doctest.testfile("syllableHelperTests.txt", verbose=True)
# doctest.testfile("removeSilentETests.txt", verbose=True)
# doctest.testfile("removeEdWhenNotASyllableTests.txt", verbose=True)
# doctest.testfile("countSyllablesTests.txt", verbose=True)
doctest.testfile("allTests.txt", verbose=True)
Python 3.3.2 is saying that the indentation error is in the first line of the code... I'm really confused. I've never been so stumped on such a simple topic.
I would guess that you are trying to run this in the Python REPL? As opposed to saving it to a file and then running it?
Python REPL ends a code block after an empty line. Try saving this in a file, say test.py and then call it from command line using python test.py.