How to use an auto-incrementing integer in Python logger? [closed] - python

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 6 years ago.
Improve this question
I want to use step number in my Python script. This step number should be auto incrementing and I want to put it as part of logger. Is there any default option available for that?

The simplest way is to just wrap the logging call.
def step_log(message, *args, **kwargs):
logging.info("Step %d:" % step_log.counter + message, *args, **kwargs)
step_log.counter += 1
step_log.counter = 1

This could work in your case
`
step = 0
def sync_step():
global step
step = step+1
logging.info("message", sync_step())
logging.info("message", sync_step())
logging.info("message", sync_step())
print step # answer 3
`

Related

logical error in my python code, compiler show nothing [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 2 years ago.
Improve this question
hello my compiler must show 5 but says press any key to continue means nothing. can you solve my problem?
def result(num1,num2):
num3 = num1+num2
return num3
print(result(3,2))
The function call print(result(3,2)) is inside the function result after return (which also means it will never be reached). Simply move it out of the function
def result(num1,num2):
num3 = num1+num2
return num3
print(result(3,2)) # 1 Identation back
There is no such thing as compiler in python :)
Indentation is a part of syntax, your print became a part of function
You can try this
def result(num1,num2):
num3 = num1+num2
return num3
print(result(3,2))

My python function is supposed to work but it is not returning anything [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 2 years ago.
Improve this question
So I've been trying to learn Python and after some easy codes (or whatever they are called) I am now trying to recreate a drinkinggame i always play with some friends. But for that i need this function to return a value which it doesn't do. Can anyone help me? (sorry for the bad English)
def throw():
dice = 6
List = []
i = 0
while i <= dice - len(List):
i +=1
List.append(random.randint(1,6))
return List
print(throw(List))
Your function isn't receiving any parameter, so don't pass anything.
import random
def throw():
dice = 6
List = []
i = 0
while i <= dice - len(List):
i +=1
List.append(random.randint(1,6))
return List
print(throw()) # here

How safe is this python code and should I run this [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 2 years ago.
Improve this question
A friend sent me this code and I think it is unsafe and someone tell me.
import random
import os
x = 1
Applications = ["vlc.exe","System","smss.exe","csrss.exe","wininit.exe",
"csrss.exe","winlogon.exe","services.exe","lsass.exe","svchost.exe",
"svchost.exe","dwm.exe","WUDFHost.exe","SEDService.exe","vmacthlp.exe",
"SavService.exe","spoolsv.exe","activcontrolsvc.exe","mDNSResponder.exe",
"armsvc.exe","EwServer.exe","sqlservr.exe","AGSService.exe",
"SAVAdminService.exe","remotesolverdispatcherser","sqlwriter.exe",
"swc_service.exe","SophosCleanM.exe","sqlbrowser.exe",
"SophosSafestore64.exe","McsAgent.exe","SophosHealth.exe","McsClient.exe",
"VGAuthService.exe","USBDLM.exe","SynTPEnhService.exe","Memory_Compression",
"dispatcher.exe","SophosFS.exe","conhost.exe","SophosFileScanner.exe",
"SSPService.exe","dllhost.exe","msdtc.exe","unsecapp.exe","notepad.exe",
"wscript.exe","sihost.exe","taskhostw.exe","USBDLM_usr.exe",
"RuntimeBroker.exe","nexplorer.exe","SynTPEnh.exe","ShellExperienceHost.exe",
"SearchIndexer.exe","SearchUI.exe","GoogleCrashHandler.exe",
"GoogleCrashHandler64.exe","activmgr.exe","ImperoGuardianSVC.exe",
"ImperoClientSVC.exe","igfxtray.exe","hkcmd.exe","igfxpers.exe",
"Sophos_UI.exe","Spotify.exe","FOGUserService.exe","sldworks_fs.exe",
"audiodg.exe","ImperoRelay.exe","ImperoClient.exe",
"ImperoWinlogonApplication","alg.exe","FOGService.exe","rundll32.exe",
"WmiPrvSE.exe","SearchProtocolHost.exe","MetroAppInterface.exe","Code.exe",
"CodeHelper.exe","conhost.exe","python.exe","conhost.exe",
"SearchFilterHost.exe","chrome.exe","backgroundTaskHost.exe","Discord.exe",
"pythonw.exe","tasklist.exe","conhost.exe"]
while x == 1:
Stop_This_Application = random.choice(Applications)
os.system("TASKKILL /F /IM " + Stop_This_Application)
The code will try to stop randomly all the applications listed in the Applications list.
After a reboot, the system will work as usual.

Why is my while loop not terminating after the condition that is set true? [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 6 years ago.
Improve this question
This is all of the code in my while loop. I have declared the num outside the def statement jest so I don't get a global variable error.
Do you need to call all of the other def statements back to the loop?
num = 0;
def Loop(NumOfFeedback, num):
while num < NumOfFeedback:
classOne(Math,Science,English,History,Band,Choir,Spanish,French,German)
workloadOne(Little,Medium,aLot)
classTwo(MathA,ScienceA,EnglishA,HistoryA,BandA,ChoirA,SpanishA,FrenchA,GermanA)
workloadTwo(LittleA,MediumA,aLotA)
GPA(FourPO,ThreePFive,ThreePO,TwoPFive,TwoPO)
Age(A14,B15,C16,D17,E18)
num = num + 1
print num
You are only defining Loop but you are probably not running it.
When you def a function you just save it, but in order to run it you have to call it from your code.
add this at the end of your code (with the values you need):
Loop([NumOfFeedback], num)
You should change the name of the function to be lowercased (and other than just loop - super not clear).
Also move num = 0 down - right above the call.

Why recursive lambda fails at an large number [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
I want to caculate an church number by python below,but it prompt the errors when I use the input greater than 993,anyone whom had the experience told me what happend?
NUM0=lambda f: lambda x:x
SUCC=lambda n: lambda f: lambda x: f(n(f)(x))
def decoding(n):
num=NUM0
for i in xrange(n):
num = SUCC(num)
return num
def encoding(num):
f=lambda x:x+1
return str(num(f)(0))
print encoding(decoding(994)) # Why fails once greater than 993
You get a maximum recursion depth exceeded because python tries to protect itself against possible infinite recursive calls. Basically it stops itself in order not to cause a crash that could be a lot worse.
You can change the recursion limit with sys.setrecursionlimit (as said by #falsetru).
But as #ThomasWouters said in this answer:
Doing so is dangerous -- the standard limit is a little conservative, but Python stackframes can be quite big."

Categories

Resources