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 1 year ago.
Improve this question
def getmessages(_,m):
coin = ""
if m.chat.id == boticindneme:
coin += m.text
return coin
if getmessages(_,m) == "something":
when I use this it gives the error _ is not defined so how can I use a function in if else
That is strange code. As a convention, python programs use _ to denote a variable that you have to put in to make a program work, but don't really need. An example is a function that returns multiple variables but you don't want them all. Suppose you wanted the second thing in a string, you could do
foo = "1,2,3"
_, bar, _ = foo.split(",", maxsplit=2)
print(bar)
Python would unpack the resulting ["1", "2", "3"] into _, then bar, then _ again. But you don't care about _ so its okay to overwrite it.
But to put it into function parameter is weird. You are requiring a parameter that you don't use. That's harsh!
When you define a function, you are creating names that the function will use for its parameters, not what outside code needs to call things.
def getmessages(_,m):
coin = ""
if m.chat.id == boticindneme:
coin += m.text
return coin
This doesn't create variables outside of the function, you need to create those yourself. Since the function doesn't actually use _, you can put anything into it. None seems like a good choice. And since we don't know what this m thing is, I'll make something up.
foo = WhateverThatMThingIs()
if getmessages(None, foo) == "something":
print('okay")
Related
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 last month.
Improve this question
I have written a program in python which takes two variables from the user but I want after it takes one to not take the other one.
I tried with the if loop and the continue funtion after the the first variable so that it continues the program as designed without asking for the second variable
Just read one variable and use a conditional to determine if you want to take a second input.
first = input("First variable")
# define second here, so you guarantee that the variable exists
# in case you don't enter the conditional.
# Otherwise you might have problems later on if you try to access
# a variable that doesn't exist.
# You might want to initialize it to None instead of "".
second = ""
# change to your actual condition.
if first == "hello":
second = input("Second variable")
# the rest of the code goes here.
print(f"First is: {first}")
print(f"Second is: {second}")
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
I'm testing stuff with pythong (I'm really mediocre and still learning); I made a game thing and i want to count how many times wins/guesses. I've tried some ways that define a function but the variable must be called before the function so it resets; again I'm really bad so I'd appreciate if someone can write me an example/explanation. PS: I'm new to stack overflow if you can't tell so apologies if didnt use a specific format or something :D
If you want to count wins, a good way to do this is with a for loop. These are sections of code with repeat a certain number of times. You can put your main game structure in a function, which is completed at each iteration of the loop. So let's say your actual game is a function called main_game():
def main_game():
# all your game code can go here
if won: # you can change this to reflect your own code
return True # the return statement passes a value back to the place that called it
else:
return False
A side note: if you don't understand basic python constructs like return or def, take a look at the python docs.
We can then loop this function round. The range() here takes an argument which represents the number of times this should be carried out, so in this case 10 times.
wins = 0
for i in range(10): # this a for loop. "i" represents the iteration of the loop.
result = main_game() # this calls the function
if result == True: # in other words, if they've scored
wins = wins+1 # add one to the wins total
The expression result = main_game() basically assigns whatever main_game() returns to a variable called result. That's why we use return True rather than print(True) in our main_game function, because it sends that value back to result.
Then it checks to see if the user won, and if so, increments their win total before iterating. Notice there are no elif or else statements, because if result is anything other than True, we don't have to do anything with the win total.
This is a basic skeleton for how you can use a counter which goes up based on a condition to do with your game, and requires some expansion, but should give you a few pointers.
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 2 years ago.
Improve this question
I have a question about two different ways of writing a piece of code. I want to know whether they are both okay or one is better under some conditions? Basically, is it better to give the variable we want to update to the function or not?
def f1(num):
output.append(num)
output = []
f1(2)
print(output)
and
def f1(num, output):
output.append(num)
output = []
f1(2, output)
print(output)
In the first example, your function works for only adding element to globally defined certain array. And it is not good approach, you cannot use it for another array.
Second one has generic approach which is better. But only one small correction; you have an array named output, and you pass it to your function, but you keep its name same in your function. So, for your function, there are two output one global and one local, better use different names in this case:
output = []
def f1(num, arr):
arr.append(num)
f1(2, output)
print(output)
Please see warning PyCharm shows in same naming case:
Consider avoiding to use the first example where possible: global variables can be very difficult to work with, generating problems you never find easily. Instead, use the second piece of code.
You could also write something like the following code:
output = []
def add(num, listName):
listName.append(num)
return listname
for _ in range(5):
output = add(_, output)
print(output)
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 am trying to declare a python variable that stores a function with the desired parameters, but when I run it, it runs the last declared variable. I am trying to store it in a variable and when I write the variable name, it runs the function with parameters associated. It is meant to run in a program called Processing:
size(1000,500)
background(255,255,255)
cred = fill(255,0,0)
corange = fill(255,127,0)
cyellow = fill(255,255,0)
cgreen = fill(0,255,0)
cblue = fill(0,0,255)
cpurple = fill(143,0,255)
mcolors = [cred,corange,cyellow, cgreen, cblue, cpurple]
y=0
def palette():
global y
global mcolors
for i in mcolors:
i
rect(0,y,20,20)
y+=22
palette() `
One way would be to wrap the calls in lambda functions:
cred = lambda: fill(255, 0, 0)
To call it you would still need the (), i.e.:
i()
However I think in this case it might be better to just store the color values rather than the actual function references, i.e.:
cred = (255, 0, 0)
This creates a tuple of the values (kind of like an immutable list). It's different from the function call parenthesis. Then later you could pass these to a function in your loop:
fill(*i)
The star operator will make it use the values from the tuple as separate arguments rather than one argument.
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 know this is a stupid question but I don't know exactly how to search for it.
I want to feed a parameter into a function to conditionally run code.
In the example below, if I specify the word single in the function call, I would like it to run the line of code at the top and return the string "This". If I specify any other word, I want it to run the second line of code and return "That".
Example:
def condfunc(myvar):
if myvar == single:
something = "This"
else:
something = "That"
return something
mysomething = condfunc(single)
I keep getting:
NameError: name 'single' is not defined
I think you are trying to test which of two strings the argument myvar is? In that case, the code should look like:
def condfunc(myvar):
if myvar == "single":
something = "This"
else:
something = "That"
return something
which can be simplified to:
def condfunc(myvar):
return "This" if myvar == "single" else "That"
and you would call it, e.g.:
test = "single"
mysomething = condfunc(test)