PYTHON - Stop executing function/def [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 9 years ago.
Improve this question
a = 0
if (a==0):
print "a==0"
print "hello"
I have many code where the print "hello" is, and I can't put an else and just TAB everything. Is there anything that can stop executing the function or something?

In Python programs, it's much better to use spaces to tabs. If you can, configure your IDE to do this. The parenthesis surrounding (a==0) are superfluous.
a= 0
if a == 0 :
print "a==0"
print "hello"
The quickest way to stop a function's execution is instruction return .

Related

Variable with " " in Python [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 months ago.
Improve this question
Trying to make a for loop work in Python I came across a variable declared with "".
i.e: res=""
What is the purpose of the quotes?
Want to know what happens in there.
res="" is an empty string. This can be used later to, for example:
Add another string to it: res += "ABC"
Check whether there is a result. An empty string returns False, while a string with at least 1 character returns True.
Check the length of the string by len(res).
I could go on but you get the point. It's a placeholder.

append an a expression containing a variable to a list in Python [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 1 year ago.
Improve this question
the expected output of this code is [x**2,x+2] but when i try to run it the system gives me an error saying that the variable 'x' is not defined.how to fix this issue. I would like to get the map the expression to a graph.
l=[1,2]
out=[]
for i in l:
if i==1:
y=x**2
out.append(y)
if i==2:
y=x+2
out.append(y)
print(out)
At y=x**2 you have used the variable x but you have not defined it, which is causing the error. I'm assuming you want it as a string, so just do y="x**2".

i want to write a python program that converts an integer to its corresponding month [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
I want to add a error message if it exceeds 12 or if a character is entered without any if statements. here is what I have written so far:
import calender
def get_mont_name(month_number):
try:
return calender.month_name[month_number]
except IndexError:
print("'[]' is not a valid month number".format(month_number))
Your code is really close to working!
Firstly, you've spelt calendar wrong, but I assume this is just a typo.
You've slightly misused format() which is probably causing your issue. Replacing your print statement with the following should fix it:
print("{num} is not a valid month number".format(num = month_number))
The curly brackets allow the format() method to identify the parts of the string you'd like to modify.
I hope this helps!

how to give pattern to string in python 3 script? [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 4 years ago.
Improve this question
I want to give a pattern to my strings in a python script.
I mean something like this:
x = "hello" + anyString
Note: this anyStringcan be anything even nothing
Update:
at this code, I want to type anything at the end of the string and get the same result
userCommand = input("you> ")
if userCommand == "hello":
print("hi!")
result:
you> hello
hi!
i want to get same result when i type anything else after hello something like this:hello!
userCommand = input("you> ")
if userCommand.startswith("hello"):
print("hi!")

Python: remove characters from a string? [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 took a python course back when i was in high school but now I barely remember anything about it. I'm bored today and though I should try some python exercises.
Example:
string = '3dc8uo8c33a v8c08oizl6ga'
The code needs to remove 3d 8u 8c ... ect
so that the
answer = 'coca cola'
Assuming the rule is "split the string along whitespace, then take every third letter of the words, and add them back together", you can use
>>> string = '3dc8uo8cc33a v8c08oizl6ga'
>>> " ".join("".join(s[2::3]) for s in string.split())
'coca cola'

Categories