I made a dictionary switcher as follows:
switcher={
0:linked_list,
1:queue,
2:stack
}
and I used switcher[key]() to just call a function.
The function runs as normal but the issue is it prints None before
taking input in while loop of my called function, in this case linked_list()
while(c!=2):
c=int(input(print("Enter operation\n1.Insert beg\n2.Exit")))
if c==1:
some code
I have tried using a return statement and lambda but still it prints None.Also I am not printing the given function.
Because what you are trying to write to the standard output is not your menu seen as a string but the object resulting from the print function.
print function is useless. Argument sent to input function is by default written to the standard output.
Therefore:
while(c!=2):
c=int(input("Enter operation\n1.Insert beg\n2.Exit\n"))
if c == 1:
some code
is enough (with an extra newline after Exit option for more readibility).
See here official documentation about input function.
Related
I'm processing some textual data and I transform them into interpretable commands that would be used as argument for a WHERE statement but I get a string and I don't know how to use it.
For example from the string :
'c_programme_nom == "2-Broke-Girls"'
I get :
"F.col('name').like('%2-Broke-Girls%')"
But I get a string and I would like to use it as a parameter in a WHERE statement.
The expected result would be :
df.where(F.col('name').like('%2-Broke-Girls%'))
I don't know if there is a way to do it.
Seems like you're looking to execute strings containing code:
You can use exec in python:
exec() function is used for the dynamic execution of Python program which can either be a string or object code. If it is a string, the string is parsed as a suite of Python statements which is then executed unless a syntax error occurs and if it is an object code, it is simply executed.
exec('print("The sum of 5 and 10 is", (5+10))')
# The sum of 5 and 10 is 15
I want to know whether I can use Input function under for loops and Lists?
I'm using the latest version of python 3.7.4.
List=['apple','Pomegranate','orange']
K=print(input('Enter the Value:'))
if (K in List):
print("yes it's in the list")
else:
print("It's not in the list")
If I entered apple I'm getting the result as it's not on the list. I want to know whether we can use Input function under for loops and lists with if-else conditions.
Your issue is with the line
K=print(input('Enter the Value:'))
You do not need print here. Print is a function that takes a value, prints it to your screen and returns None. You passed the input to print, but you want to store the value in K, not print it to the screen (the user is entering the value so they probably do not need to see it again). So change this to:
K=input('Enter the Value:')
Here you can check your error with print function.
List=['apple','Pomegranate','orange']
K=print(input('Enter the Value:'))
print(K)
.....
K is None in this case.
I was learning the map function,
I was trying to get the message length by using map().
my code is
messages['length'] = messages['message'].map(lambda text: len(text))
But I am not sure since I read the map ducument map(functions, list)
The code above, should I include the list?
Thanks guys
map() is a function, and not a method. That means some_object.map(some_function) isn't valid syntax.
The proper call would be:
messages['length'] = map(len, messages['message'])
I'm working on a quiz program and need some help. I'm trying to replace words one at a time, but Python isn't saving the previously replaced string. Here is a mini example of what I mean:
replacedQuiz=""
easyQuiz = """
You can change a string variable to an integer by typing (__1__)
in front of the variable. It also works vice versa, you can change an
integer
variable to a string by typing (__2__). This is important to remember before
you __3__ strings together, or else a TypeError will occur. While adding an
integer to a string, it is important to separate it using a __4__ (use the
symbol). \n"""
def replaceWord(replaced, quiz, numCount):
if numCount == 1:
replaced = quiz.replace("__1__", "int")
if numCount == 2:
replaced = replaced.replace("__2__", "str")
if numCount == 3:
replaced= replaced.replace("__3__", "concatenate")
if numCount == 4:
replaced= replaced.replace("__4__", ",")
print replaced
def easy():
QCount=1
print easyQuiz
while QCount < 5:
replaceWord(replacedQuiz, easyQuiz, QCount)
QCount += 1
print easy()
I thought that by making a String called replacedQuiz, it would save the first replacement and then I could continue replacing the words inside the quiz and updating it. Please help! I don't know where I'm going wrong
You seem to have made a slight mistake in the scope of your variable replacedQuiz (it'd certainly suggest that you check out some explanation of this topic). Basically, you are replacing replacedQuiz by its new value only within your current function. Your other functions only have access to the global value you defined earlier. There are several ways to fix this (e.g. the global keyword) but the standard way would be to return the new replacedQuiz from your function.
To do so, add the following line to the end of your replaceWord function:
return replacedQuiz
This tells Python to use this value at the line it was called at. You can then define a new value for replacedQuiz within easy by just defining it as the returned value:
replacedQuiz = replaceWord(replacedQuiz, easyQuiz, QCount)
I am trying to build a command which is similar to LaTeX \cite{}, which accepts a comma-separated list of parameters like this
\cite{Wall91, Schwartz93}
I would like to pass each item in the comma-separated list which the parameter represents to another command and return the concatenation of the individual results. I imagine it to be something like this:
\newcommand{\mycite}[1]{%
\#for\var:=\split{#1} do{%
\processCitation{\var}%
}%
}
Literature on String manipulation, variables and looping in LaTeX would be great!
Also: Is there a way to join the individual results using commas again?
Thanks!
Using Roberto's link I arrived at this solution:
\makeatletter
% Functional foreach construct
% #1 - Function to call on each comma-separated item in #3
% #2 - Parameter to pass to function in #1 as first parameter
% #3 - Comma-separated list of items to pass as second parameter to function #1
\def\foreach#1#2#3{%
\#test#foreach{#1}{#2}#3,\#end#token%
}
% Internal helper function - Eats one input
\def\#swallow#1{}
% Internal helper function - Checks the next character after #1 and #2 and
% continues loop iteration if \#end#token is not found
\def\#test#foreach#1#2{%
\#ifnextchar\#end#token%
{\#swallow}%
{\#foreach{#1}{#2}}%
}
% Internal helper function - Calls #1{#2}{#3} and recurses
% The magic of splitting the third parameter occurs in the pattern matching of the \def
\def\#foreach#1#2#3,#4\#end#token{%
#1{#2}{#3}%
\#test#foreach{#1}{#2}#4\#end#token%
}
\makeatother
Usage example:
% Example-function used in foreach, which takes two params and builds hrefs
\def\makehref#1#2{\href{#1/#2}{#2}}
% Using foreach by passing #1=function, #2=constant parameter, #3=comma-separated list
\foreach{\makehref}{http://stackoverflow.com}{2409851,2408268}
% Will in effect do
\href{http://stackoverflow.com/2409851}{2409851}\href{http://stackoverflow.com/2408268}{2408268}