def day_display_control(entered_variable,controlling_number,return_message):
if entered_variable == controlling_number:
return(return_message)
I have been trying to find a problem here, as IDLE keeps on giving an indentation error saying expected an indented block yet I have found none so far, my indentation width is 4 and I tried using only tab as well, haven't found the solution, thank you in advance as this is probably a really basic question.
P.S:
I have also tried to debug the rest of the code without this line, yet while this gives the same error:
def day_display():
day_display_number = day % 7
day_display = day_diplay_control(day_display_number,0,Monday)
day_display = day_diplay_control(day_display_number,1,Thuesday)
day_display = day_diplay_control(day_display_number,2,Wednesday)
day_display = day_diplay_control(day_display_number,3,Thursday)
day_display = day_diplay_control(day_display_number,4,Friday)
day_display = day_diplay_control(day_display_number,5,Saturday)
day_display = day_diplay_control(day_display_number,6,Sunday)
Do not mind the quality of the code, the problem is that previous 'def's don't cause this to happen, such as;
def typeWait(message,delay):
message = str(message)
print(message)
sleep(delay)
P.P.S: I have just realized, as of yesterday, that python was not 3.6 anymore, but instead 3.7 alpha 2, which leads me to believe that this is either a new feature or a bug, I have found no articles about either of them yet, so if anyone knows what the problem is, I would appriciate it a lot.
I agree with #jasonharper. Check if you mistyped = instead of ==, and also if any of these are missing: ), ] or }. It might not be near the block of these functions.
Related
I've narrowed it down to the elif part of my setup() function stopping my code from functioning as the first if statement runs fine when used, it also doesn't return any error messages when the code doesn't continue.
I've removed this function to ensure my other ones run as normal, which they do, which means it must be to do with this specifically?
Any help would really be appreciated, I've been at this for days and can't find find the problem, I'm probably being stupid and missing something simple.
This is also my first time asking a question here, if I've haven't given the necessary information or more is needed, apologies and please do ask.
Here's the function:
class votingSystem():
def setup():
setup = False
while not setup:
f1 = open('lib.py')
search = 'abstain'
if search not in f1.read():
setup = True
with open('lib.py', 'w') as file:
file.write('abstain = 0' + '\n' * 5)
elif search in f1.read():
setup = True
with open('lib.py', 'w') as file:
file.write('\n\n\n\n' * 5)
setup()
I've got some data in np.ndarrays that I want to normalise to be between 0 and 1. I asked here what the best way to do it was, and how to avoid dividing by 0, and someone told me the best way was to use np.nan_to_num().
That seemed to work, I don't think I had any problems with it.
Since that previous question, my code has evolved, and I now want to do the same thing with the three different arrays trainingsignals, validationsignals and testsignals:
TrainingSigMaxes = np.max(trainingsignals, axis = 1)
TrainingNormSignals=np.nan_to_num(trainingsignals/TrainingSigMaxes[:,np.newaxis])
ValidationSigMaxes = np.max(validationsignals, axis = 1)
ValidationNormSignals=np.nan_to_num(validationsignals/ValidationSigMaxes[:,np.newaxis])
TestSigMaxes = np.max(testsignals, axis = 1)
TestNormSignals=np.nan_to_num(testsignals/TestSigMaxes[:,np.newaxis])
But when I run the code it gives me the error message: "RuntimeWarning: invalid value encountered in true_divide".
Can anyone help me solve this issue?
Thanks a lot in advance.
It is just a warning. You will still get the 'approximately correct' output.
If you want to hide warnings, you could run this beforehand:
import warnings
warnings.filterwarnings("ignore")
or for more customization, you could refer this.
Note : 'Approximately correct' value is based on the function
numpy.nan_to_num. You could refer the documentation.
I am fairly new to Python (and programming in general), so please excuse my lack of knowledge or understanding to something you may find obvious. I'm not stupid though, so hopefully I should be able to work it out.
I am making a small text-based survival game, and I have encountered an issue which I cannot seem to solve, which is the:
AttributeError: 'int' object has no attribute 'sleep'
In the console when I try and run my program.
import time , sys , random , shelve
# /gather command
if '/gather' in Input and command_state == True:
if 'wood' in Input:
print('Collecting wood...')
if tool != "Axe":
time.sleep(random.randrange(5 , 10))
print("Test")
else:
time.sleep(random.randrange(5 , 10))
print("Test")
I really don't understand what is causing this and after looking through the advice given on similar topics I have found no solution. Any help would be appreciated!
If you'd like me to put up the whole script, please just ask. I have only put up the block of code that was causing the issue (because none of the other code seemed to affect anything here).
As commented above you are overwriting the time module by making a variable named time. Simply rename the time variable!
I already read the other questions and answers but couldn't implement any of the solutions to my code. I'm still clueless about the reason why this code gives a runtime error.
I'm trying to submit the code on CodeChef, yet it gives the Runtime Error(NZEC), although the code runs flawlessly on my console for some inputs. Here's my code:
def GetSquares(base):
if not base or base < 4:
return 0
else:
x = (base - 4) - (base % 2) + 1
return x + GetSquares(base - 4)
num_test = int(input())
for test in range(num_test):
base = int(input())
print (int(GetSquares(base)))
Codechef's explanation for NZEC:
NZEC stands for Non Zero Exit Code. For C users, this will be
generated if your main method does not have a return 0; statement.
Other languages like Java/C++ could generate this error if they throw
an exception.
The problem I'm trying to solve:
https://www.codechef.com/problems/TRISQ
The problem description says that the input is constrained to be < 10^4. That's 10,000! Your code will need to make 10,000/4 = 2500 recursive calls to GetSquares, that's a lot! In fact, it's so much that it's going to give you, fittingly, this error:
RuntimeError: maximum recursion depth exceeded
You're going to have to think of a better way to solve the problem that doesn't involve so much recursion! Because you're doing this coding challenge, I'm not going to give a solution in this answer as that would sort of defeat the purpose, but if you'd like some prodding towards an answer, feel free to ask.
The question puts a constraint on the value of 'B' which is 10000 at max, which means there are a lot of recursive calls and giving a runtime error. Try solving using iteration.
I just came accross the following code in an existent project, which I'm working on:
if True:
x = 5
y = 6
return x+y
else:
return 'Something
Inside the if True are lots of conditions and some will also return the function already.
Why would somebody write in that way? The code contained some other bugs also, but was just wondering about the if True: statement as it didn't make any sense to me. Probably also pretty stupid to ask it, but was wondering hehe.
It might be a remnant of debugging or refactoring. It may be that instead of True, there was orginally a condition or variable there but it has now been replaced by True. The developer perhaps left it there without refactoring or cleaning it up all the way.
If you're free to edit the code as you wish and you're sure that the else is no longer needed, then you can remove it. It indeed makes no sense to have code in your codebase that will never run.
True doesn't necessarily mean True
True = False
if not True :
print "True is false" # This prints ok
Honestly, I don't think anyone would code like this.
Does not make any sense to me, my guess is that someone wanted to have two distinct code paths that he could alternate between a'la using #if 1 .. #else -> #if 0 ... for debugging or such purposes.
Other possibility was that, as #SimeonVisser suggested, the original developer was refactoring or cleaning up the code (and did not have an emulator that allows one to easily remove 1 step of indentation from a block of code)
It could be a flag used for debugging.
It's simply used to ensure that the else: block is never executed.
I have used if True: for some blocks to ensure that my code really does what I want. Usage for debugging or refactoring.
All in all it makes no real sense to use this in an application but for testing or debugging it's somehow acceptable.